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:
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.
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:
IDE adds libraries, configuration files, and opens the welcomeStrutsPage.jsp
in the Editor.
The steps for creating the user registration page are as follows:
register-user-page
.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:
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.
RegisterForm
. actionforms
.private String username; private String password; private String email; private String country;
Finally, we will tie everything together by creating the Struts Action.
RegisterAction
The last detail to add is a forward tag in struts-config.xml
.
Open struts-config.xml:
success
. success.jsp
, and click on Select File.Back on the Project window, navigate to the register-user-page.jsp
, right-click on it, and select Run.
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 actionWe 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.
18.188.96.5