Hi All,
In this post I am going to brief about Struts 2.0 framework and how to configure it with the tomcat server.
Struts 2.0 is a simple MVC framework. It helps us in converting simple java class (POJO) into an action class that serves the requests. It helps us map the request parameters with the action class variables and also helps in validation of the input parameters without much coding. It provides the developer with lots of tag to handle the display of data in the web page with minimal coding. Struts 2.0 also supports ajax theme and this makes the page more dynamic with least amount of effort from the developer.
Now let us see how to configure Struts 2.0 with tomcat container. The download links are below,
Tomcat -
http://tomcat.apache.org/download-55.cgiStruts -
http://struts.apache.org/download.cgi#struts216The steps to be followed for configuring Structs are below,
1. Configure Tomcat's web.xml file to include Struts 2.0 filter.
2. Write a simple jsp page.
3. Write your simple action class.
4. Modify struts.xml (the controller) to define the action, its mapped class and the resultant Jsp (view).
web.xml Configuration :We need to add the struts filter in web.xml so that all the requests coming to tomcat with ".action" should now be routed to struts framework. All other requests which contain .html, .gif etc will be taken care by server directly.
web.xml |
<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> |
JSP Page :Before we create a jsp, we need to include the uri for struts tags. In the example below, we have used "<s:property value="message" />" to get the value that is set in the variable message in action class that is mapped to this jsp. Struts just invokes the getter of the variable to retrieve the value from the action class.
MyFirstView.jsp |
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title> My First Struts Page </title> </head> <body> <s:property value="message" /> </body> </html> |
Action Class :Any java class can act as the request handler in struts, provided it has a public method that takes no parameters and has a String return type. The string that is returned by the method in action class will be used by struts to map to a particular jsp (as defined in struts.xml). If no method is specified in the action mapping, struts will transfer the control to "execute" method of the action class.
MyActionClass.java |
package tutorial; import com.opensymphony.xwork2.ActionSupport; public class MyActionClass extends ActionSupport { public static final String MESSAGE = "Struts"; private String message;
public String execute() throws Exception { setMessage(MESSAGE); return SUCCESS; }
public void setMessage(String message) { this.message = message; }
public String getMessage() { return message; } } |
struts.xml Configuration :struts.xml |
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="tutorial" extends="struts-default"> <action name="HelloWorld" class="tutorial.MyActionClass"> <result>/MyFirstView.jsp</result> </action> <!-- Add your actions here --> </package> </struts> |
If you have any queries/comments, please post here and I will try to reply within a day.