March 1, 2021
Hot Topics:

Developing a Struts Application with the NetBeans IDE

  • By Anghel Leonard
  • Send Email »
  • More Articles »

Implementing the Business Logic in Struts Style

Your next task is to generate an Action class and implement the execute method. In Struts parlance: "when a request is received, the Controller invokes an Action class. The Action class consults with the Model (or, preferably, a Facade representing your Model) to examine or update the application's state." The execute method is the "brain" of your application, because here you tell the application what to do.

For starters, follow the steps below to generate an Action stub:

  1. In the Projects tab, right-click on the login project and select New | Other ... from the contextual menu.
  2. In the New File wizard's Categories list, select the Struts category and from the File Types list, select Struts Action type and press Next.
  3. Now you have to type a name for the new action. For example, type LoginAction in the Class Name section. Also, choose the com.myapp.struts package from the Package.
  4. Type /login in the Action Path section. This value must be the same as the one provided in the action attribute of the <html:form> tag in login.jsp. Click Next.
  5. In this window, you have to associate the Action with an ActionForm. By default, the IDE did that for you, but notice that this can be accomplished manually by selecting the corresponding ActionForm from the list ActionForm Bean Name.
  6. In addition:
    • Type /login.jsp for the Input Resource field, or use the Browse button.
    • Set the Scope to Request.
  7. Click Finish.

Now, you should see the generated code of your action in the Source Editor. If you look in struts-config.xml, you will notice the new added entry under Action Mappings, like this:

<action input="/login.jsp" name="LoginActionForm" path="/login" scope="request" type="com.myapp.struts.LoginAction"/>

Going further, you must add a behavior to the execute method. You have a simple scenario: the login credentials should be an email address of "admin@yahoo.com" and a password of "admin." If the provided credentials match these, then you forward the user to success.jsp, if not to failure.jsp. The code looks like this after you have used the generated code and added the needed lines:

/* forward name="success" path="" */
 private static final String SUCCESS = "success";
 private static final String FAILURE = "failure";
  * This is the action called from the Struts framework.
  * @param mapping The ActionMapping used to select this instance.
  * @param form The optional ActionForm bean for this request.
  * @param request The HTTP Request you are processing.
  * @param response The HTTP Response you are processing.
  * @throws java.lang.Exception
  * @return
 public ActionForward execute(ActionMapping mapping, ActionForm form,
         HttpServletRequest request, HttpServletResponse response)
         throws Exception {
     LoginActionForm data = (LoginActionForm)form;
     String email = data.getEmail();
     String pass = data.getPass();
     if ((email.equals("admin@yahoo.com")) && (pass.equals("admin"))){
         return mapping.findForward(SUCCESS);
     } else {
         return mapping.findForward(FAILURE);

Configure the Forwards Entries

As you can see, your action class contains two forwarding conditions, SUCCESS and FAILURE. In this section, you configure these forwards in the struts-config.xml file as follows (Without these configurations, Struts will not know what JSP pages to associate to the forwarding conditions. You know that SUCCESS should be associated to success.jsp and FAILURE to failure.jsp, and now Struts will also know that.):
  1. In struts-config.xml, right-click anywhere in the action entry for LoginActionForm and choose Struts | Add Forward from the contextual menu (notice that in this contextual menu you have an entire set of Struts components that can be added through the IDE wizards).
  2. In the Forward Name section, type "success." In the Resource File, type /success.jsp or use the Browse button to navigate to this page (see Figure 7). Click Add.

    Click here for larger image

    Figure 7. Add a Struts Forward

Repeat this process for FAILURE, but select the /failure.jsp page and type "failure" in the Forward Name section. Now, in struts-config.xml, you should have this:

<action input="/login.jsp" name="LoginActionForm" 
     path="/login" scope="request" type="com.myapp.struts.LoginAction">
  <forward name="success" path="/success.jsp"/>
  <forward name="failure" path="/failure.jsp"/>

Implementing the Validate Method of the LoginActionForm

Bottom of LoginActionForm, you have a validate method generated by the IDE (you will find a comment there, which you have to uncomment first). This method allows you to put more reusable validation in ActionForm and return non-empty ActionErrors from validate to trigger redisplay of input data.

Author Note: Remember that Struts has powerful support for validating dates. Check out the specialized tutorials for more information.

Next, you add a basic validation over the provided login credentials:

 public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
     ActionErrors errors = new ActionErrors();
     if (getEmail() == null || getEmail().length() < 6) {
         errors.add("wrongemail", new ActionMessage("errors.email", "This email"));
     if (getPass() == null || getPass().length() < 5) {
         errors.add("wrongpass", new ActionMessage("errors.minlength", "Password", "6"));
     return errors;

You're done! Now you can test the application and see how it works. Press the Run Main Project button over the IDE main toolbar! In Figure 8, you can see a simple test of the validation process.

Figure 8. Running the Login Application Sample


The NetBeans IDE provides great support for developing Struts applications, and it is a pleasure to work with in the production stage. When you discover and utilize the other facilities of NetBeans, you will have the perfect IDE for developing amazing Java-based Web applications.

Code Download

  • StrutsAppNetBeans_login.zip
  • About the Author

    Anghel Leonard is a senior Java developer with more than 13 years of experience in Java SE, Java EE, and the related frameworks. He has written and published dozens of articles about Java technologies and two books about XML and Java (one for beginners and one for experts).

    Page 3 of 3

    This article was originally published on June 21, 2010

    Enterprise Development Update

    Don't miss an article. Subscribe to our newsletter below.

    Thanks for your registration, follow us on our social networks to keep up-to-date