The configuration of Struts 2 is different from the way Struts 1 configuration. This blog demonstrates the steps required to configure Struts 2 on Tomcat.
These are the steps:
(1) Install Sun JDK 1.6
(2) Install Apache Tomcat 6.0.32
(3) Download struts-2.2.1.1-apps.zip from struts web site.
(4) This contains struts2-blank.war file. This is an empty war file containing struts2 configuration. We will use this to configure the struts2 web application.
(5) Copy struts2-blank.war file to the webapps folder of the Tomcat server.
(6) Start the Tomcat server.
(8) You should see the following screenshot of the application, which means that the application is working fine.
Now, let’s look at various configuration elements that goes with configuring struts2 application.
Web.xml
The web.xml file is used to configure the FilterDispatcher class that handles all requests to struts2 framework. FilterDispatcher class is configured as a filter and maps all the application requests to Struts2 framework.
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_9" version="2.4" 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"> <display-name>Struts Blank</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app> |
Struts.xml
The mappings for MVC are declared in the struts.xml file. This file is located in the folder – WEB-INF/src/java
<action name="HelloWorld" class="example.HelloWorld"> <result>/example/HelloWorld.jsp</result> </action> <package name="default" namespace="/" extends="struts-default"> <default-action-ref name="index" /> <action name="index"> <result type="redirectAction"> <param name="actionName">HelloWorld</param> <param name="namespace">/example</param> </result> </action> </package> |
HelloWorld.jsp
The following code snippet shows the use of struts tag library to create the view.
<%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title><s:text name="HelloWorld.message"/></title> </head> <body> <h2><s:property value="message"/></h2> <h3>Languages</h3> <ul> <li> <s:url id="url" action="HelloWorld"> <s:param name="request_locale">en</s:param> </s:url> <s:a href="%{url}">English</s:a> </li> <li> <s:url id="url" action="HelloWorld"> <s:param name="request_locale">es</s:param> </s:url> <s:a href="%{url}">Espanol</s:a> </li> </ul> </body> </html> |
HelloWorld.java
The execute method is called to process the request. This method generally call the business POJO classes and fetches / updates data to database. Once done, this call the view to be rendered based on the return string from the execute method.
public class HelloWorld extends ExampleSupport { public String execute() throws Exception { setMessage(getText(MESSAGE)); return SUCCESS; } /** * Provide default valuie for Message property. */ public static final String MESSAGE = "HelloWorld.message"; /** * Field for Message property. */ private String message; /** * Return Message property. * * @return Message property */ public String getMessage() { return message; } /** * Set Message property. * * @param message Text to display on HelloWorld page. */ public void setMessage(String message) { this.message = message; } } |
The view to be rendered is again the same page – HelloWorld.jsp, the same page is called with different message. The following screenshot shows the HelloWorld example.
The above description demonstrates how easy it is to setup Struts 2 MVC web framework. It also demonstrates that it is quite different from configuring Struts 1 framework.
This comment has been removed by a blog administrator.
ReplyDelete