- 本项目使用struts2为2.3 .2,spring为5.0.8
- 导入基本的struts2和spring的基本jar包,该包的作用https://blog.youkuaiyun.com/mjl960108/article/details/53541707?utm_source=blogxgwz6
- 引入struts2-spring-plugin-x.x.x.jar,这个包在Struts2的lib中
-
创建User类
package com.zg.sxsh.spring; public class User { private String name; private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
-
创建AddUser类
package com.zg.sxsh.spring; import com.opensymphony.xwork2.ActionSupport; public class AddUser extends ActionSupport { private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } @Override public String execute() throws Exception { System.out.println(user.getName()); System.out.println(user.getPassword()); return SUCCESS; } }
-
配置web.xml文件,struts2与spring的整合要添加 <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener><?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_4_0.xsd" version="3.0"> <display-name>SXSH</display-name> <welcome-file-list> <welcome-file>index.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.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
- 配置applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?> <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.xsd"> <bean name="addUser" class="com.zg.sxsh.spring.AddUser"> <property name="user" ref="user"></property> </bean> <bean name="user" class="com.zg.sxsh.spring.User"> <property name="name" value="lulu"></property> <property name="password" value="1234"></property> </bean> </beans>
-
添加addUser.jsp文件
<%-- Created by IntelliJ IDEA. User: 93281 Date: 2018/11/22 Time: 16:11 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>AddUser</title> </head> <body> <s:property value="user.name"></s:property> <s:property value="user.password"></s:property> </body> </html>
-
配置struts.xml文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.devMode" value="false"></constant> <package name="HelloWorld" extends="struts-default"> <action name="addUser" method="execute" class="addUser"> <result name="success">/pages/addUser.jsp</result> </action> </package> </struts>
- 结束