First Struts Application

With the Struts architecture in mind, in this section, we create a simple Struts application with just one Struts action that forwards to a JSP page. It does not perform any special processing; it simply proves that the Struts framework is loaded and that the Spring application is using it to process the requests.

Now we create a web.xml descriptor, a struts-config.xml Struts descriptor file, and Spring applicationContext.xml files. The web.xml file includes the Spring servlets as well as the Struts servlet, both of which appear in Listing 19-1.

Listing 19-1: The web.xml Descriptor

image from book
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC      "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"      ""> <web-app>     <display-name>Pro Spring Chapter 19 Sample application</display-name>     <description>dtto</description>     <context-param>         <param-name>contextConfigLocation</param-name>         <param-value>WEB-INF/applicationContext.xml</param-value>     </context-param>     <servlet>         <servlet-name>context</servlet-name>         <servlet-class>org.springframework.web.context.ContextLoaderServlet         </servlet-class>         <load-on-startup>1</load-on-startup>     </servlet>     <servlet>         <servlet-name>ch19</servlet-name>         <servlet-class>             org.springframework.web.servlet.DispatcherServlet         </servlet-class>         <load-on-startup>2</load-on-startup>     </servlet>     <servlet>         <servlet-name>action</servlet-name>         <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>         <load-on-startup>3</load-on-startup>     </servlet>         <servlet-mapping>         <servlet-name>ch19</servlet-name>         <url-pattern>*.html</url-pattern>     </servlet-mapping>     <servlet-mapping>         <servlet-name>ch19</servlet-name>         <url-pattern>*.tile</url-pattern>     </servlet-mapping>     <servlet-mapping>         <servlet-name>action</servlet-name>         <url-pattern>*.do</url-pattern>     </servlet-mapping> </web-app>
image from book

The web.xml file still holds references to the ch19, which is a Spring DispatcherServlet, but it adds a definition of the action servlet, which is a Struts ActionServlet that is mapped to Struts-recommended *.do requests.

Next, we create the struts-config.xml file, which is a standard Struts configuration file. We start off with just a single action with one action forward definition, as shown in Listing 19-2.

Listing 19-2: The struts-config.xml File

image from book
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE struts-config PUBLIC     "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"     "">      <struts-config>              <action-mappings>         <action path="/index"              type="com.apress.prospring.ch19.web.actions.IndexAction"              validate="false">             <forward name="success" path="/WEB-INF/views/default.jsp"/>         </action>     </action-mappings>      </struts-config>
image from book

Here, the /index action is implemented in the IndexAction, which is a subclass of the Action class. It does not require any form beans, and it has one forward declaration to the index.jsp page, which is stored in /WEB-INF/views/default.jsp. The implementation of the IndexAction is shown in Listing 19-3.

Listing 19-3: IndexAction Implementation

image from book
package com.apress.prospring.ch19.web.actions;      import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;      import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping;      public class IndexAction extends Action {          public ActionForward execute(ActionMapping mapping, ActionForm form,          HttpServletRequest request, HttpServletResponse response)          throws Exception {         return mapping.findForward("success");     } } 
image from book

The class overrides the execute() method to process the request and return an ActionForward object, which Struts looks up based on its name passed to the findForward() method. The JSP page is just a simple "Hello, World" style page because there is no model to be displayed.

When we build and deploy the application and make a request to / URL, we see the output generated by the index.jsp page.

Pro Spring
Pro Spring
ISBN: 1590594614
EAN: 2147483647
Year: 2006
Pages: 189

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: