You are on page 1of 4

Struts Overview In this tutorial you will create a simple struts action class that will forward to different

views depending on request parameters. Overview of the WebApp:

web.xml <servlet> <servlet-name>FrontController</servlet-name> <servlet-class> org.apache.struts.action.ActionServlet </servlet-class> <!-- Path of the struts configuration file --> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <!-- Load the servlet on startup --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>FrontController</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>

All requests to .do go to the FrontController which is an alias for the ActionServlet

ActionServlet
package cool; Class FirstAction extends Action{ public execute (){ struts-config.xml
<action-mappings> <!-- Declare the /firstaction.do action --> <action path="/firstaction" type="cool.FirstAction"> <forward name="success" path="/success.jsp"/> <forward name="errorpage" path="/error.jsp"/> </action> </action-mappings> <action-mappings> </action-mappings>

Configures

if(..){ findforward(error) } else { findforward(success) } } }


success.jsp

error.jsp

The web application you will create will have the following files: <TOMCAT_HOME>/webapps/firststruts/WEB-INF/classes/FirstAction.class /WEB-INF/lib/commons-beanutils.jar /WEB-INF/lib/commons-digester.jar /WEB-INF/lib/struts.jar /WEB-INF/web.xml /WEB-INF/struts-config.xml error.jsp success.jsp (The jar files can be copied from the lib directory of the struts distribution) Step 1: Create a subclass of Action, this class must have an execute method. The execute method will forward to different aliases based on the request parameters. FirstAction.java
package cool; import org.apache.struts.action.*; import javax.servlet.http.*; public class FirstAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { String name= request.getParameter("name"); if (name==null){ request.setAttribute("error","no name entered"); return mapping.findForward("errorpage"); } else{ return mapping.findForward("success"); } } }

Step 2: Create the Views. success.jsp


Yippee thanks for entering your name <%= request.getParameter("name") %>

error.jsp
The error is: <%= request.getAttribute("error") %>

Step 3: Create the web.xml Any requests to *.do will be directed to the FrontController which is an alias fot the Action Servlet. The struts-config.xml will configure the controller.
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <!-- Declare the Struts ActionServlet --> <servlet> <servlet-name>FrontController</servlet-name> <servlet-class> org.apache.struts.action.ActionServlet </servlet-class> <!-- Path of the struts configuration file --> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <!-- Load the servlet on startup --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>FrontController</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>

Step 4: Create the struts-config.xml <?xml version="1.0" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"> <struts-config> <action-mappings> <!-- Declare the /firstaction.do action --> <action path="/firstaction" type="cool.FirstAction"> <forward name="success" path="/success.jsp"/> <forward name="errorpage" path="/error.jsp"/> </action> </action-mappings> </struts-config>

5) Create the web app structure under tomcats web apps directory 6) Access the webapp with the following urls http://127.0.0.1:8080/firststruts/firstaction.do http://127.0.0.1:8080/firststruts/firstaction.do?name=zahir

You might also like