<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Struts2Spring3Intergration</display-name> <welcome-file-list> <welcome-file>User.jsp</welcome-file> </welcome-file-list> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>The important thing to note here is the listener that we have configured. The ContextLoaderListener is required to load the spring context file. Spring's configuration file is called applicationContext.xml file and it must be placed at the same level as the web.xml file.
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <context:component-scan base-package="com.dineshonjava.struts2.bean" /> <bean id="user" class="com.dineshonjava.struts2.action.user.UserAction"> </bean> <bean id="userBean" class="com.dineshonjava.struts2.bean.UserBean"> </bean> </beans>
package com.dineshonjava.struts2.action.user; import org.springframework.beans.factory.annotation.Autowired; import com.dineshonjava.struts2.bean.UserBean; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; /** * @author Dinesh Rajput * */ public class UserAction extends ActionSupport implements ModelDriven<UserBean>{ private static final long serialVersionUID = 1L; @Autowired private UserBean userBean; public String execute() { return SUCCESS; } public String addUser() { return SUCCESS; } @Override public UserBean getModel() { return userBean; } }Here userBean property autowired with action class UserAction java it is also powerfull feature of Spring framework.
package com.dineshonjava.struts2.bean; /** * @author Dinesh Rajput * */ public class UserBean { private String userName; private int userAge; private String userGender; private String userJob; private String []userHobbies; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public int getUserAge() { return userAge; } public void setUserAge(int userAge) { this.userAge = userAge; } public String getUserGender() { return userGender; } public void setUserGender(String userGender) { this.userGender = userGender; } public String getUserJob() { return userJob; } public void setUserJob(String userJob) { this.userJob = userJob; } public String[] getUserHobbies() { return userHobbies; } public void setUserHobbies(String[] userHobbies) { this.userHobbies = userHobbies; } }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <constant name="struts.custom.i18n.resources" value="myapp" /> <package name="user" extends="struts-default"> <action name="user" class="user" method="execute"> <result name="success">/success.jsp</result> <result name="input">/User.jsp</result> </action> </package> </struts>The important thing to note is that we are using the id user to refer to the class. This means that we are using spring to do the dependency injection for the User class.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <style type="text/css"> b{color: blue;} </style> <title>Struts2 - Spring integration | dineshonjava.com</title> </head> <body> <h1><span style="background-color: #FFFFcc">User From Struts2 - Spring integration</span></h1> <h2>Add User</h2><b> <s:form action="user" method="addUser"> <s:textfield name="userName" key="user.name" /> <s:textfield name="userAge" key="user.age" value=""/> <s:radio name="userGender" key="user.gender" list="{'Male','Female'}" /> <s:select name="userJob" key="user.job" list="%{#{'Software':'Software','Hardware':'Hardware','Networking':'Networking','Marketing':'Marketing'}}"/> <s:checkboxlist name="userHobbies" key="user.hobby" list="{'Cricket','Football','Drawing','Cooking','Driving','Movie'}" /> <s:submit key="submit" align="center"/> </s:form> </b> </body> </html>Creating success.jsp file for after submit.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Struts2 - Spring integration | dineshonjava.com</title> </head> <body> <h1><span style="background-color: #FFFFcc">User From Struts2 - Spring integration</span></h1> <h2>List of User</h2> <ul> <li>User Name : <s:property value="userName" /></li> <li>User Age : <s:property value="userAge" /></li> <li>User Gender : <s:property value="userGender" /></li> <li>User Jobs : <s:property value="userJob" /></li> <li>User Hobbies : <s:property value="userHobbies" /></li> </ul> </body> </html>myapp.properties
user.name=User Name user.age=User Age user.gender=Gender user.job=Job Type user.hobby=Hobbies submit=Add User
Labels: struts2