July 16, 2011

Struts 2 - Spring framework Integration - Hello World Example

0) Ensure that both Struts 2 and Spring are setup in the web application. For Struts 2-enable the web application, refer this link and to Spring-enable the web application refer this link.

1) Download the Struts-Spring plugin jar from the link here and copy it in the WebContent/WEB-INF/lib folder of the web application

2) Modify the Struts 2 filter configuration in the web.xml file as follows,

<filter>
   <filter-name>struts2</filter-name>
   <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

3) Copy the following configuration for the action element in the struts.xml file. The class attribute of the action element is assigned to the bean id from the applicationContext.xml rather than the actual class name

<action name="hello" class="greetingService">
   <result>/successSpring.jsp</result>
</action>

4) Modify the GreetingServiceImpl.java class to enter the following execute() and getGreeting() methods,

public String execute() { 
   return "success";
}

public String getGreeting() {
   return this.greeting;
}

5) Create a JSP - successSpring.jsp - in the WebContent folder of the web application

<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<body>   <s:property value="greeting" />
</body>
</html>

6) Test the example with the following URL in the browser,

http://localhost:8080/WebAppName/hello.action

No comments:

Post a Comment