Struts 2 provides plug-ins for integration to any other frameworks. If you are using Spring for your bean management, then it is very easy for you to integrate Spring container to Struts 2 application. Spring can manage the beans and struts actions classes as beans. You have to just specify the bean name in the struts configuration file. The following steps to be taken for the Struts 2 and Spring integration.
- Download the Struts2-Spring-plugin.jar. It is the library which integrates both the frameworks.
- Add Spring libraries into the lib folder
- Add context loader in the web.xml for loading the spring configuration file.
The above steps are the additional tasks which you have to do with the Struts 2 application for enabling the integration. This tutorial shows a very simple example for the integration. Lets look at the example code.
1. Action Class and Bean
Write a action class and declare a bean which will be injected from the spring container with default values.
package javabeat.net.struts2; public class Struts2SpringIntegrationAction { //Spring Injection private UserDetails userDetails; public UserDetails getUserDetails() { return userDetails; } public void setUserDetails(UserDetails userDetails) { this.userDetails = userDetails; } public String execute(){ return "success"; } }
UserDetails.java
package javabeat.net.struts2; public class UserDetails { private String userName; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } }
2. Spring Configuration File
It is the traditional spring configuration file for declaring the spring beans. Note that here I have declared Struts action class as the spring beans and injected UserDetails beans to the action class.
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="userDetails" class="javabeat.net.struts2.UserDetails"> <property name="userName" value="Rahul Dravid"/> </bean> <bean id="struts2Spring" class="javabeat.net.struts2.Struts2SpringIntegrationAction"> <property name="userDetails" ref="userDetails"/> </bean> </beans>
3. Struts 2 Configuration File
Struts 2 configuration file would use the spring’s bean name in the class attribute of the action mapping.
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="uitagsdemo" extends="struts-default"> <action name="Struts2Spring" class="struts2Spring" > <result name="success">Result.jsp</result> </action> </package> </struts>
4. JSP File
Write a simple JSP to print the values.
Result.jsp
<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <body> <h2>JavaBeat - Struts 2 Spring Integration Demo</h2> <h4> <s:push value="userDetails"> User Name : <s:property value="userName" /><br> </s:push> </h4> </body> </html>
5. Web.xml
There are two entries in the web.xml
- Struts 2 filter and
- Spring configuration loader
<?xml version="1.0" encoding="UTF-8"?> <web-app> <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> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> </web-app>
6. Struts 2 Spring Integration Demo
If you access the application using URL http://localhost:8080/Struts2App/Struts2Spring, you would see the following output.