Spring and Struts 2 Integration
Spring framework provides an easy way to manage the dependency. It can be easily integrated with struts 2 framework.
The ContextLoaderListener class is used to communicate spring application with struts 2. It must be specified in the web.xml file.
You need to follow following steps:
- Create struts2 application and add spring jar files.
- In web.xml file, define ContextLoaderListener class.
- In struts.xml file, define bean name for the action class.
- In applicationContext.xml file, create the bean. Its class name should be action class name e.g. com.javatpoint.Login and id should match with the action class of struts.xml file (e.g. login).
- In the action class, define extra property e.g. message.
Example of Spring and Struts 2 Integration
You need to create following files for simple spring and struts 2 application:
- index.jsp
- web.xml
- struts.xml
- applicationContext.xml
- Login.java
- welcome.jsp
- error.jsp
1) index.jsp
This page gets the name from the user.
- <%@ taglib uri="/struts-tags" prefix="s"%>
-
- <s:form action="login">
- <s:textfield name="userName" label="UserName"></s:textfield>
- <s:submit></s:submit>
- </s:form>
2) web.xml
It defines controller class for struts 2 and ContextLoaderListener listener class to make connection between struts2 and spring application.
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http:
- http:
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>
- org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
- </filter-class>
- </filter>
-
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
-
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
-
- </web-app>
3) struts.xml
It defines the package with action and result. Here, the action class name is login which will be searched in the applicationContext.xml file.
- <?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>
- <package name="abc" extends="struts-default">
- <action name="login" class="login">
- <result name="success">welcome.jsp</result>
- </action>
-
- </package>
-
- </struts>
4) applicationContext.xml
It defines a bean with id login. This beans corresponds to the mypack.Login class. It will be considered as the action class here.
It should be located inside the WEB-INF directory.
- <?xml version="1.0" encoding="UTF-8"?>
- <beans
- xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http:
- http:
-
- <bean id="login" class="mypack.Login">
- <property name="message" value="Welcome Spring"></property>
- </bean>
-
- </beans>
5) Login.java
It defines two property userName and message with execute method where success is returned.
- package mypack;
- public class Login {
- private String userName,message;
-
- public String getMessage() {
- return message;
- }
- public void setMessage(String message) {
- this.message = message;
- }
- public String getUserName() {
- return userName;
- }
- public void setUserName(String userName) {
- this.userName = userName;
- }
- public String execute(){
- return "success";
- }
- }
6) welcome.jsp
It prints values of userName and message properties.
- <%@ taglib uri="/struts-tags" prefix="s"%>
-
- Welcome, <s:property value="userName"/><br/>
- ${message}
7) error.jsp
It is the error page. But it is not required in this example because we are not defining any logic in the execute method of action class.
Output
|