Thursday, November 02, 2006

The Struts 2.0 Controller

The major difference in architecture of Struts 2.0 is in the controller. Instead of the controller servlet of Struts 1.x (ActionServlet), Struts 2.0 introduces a Servlet Filter (FilterDispatcher). The filter has to be configured in the web.xml, as follows
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
You don't have to declare a tag library as it is included in struts-core.jar. Using filters in a web application has many advantages, such as:
  • You can cascade functionality in multiple Filters, without modifying the code in the rest of the app.
  • Filters can wrap the request and response they hand on to the rest of the app, adding additional functionality there.
  • Filters could be used to perform pre-processing on the request, and/or post-processing on the response, to perform data transformations required by the application.
  • Filters can be used to enforce "users do not have direct access to a JSP page"
  • Filters can let you use existing servlets be used as Actions, without giving up things like the form bean processing.
  • Filters can, in effect, "remap" request URIs by doint a RequestDispatcher.forward() to the new target.
  • Filters apply to requests for static content, as well as requests to the controller servlet and JSP pages, so you can do extra processing around them as well.
Using a servlet filter instead of a Servlet has a few interesting and useful consequences such as allowing better integration with other products, by adding additional filters to aid the FilterDispatcher. For example: The Controller can clean-up the Action Context to avoid any memory leaks. But sometimes, the controller may not integrate well with some products, in which case a new filter can be created and used in a chain to ensure proper integration. ActionContextCleanUp is a filter used to work with the FilterDispatcher to allow better integration with SiteMesh. SiteMesh is a page decorator tool (like tiles) from OpenSymphony. In order to user SiteMesh with Struts 2.0, you have to define an additional filter in your web.xml file. Thus, when using SiteMesh, your web.xml filters would look like this:
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>org.apache.struts2.sitemesh.VelocityPageFilter</filter-class>
</filter>
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>

<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

No comments:

Post a Comment

Popular Posts