- 1). Create a new Struts project by clicking "File" and "New Project." Under "Java Web," select "Web Application" and click "Next." When prompted, name your application "StrutsTutorialApp" and click "Next" again. You will be given a choice between "GlassFish" and "Apache Tomcat" as your HTTP server. Leave the default for now and click "Next." Finally, you will be given a choice of supported Web App frameworks to use in your application. The frameworks are not mutually exclusive: you can select as many or as few as you need. However, keep the complexity at a minimum for now and simply select "Struts" and click "Finish."
NetBeans will now automatically generate a basic Struts Web-app structure for your use. You can test it by pressing "F6." This will take a few moments; NetBeans needs to start the Web server you selected, start the Java Database, and open the Web app in you default Web browser. Once it finishes, however, you should see a short message welcoming you to Struts development in NetBeans. - 2). Add a Web-form to your application. To do this, double-click the "index.jsp" file in the project viewer. A Java Server Page document will be opened containing the HTML and Javascript code for the current opening page. Adding a Web form is a simple process. First, add the following code within the <body> tags of the existing page:
<html:form action="/login">
</html:form>
Next, move the cursor so it is just between these two tags. Click "Window" and "Palettes." Drag the "Table" option to the current cursor position. In the dialog, set the "Rows" to 3, the "Columns" to 2, and all the other values to 0 and click OK. The following code will be inserted:
<table border="0">
<thead>
<tr>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</tbody>
</table>
Remove all the lines between (and including) "<thead>" and "</thead>." They aren't needed. This leaves a table body with two rows (tr) and two columns for each row (td).
Fill in the table so it reads as follows:
<table border="0">
<tbody>
<tr>
<td colspan="2">
<bean:write name="LoginForm" property="error" filter="false"/>
</td>
</tr>
<tr>
<td>Enter your name:</td>
<td><html:text property="name" /></td>
</tr>
<tr>
<td>Enter your email:</td>
<td><html:text property="email" /></td>
</tr>
<tr>
<td></td>
<td><html:submit value="Login" /></td>
</tr>
</tbody>
</table>
Click "Run" again and you should see your form; however, the "Login" button doesn't work, because you still need to define an Action to handle it and a page to be displayed when you have logged in. - 3). Create an ActionForm bean. An ActionForm is a Struts component that allows the server to remember (or "persist") data from a client between Web requests. To create one, right-click the name of your project in the project view and select "New" and "Other." Under "Struts," select "Struts ActionForm Bean" and click "Next." Name it "LoginForm" and select your Web-app name in the "Package" drop-down box.
This creates a Java file named "LoginForm" with some basic structure already existing. This ActionForm needs to store the information from the login in the previous step, so it needs a "name" and an "email." It already has a name, so half the work is done. Add the following line within the class:
private String email;
Next, click the word "email" and press "Alt-Insert" on your keyboard. This calls up the automatic code generator. Select "Getter and Setter." - 4). Create an Action. An Action is the Struts component that handles any processing that must be handled by the application between when the user sends a request and the application answers. To create an Action, right-click your project name in the project viewer and select "New" and "Other." Go to "Struts" and select "Struts Action." Name the action "LoginAction" and select your project from the package list. Finally, type "/login" into the Action Path field.
Paste the following code within the "execute" method:
LoginForm formBean = (LoginForm)form;
String name = formBean.getName();
String email = formBean.getEmail();
if ((email.indexOf("@") == -1) {
return mapping.findForward("failure");
}
return mapping.findForward("success");
You should notice the lines that read 'mapping.findForward("success")' and "failure". The Action is going to look for a forwarding rule for how to handle the events "success" and "failure." Specifically, if the email address doesn't contain an "@" sign, it will forward the user to the "failure" page. Otherwise, the user goes to the "success" page. - 5). Open "struts.config.xml" from the project window and right-click the line that reads "LoginForm." Select "Struts" and select "Add Forward." Name it "success" and set the "Resource File" to "/WEB-INF/success.jsp." Click "Add."
Repeat this to create another forward named "failure." This one should use "index.jsp" as its resource file. - 6). Create the "success.jsp" page by right-clicking the "WEB-INF" folder in your window and choosing "New" and "JSP." Paste the following code within it:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login Success</title>
</head>
<body>
<h1>Congratulations!</h1>
<p>You have successfully logged in.</p>
<p>Your name is: .</p>
<p>Your email address is: .</p>
</body>
Your first Struts application is now finished! Press "F6" to run the program and test it out. Try two tests: first, enter a valid name and email address to see the success page. Second, enter a name with an invalid (no @) email address to see the failure page.
previous post
next post