Struts Made Simple PDF Print E-mail
User Rating: / 0
PoorBest 
Written by Nick Griffith   
Wednesday, 21 February 2007

Struts is a common Java framework used for Web Application development.  If you are inexperienced, it can seem pretty overwhelming but if you just understand the way the parts work together then it is extremely easy to use.  This is a quick rundown of the main components of the Struts framework and how they work together.

The Form

 

In struts, the Form is the container for data to be displayed within the JSPs.  Here is a simple form example

 

 

class SampleForm extends ActionForm

{

            private String firstName

            private String lastName

 

            public getFirstName(){

              return firstName;

            }

 

            public setFirstName(String firstName){

              firstName = firstName;

            }

 

            public getLastName(){

              return lastName;

            }

 

            public setLastName(String lastName){

             lastname = lastName;

            }

}

 

 

This form now contains a firstName and lastName and these values can be set inside the either the Action or the JSP.

 

To use these values on the JSP all you would need to do is

 

<html:form action="sample.do" method="post" >

    <html:text name=”sampleForm” property=”firstName”/>

    <html:text name=”sampleForm” property=”lastName”/>

</html:form>

Now you have 2 text fields to populate these strings when you submit the form.

 

 

The Action

 

The action is where you do all of the business logic involved in the Struts framework.  It is here that you build the methods to populate the forms to display and save data to and from the forms.

Here is a simple example action to go with the form we created earlier.

 

public class SampleAction extends Action {

 

    public ActionForward execute(ActionMapping mapping,

                                 ActionForm form,

                                 HttpServletRequest request,

                                 HttpServletResponse response)  {

       

            SampleForm theForm = (SampleForm) form;

 

            theForm.setFirstName(“Jon”);

            theForm.setLastName(“Doe”);

            return mapping.findForward(“sample”);     

    }

}

 

This action will be called when the url of  /name.do is called.  You set the URL that corresponds to this action in the struts-config.xml.  You also link the action to the corresponding form in this config file.

 

Struts-config.xml

 

The struts-config.xml is where you link everything together.  It is pretty self explanatory but if you need to know what all the different TLD definitions are then go to the struts website.  Here is the sample config file to go with the SampleAction and SampleForm

 

<struts-config>

 

    <form-beans>

       <form-bean name="SampleForm" type="com.sample.form.SampleForm"/>

    </form-beans>

    <action-mappings>

      <action path="/sample" type="com.sample.action.SampleAction" name="SampleForm" >

          <forward name="sample" path="/sample.jsp"/>

      </action>

</struts-config>

 

Put these three features together and you now have a simple struts application.

 

 

 

Last Updated ( Wednesday, 21 February 2007 )
 
a