JBoss Struts tutorial

This article explains how you can setup quickly an application based on Struts on JBoss 5 application server.  

Install JBoss Struts plugin on Eclipse

For our example we have installed Eclipse IDE for Java Developers (Ganymede) which is the Enterprise Version of Eclipse foundation: check it here:
http://www.eclipse.org/downloads/

Once you have extracted Eclipse you need to update your plugins: from the Help Menu, select "Software Updates". In the "Available Software" Click on "Add site" and add JBoss plugins site:
http://download.jboss.org/jbosstools/updates/nightly/trunk/
Here there's a whole lot of plugins, however what you need is basically JBoss AS plugin and Struts plugin. Once installed you have to restart Eclipse.

Create the Web project

Create a new Dynamic Web Project from Menu : File | New | Other | Web | Dynamic Web project.

jboss struts
Set the Web module version to 2.5 and choose as Runtime your jBoss Runtime environment.

Now Add Struts capabilities to your project. Right click on it and select:

jboss struts
The Struts wizard will start. The first gui will ask where you have your web.xml file. You can leave the default value.

Next GUI, Project Modules, will add Struts Support to your Project. Click on the Button and select the Struts version and configuation files.

jboss struts tutorial
Now your project Modules should be compiled with the correct configuration. Click next.

In the last applet you will basically select the classes/lib path Servlet and Struts version. Select Servlet version 2.5 , Struts 1.2 and leave unchecked the "Add libraries".

jboss struts

Unfortunately I found some incompatibilities with the libraries provided by the Tool so we have to donwload the libraries from Jakarta site.

Download Struts libraries

Download Struts 1.3 libraries from jakarta site:
http://apache.fastbull.org/struts/library/struts-1.3.10-lib.zip

Unzip the libraries in a folder, such as StrutsLibs

Configure the Project to use Struts libraries

Configuring your project to use Struts libraries by right-clicking on the Project and selecting "Properties". Configure the build-path by adding all libraries except commons-logging.jar which is already loaded by JBoss.

jboss struts
The libraries you have selected needs to be added also in the WEB-INF/lib folder. You can do this by selecting again Project properties | Java EE Module Dependancies.
Check all Struts libraries and Click Ok.

Add Struts Components 

Following is a sample Struts application which we have used to test Struts on JBoss.
The configuration file struts-config.xml contains basically one Action named com.sample.LoginAction , one FormBean named com.sample.LoginFormBean and three jsp: login.jsp, welcome.jsp and error.jsp.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
                               "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
 <data-sources/>
 <form-beans>
  <form-bean name="LoginForm" type="com.sample.LoginFormBean"/>
 </form-beans>
 <global-exceptions/>
 <global-forwards/>
 <action-mappings>
  <action input="/login.jsp" name="LoginForm" path="/checkLogin"
   scope="request" type="com.sample.LoginAction">
   <forward name="welcome" path="/welcome.jsp"/>
   <forward name="error" path="/error.jsp"/>
  </action>
 </action-mappings>
 <controller/>
 <message-resources parameter="com.sample.ApplicationResources"/>
</struts-config>

This is how our configuration looks like from the Diagram view:

jboss struts
This is our login.jsp page:  

<%@ taglib uri="/WEB-INF/struts-html" prefix="html"%>

<html:html>

<body>
<html:form action="/checkLogin">
    <html:errors />

    <table>
        <tr>
            <td align="center" colspan="2"><font size="4">Please
            Login</font>
        </tr>
        <tr>
            <td align="right">Username</td>
            <td align="left"><html:text property="username" size="30"
                maxlength="30" /></td>
        </tr>
        <tr>
            <td align="right">Password</td>
            <td align="left"><html:text property="password" size="30"
                maxlength="30" /></td>
        </tr>

        <tr>
            <td align="right"><html:submit>Login</html:submit></td>
            <td align="left"><html:cancel>Cancel</html:cancel></td>
        </tr>
    </table>
</html:form>


</body>
</html:html>

The login.jsp is backed by a LoginFormBean which contains the mapping for the corresponding fields:


package com.sample;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class LoginFormBean extends org.apache.struts.action.ActionForm {

    String username;
    String password;
    
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public LoginFormBean () {
    }

    public void reset(ActionMapping actionMapping, HttpServletRequest request) {
        // TODO: Write method body
        this.username = null;
        this.password = null;
    }

    public ActionErrors validate(ActionMapping actionMapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        
        if( getUsername() == null || getUsername().length() < 1 ) {
            errors.add("username",new ActionMessage("username.error"));
          }
          if( getPassword() == null || getPassword().length() < 1 ) {
            errors.add("password",new ActionMessage("password.error"));
        }
          return errors;

    }


}
 

Notice this class uses a Validator to check the validity of the fields. You have to add a file named ApplicationResources.properties in the folder com.sample

username.error=username cannot be null

password.error=Password cannot be null

Last piece of code is the LogicAction which demandates the logic for logging in to an external DAO class....(which you have to implement)


package com.sample;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

 
import com.sample.LoginDAO;
import com.sample.LoginFormBean;

public class LoginAction extends org.apache.struts.action.Action {
       
    public LoginAction() {
    }
    
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        LoginFormBean loginForm = (LoginFormBean)form;
        boolean validLogin = LoginDAO.login(loginForm.getUsername(),
                                            loginForm.getPassword());
        
        if (validLogin) {
            return mapping.findForward("welcome");           
        }
        else {
            return mapping.findForward("error");            
        }
    }

}

That's all. Add two landing pages welcome.jsp and error.jsp and deploy your application. If you have deployed your application as StrutsExample.war then you can access to it with: http://localhost:8080/StrutsExample/login.jsp


Have fun with JBoss and Struts !
Click here to download the code

Related articles available on mastertheboss.com

How do I reload dynamically my jsp from an ear ?

  Deploy your Jsp as part of an application deployed in exploded

Where is my compiled JSP ?

JBoss recipe of the day

How do you configure JBoss to enable HTTP logging ?

To enable HTTP logging, you need to go to the deploy/jbossweb-tom

How to restrict access to JBoss web application by IP or Host?

You can create easily Access control list based on the IP address

How do you configure a Web Application as Default Web app ?

  JBoss AS 4-5-6 First you have to remove the default

Developing Portlets with JBoss Portal

JBoss Portal is an open source platform for hosting and serving a

Follow us on Twitter