Struts Programming

80
rate this page

By learnprogramming


What is Struts ?

Struts is an implementation of the MVC design pattern for Java Web Application.

The Struts Framework is a standard for developing Web applications based on the Model-View-Controller (MVC) design paradigm, distinctly separating all three levels:

  • Model: application state
  • View: presentation of data (JSP, HTML)
  • Controller: routing of the application flow. The Controller reacts to the user input. It creates and sets the model.

Struts is basically the Controller. The Struts Controller is a servlet that maps events (an event generally being an HTTP post) to classes.


How Struts Works ?

  • Client browser

    An HTTP request from the client browser creates an event. The Web container will respond with an HTTP response.

  • Controller

    The Controller receives the request from the browser, and makes the decision where to send the request. With Struts, the Controller is a command design pattern implemented as a servlet. The struts-config.xml file configures the Controller. The controller servlet knows the configuration file from the web.xml where it is declared as follows :

    <servlet>

    ....

    <init-param>

    <param-name>config</param-name>

    <param-value>/WEB-INF/struts-config.xml</param-value>

    </init-param>

    </servlet>

  • Business logic

    The business logic updates the state of the model and helps control the flow of the application. With Struts this is done with an Action class as a thin wrapper to the actual business logic.

  • Model state

    The model represents the state of the application. The business objects update the application state. ActionForm bean represents the Model state at a session or request level, and not at a persistent level. The JSP file reads information from the ActionForm bean using JSP tags.

  • View

    The view is simply a JSP file. There is no flow logic, no business logic, and no model information -- just tags. Tags are one of the things that make Struts unique compared to other frameworks like Velocity.

How to implement Struts ?

Struts provides the controller in the form of an ActionServlet class. The framework uses this class for handling all the requests. To enable your application to use this controller servlet, include the following in the deployment descriptor i.e. web.xml

<servlet>

<servlet-name>EAIActionServlet</servlet-name>

<servlet-class>org.apache.struts.action.ActionServlet

</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>EAIActionServlet</servlet-name>

<url-pattern>/*.do</URL-pattern>

</servlet-mapping>

In web.xml, we also need to specify the mapping of application URLs to this servlet. In the above code any URL with .do extension calls the ActionServlet to handle the request.

To carry out an appropriate action for a particular request, the ActionServlet is configured using Action Mappings in the struts-config.xml file. An Action Mapping specifies a fully qualified Action class name that should be invoked when a URL matching its path is requested. Following is a sample action mapping :

<action path="/Register"

type="oracle.otnsamples.jintdemo.marvel.web.actions.RegisterAction"

scope="request"

name="registerForm">

<forward name="success" path="/jsps/Message.jsp"/>

<forward name="failure" path="/jsps/Register.jsp"/>

<forward name="addCart" path="/AddToCart.do"/>

</action>

When a URL for the application ending with Register.do is called, the ActionServlet searches for a mapping with path as /Register. It then instantiates the class defined by the type attribute to perform the required action. Any form parameters in the request object are passed to the Action class using the form bean defined by the name attribute. The name attribute defines a logical name for the form bean, the mapping for which done is using <form-bean> tag in sturts-config.xml file as shown below:

<form-beans>

<form-bean name="registerForm"

type="oracle.otnsamples.jintdemo.marvel.web.forms.RegisterForm"/>

</form-beans>

Using this input the Action class performs operations to serve the request. Depending on the result of operation, the user is directed to the next view using the ActionForward object (which represents the <forward> tag in Action Mappings).

Relationships among ActionServlet (Controller), ActionForm (Form State), and Action (Model Wrapper).
Relationships among ActionServlet (Controller), ActionForm (Form State), and Action (Model Wrapper).

Struts Glossary (to be continued...)

Action class: wrapper around the business logic. The Controller looks at the incoming event and dispatches the request to an Action class. The struts-config.xml determines what Action class the Controller calls.

ActionForm class: represents a general concept of data that is set or updated by a HTML form.

ActionMapping: The ActionMapping contains the knowledge of how a specific event maps to specific Actions. The ActionServlet (Command) passes the ActionMapping to the Action class via the perform() method. This allows Action to access the information to control flow.

ActionServlet class: the Command (Controller) part of the MVC implementation and is the core of the Framework. ActionServlet (Command) creates and uses Action, an ActionForm, and ActionForward. It passes the parameterized classes to ActionForm using the perform() method.

struts-config.xml: struts-config.xml controls which HTML form request maps to which ActionForm. The controller servlet knows the configuration file from the web.xml. The configuration file usually is placed in the WEB-INF folder of the application Web Archive (WAR file).

  —   Rate it:  up  down  [flag this hub]

Comments

RSS for comments on this Hub Small RSS Icon

sundaris  says:
2 months ago

nice informative post ons truts thanks....will become ur an soon!

Submit a Comment

Members and Guests

Sign in or sign up and post using a hubpages account.


optional



working