Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Spring and Struts 2


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:

  1. Create struts2 application and add spring jar files.
  2. In web.xml file, define ContextLoaderListener class.
  3. In struts.xml file, define bean name for the action class.
  4. In applicationContext.xml file, create the bean. Its class name should be action class name e.g. com.Kreationnext.Login and id should match with the action class of struts.xml file (e.g. login).
  5. 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:

  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. <s:form action=“login”>
  3. <s:textfield name=“userName” label=“UserName”></s:textfield>
  4. <s:submit></s:submit>
  5. </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. <listener>
  17. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  18. </listener>
  19.   <filter-mapping>
  20.     <filter-name>struts2</filter-name>
  21.     <url-pattern>/*</url-pattern>
  22.   </filter-mapping>
  23.   </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. </package>
  10. </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. <bean id=“login” class=“mypack.Login”>
  9. <property name=“message” value=“Welcome Spring”></property>
  10. </bean>
  11. </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. public String getMessage() {
  5.     return message;
  6. }
  7. public void setMessage(String message) {
  8.     this.message = message;
  9. }
  10. public String getUserName() {
  11.     return userName;
  12. }
  13. public void setUserName(String userName) {
  14.     this.userName = userName;
  15. }
  16. public String execute(){
  17.     return “success”;
  18. }
  19. }

6) welcome.jsp

It prints values of userName and message properties.

  1. <%@ taglib uri=“/struts-tags” prefix=“s”%>
  2. Welcome, <s:property value=“userName”/><br/>
  3. ${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!
download this example (developed using Myeclipse IDE)

Output

struts2integration1

struts2integration2.jpg

Comments

comments