Spring and Struts 2 Integration

Spring structure gives a simple method to deal with the reliance. It very well may be effectively coordinated with struts2 2 framework.

The ContextLoaderListener class is utilized to impart spring application with struts2 2. It must be indicated in the web.xml file.


You need to follow following steps:

  1. Create struts2 application and include spring container files.
  2. In web.xml document, characterize ContextLoaderListener class.
  3. In struts.xml document, characterize bean name for the activity class.
  4. In applicationContext.xml document, make the bean. Its class name ought to be activity class name for example com.javacodegeeks.Login and id should coordinate with the activity class of struts.xml document (for example login).
  5. In the action class, characterize additional property for example message.

Example of Spring and Struts 2 Integration

You need to create following files for simple spring and struts 2 application:

  1. index.jsp
  2. web.xml
  3. struts.xml
  4. applicationContext.xml
  5. Login.java
  6. welcome.jsp
  7. error.jsp
1) index.jsp

This page gets the name from the user.

  1. <%@ taglib uri="/struts-tags" prefix="s"%>  
  2.   
  3. <s:form action="login">  
  4. <s:textfield name="userName" label="UserName"></s:textfield>  
  5. <s:submit></s:submit>  
  6. </s:form>  

2) web.xml

It defines controller class for struts 2 and ContextLoaderListener listener class to make connection between struts2 and spring application.

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.   <welcome-file-list>  
  8.     <welcome-file>index.jsp</welcome-file>  
  9.   </welcome-file-list>  
  10.   <filter>  
  11.     <filter-name>struts2</filter-name>  
  12.     <filter-class>  
  13.         org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
  14.     </filter-class>  
  15.   </filter>  
  16.     
  17. <listener>   
  18. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   
  19. </listener>   
  20.     
  21.   <filter-mapping>  
  22.     <filter-name>struts2</filter-name>  
  23.     <url-pattern>/*</url-pattern>  
  24.   </filter-mapping>  
  25.     
  26.   </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.

  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"   
  3. "http://struts.apache.org/dtds/struts-2.1.dtd">  
  4. <struts>  
  5. <package name="abc" extends="struts-default">  
  6. <action name="login" class="login">  
  7. <result name="success">welcome.jsp</result>  
  8. </action>  
  9.   
  10. </package>  
  11.   
  12. </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.

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:p="http://www.springframework.org/schema/p"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  7. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  8.   
  9. <bean id="login" class="mypack.Login">  
  10. <property name="message" value="Welcome Spring"></property>  
  11. </bean>  
  12.   
  13. </beans>  

5) Login.java

It defines two property userName and message with execute method where success is returned.

  1. package mypack;  
  2. public class Login {  
  3. private String userName,message;  
  4.   
  5. public String getMessage() {  
  6.     return message;  
  7. }  
  8. public void setMessage(String message) {  
  9.     this.message = message;  
  10. }  
  11. public String getUserName() {  
  12.     return userName;  
  13. }  
  14. public void setUserName(String userName) {  
  15.     this.userName = userName;  
  16. }  
  17. public String execute(){  
  18.     return "success";  
  19. }  
  20. }  

6) welcome.jsp

It prints values of userName and message properties.

  1. <%@ taglib uri="/struts-tags" prefix="s"%>  
  2.   
  3. Welcome, <s:property value="userName"/><br/>  
  4. ${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.

  1. Sorry!   





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition