View RSS Feed

My Java Tips

Struts Controller

Rate this Entry
by , 11-18-2011 at 07:13 PM (979 Views)
Let us see how to configure the struts-config.xml file to map the request to some destination servlet or jsp file.


The class org.apache.struts.action.ActionServlet is at the heart of the Struts Framework. It is the Controller part of the Struts Framework. ActionServlet is configured as Servlet in the web.xml file as shown in the following code.

<!-- Standard Action Servlet Configuration (with debugging) -->
Java Code:
<servlet>
     <servlet-name>action</servlet-name>
      <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
      <init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
      </init-param>
     <init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
        </init-param>
        <init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
     </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>
This servlet is responsible for handling all the request for the Struts Framework, user can map the specific pattern of request to the ActionServlet. <servlet-mapping> tag in the web.xml file specifies the url pattern to be handled by the servlet. By default it is *.do, but it can be changed to anything. Following code form the web.xml file shows the mapping.</servlet-mapping>

<!-- Standard Action Servlet Mapping -->
Java Code:
<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>
The above mapping maps all the requests ending with .do to the ActionServlet. ActionServlet uses the configuration defined in struts-config.xml file to decide the destination of the request. Action Mapping Definitions (described below) is used to map any action.



hello.jsp
Java Code:
<ins datetime="2008-01-16T16:33:42+00:00">
&lt;%@ taglib uri="/tags/struts-bean" prefix="bean" %&gt;
&lt;%@ taglib uri="/tags/struts-html" prefix="html" %&gt;
<h3><bean:message key="hello.heading"></bean:message></h3>
<bean:message key="hello.message">

</bean:message>
</ins>
Forwarding the hello.do request to hello.jsp

The "Action Mapping Definitions" is the most important part in the struts-config.xml. This section takes a form defined in the "Form Bean Definitions" section and maps it to an action class.

Following code under the <action-mappings> tag is used to forward the request to the hello.jsp.</action-mappings>
Java Code:
<action path="/hello" forward="/pages/hello.jsp"></action>
To call this hello.jsp file we will use the following code.

First Request to the controller
Once the use clicks on on First Request to the controller link on the index page, request (for hello.do) is sent to the Controller and the controller forwards the request to hello.jsp. The content of hello.jsp is displayed to the user.

Submit "Struts Controller" to Facebook Submit "Struts Controller" to Digg Submit "Struts Controller" to del.icio.us Submit "Struts Controller" to StumbleUpon Submit "Struts Controller" to Google

Comments