The Controller: ActionServlet and ActionMapping
Previous | Home | Next |
The controller's main job is to map requests to action i.e. the controller is the first component to act in the processing. So, controller function as a backbone for
a struts based application. Struts have Action class and ActionServlet class which is used as controller. Controller servlet is most core of a Struts based application.
The Controller handles all requests from the user and selects the view to return. When the Controller receives a request from user, Controller forwards the request to the
appropriate handler, which interprets what action to take based on that request.
Struts provides a ActionServlet class which is responsible for initializing a Struts applications configuration file and receiving all incoming request and on the behalf of request
ActionServletpopulating the Form Bean with data, validating the From bean and then selecting the appropriate Action class to execute.The Action class is where the Struts
Frameworks is ends and applications model(code) begins.After doing calculations and manipulations Action class returns a key on the behalf of this key ActionServlet class
will decides the next view.
The ActionServlet Class is controller class that receives all incoming requests for the application. ActionServlet is responsible for initializing the Struts framework for your application.
Like other servlet ActionServlet must be configured in web.xml (in Web application deployment descriptor).
There are two ways that ActionServlet can be configured to receive requests in web.xml.
- First, ActionServlet can be configured using path mapping, as shown here: Path mapping routes to ActionServlet all requests that match a specified path.
The default path is /do/*.action org.apache.struts.action.ActionServlet config /WEB-INF/struts-config.xml 1 action /do/*
- The second way to map requests to ActionServlet is to use extension mapping. Extension mapping maps to ActionServlet all requests with the specified extension.
The default extension to use is *.doaction org.apache.struts.action.ActionServlet config /WEB-INF/struts-config.xml 1 action *.do
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <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>3</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>3</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans> <form-bean name="helloForm" type="com.r4r.struts.HelloForm"/> </form-beans> <global-exceptions /> <global-forwards> <forward name="helloAction" path="/helloAction.do"/> </global-forwards> <action-mappings> <action path="/helloAction" type="com.r4r.struts.HelloAction" name="helloForm"> <forward name="success" path="/success.jsp"/> </action> </action-mappings> <message-resources parameter="com.r4r.struts.ApplicationResources" /> </struts-config>
Previous | Home | Next |