Using Apache Struts as a web framework

Apache Struts is one of the best-known web frameworks. It is on top of the Java Platform and hosted by the Apache Foundation. Struts is available for free, under the Apache License.

The main goal of Apache Struts is to build web applications with a clear separation between the Model, View, and Controller. As with JSF, there is one focal point of configuration, the struts-config.xml and the controller, called ActionServlet, which controls the navigation flow. Other parts of the Struts framework are:

  • Action: Basically these are the requests from the client. The ActionServlet checks the URI to determine which action to call. Actions are used to access the business classes.
  • ActionForm: Beans used by the action to temporarily store and validate data from the form pages.

Struts is one of the many web frameworks supported by NetBeans. The editor makes it possible to add XML configuration tags to struts-config.xml, handle ActionServlet and ActionForm, and other features which we will check soon.

How to do it...

We will need to create a New Project and add the Struts Framework along the way. Here's how.

Right-click on the Projects window, select New Project:

  1. A New Projects dialog is shown. Under Categories, select Java Web and under Projects, select Web Application, and then click Next.
  2. Under Name and Location:
    • Type StrutsWebApp under Project Name.
    • Leave Project Location with the default value.
    • Leave Project Folder with the default value.
    • Select Use Dedicated Folder for Storing Libraries if not marked.
    • Select Set as Main Project.
  3. Click Next.
  4. Under Server and Setting, enter:
    • Server : Apache Tomcat 7.0.11
    • Java EE version : Java EE 5
    • Context Path : /StrutsWebApp
  5. Click Next.
  6. Frameworks: Under the Frameworks selection, click on Struts 1.3.8 (please note that a newer version of Struts might be available from NetBeans IDE; this recipe may work on this version too).
  7. Leave all the values in Action Servlet Name, Action URL Pattern, and Application Resource with their default values.
  8. Click Finish.

IDE adds libraries, configuration files, and opens the welcomeStrutsPage.jsp in the Editor.

The steps for creating the user registration page are as follows:

  1. Right-click on StrutsWebApp.
  2. Select New and JSP....
  3. When the New JSP File dialog opens, under File Name, type register-user-page.
  4. Leave Project and Location with their default values.
  5. Under Folder, type pages.
  6. Leave Options with JSP File (Standard Syntax).
  7. Click Finish.

With register-user-page.jsp open in the editor, add the HTML and bean taglibs right after the @page:

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>

And replace the following tag:

<h1>Hello World!</h1>

With:

<html:form action="/register">
<table border="0">
<thead>
<tr>
<th>Username:</th>
<th><html:text property="username" /></th>
</tr>
</thead>
<tbody>
<tr>
<td>Password:</td>
<td><html:password property="password" /> </td>
</tr>
<tr>
<td>E-mail</td>
<td><html:text property="email" /></td>
</tr>
<tr>
<td>Country:</td>
<td>
<html:select property="country">
<html:option value="brazil">Brazil</html:option>
<html:option value="finland">Finland</html:option>
<html:option value="india">India</html:option>
<html:option value="usa">USA</html:option>
</html:select>
</td>
</tr>
</tbody>
</table>
<html:submit value="Register" />
</html:form>

Next step is to create the success page:

  1. Right-click on StrutsWebApp.
  2. Select New and JSP....
  3. When the New JSP File dialog opens, under File Name, type success.
  4. Leave Project and Location with their default values.
  5. Under Folder, type pages.
  6. Leave Options with JSP File (Standard Syntax).
  7. Click Finish.

With success.jsp open in the editor, add the bean taglib to the page right after the @page:

<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>

And replace the following tag:

<h1>Hello World!</h1>

With:

User <bean:write name="RegisterForm" property="username" /> successfully created.

Since it is a Struts project, we will need to add an ActionForm to our project that will be named as RegisterForm.

  1. Right-click on StrutsWebApp.
  2. Select New and Other.
  3. When the New File dialog opens; Choose File Type: Under Categories, select Struts.
  4. Under File Types, select Struts ActionForm Bean.
  5. Click Next.
    How to do it...
  6. Under Name and Location:
    • Under Class Name, type RegisterForm.
    • Leave Project and Location with their default values.
    • Under Package, type actionforms.
    • Leave Created File, Superclass, and Configuration File with their default values.
  7. Click Finish.
  8. Delete the contents of the class and add:
    private String username;
    private String password;
    private String email;
    private String country;
    
  9. Then press Alt+Insert and when the Generate dialog shows up, select Getter and Setter..., as shown in the following screenshot:
    How to do it...
  10. Click on all the fields under Generate Getters and Setters dialog and click Generate.
How to do it...

Finally, we will tie everything together by creating the Struts Action.

  1. Right-click on StrutsWebApp.
  2. Select New and Other.
  3. When the New File dialog opens, for Choose File Type: Under Categories, select Struts.
  4. Under File Types, select Struts Action.
  5. Click Next >.
  6. Name and Location:
    • Under Class Name, enter RegisterAction
    • Leave Package and Source Packages with their default values
    • Under Package, type actions
    • Leave Created File, Superclass, and Configuration File with their default values
    • Under Action Path, enter /register
  7. Click Next.
    How to do it...
  8. ActionForm Bean, Parameter:
    • Under ActionForm Bean Name, the IDE automatically finds RegisterForm
    • On Input Source, erase the values from the field
    • For Scope, select Request
    • Uncheck Validate ActionForm Bean
How to do it...

The last detail to add is a forward tag in struts-config.xml.

Open struts-config.xml:

  1. Right-click inside the editor and select Struts and Add Forward.
  2. On the Add Forward dialog, enter the following configuration:
    • Under Forward Name, type success.
    • On the Forward Top section, click on Browse....
    • The Browse Files dialog opens, expand Web Pages, Pages and click on success.jsp, and click on Select File.
    • Back on the Add Forward dialog, under the Location section, click on Action, and select /register.
  3. Click Add.
How to do it...

Back on the Project window, navigate to the register-user-page.jsp, right-click on it, and select Run.

How it works...

In this recipe, we have created a simple case of registering a user.

No database is involved, just a simple way to show how NetBeans handles the very basics of Struts.

In the user registration page, we have the first usage of Struts components in the web page. The components in this page come from the HTML taglib.

<html:form action="/register"> defines which action is being pointed at after the<html:submit> is used.

A combination of<html:text>, <html:password>, <html:select>, and<html:submit> are used. The names, by themselves, are very descriptive:

  • <html:text> provides a text box
  • <html:password> also provides a text box, but with character obfuscation
  • <html:select> shows a normal drop box with some countries pre-configured
  • <html:submit> triggers the page to be submitted to the corresponding action

We proceed to create our success page. This page confirms that everything went well with the registration of our user, and prints the user name on the screen.

For this to happen, we need to use the bean taglib. This tag requires the ActionForm and the property to be printed:

<bean:write name="RegisterForm" property="username" />

Readers would have to create their own implementation of Struts ActionForm class/interface. This implementation should contain a few simple String-based properties. NetBeans can help with this by automatically generating getters and setters.

The set is complete by using the IDE to create the action class. The IDE generates the class, adds a temporary implementation of the execute method and inserts an action tag to the sturts-config.xml.

Finally, with the IDE's help, we edit struts-config.xml once more to add the forward tag with a neat GUI interface.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.96.5