一、新建web项目
结构
二、代码及配置
Action类:
package net.amiba.action;
import com.opensymphony.xwork2.ActionSupport;
import net.amiba.bean.Emp;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import java.util.ArrayList;
import java.util.List;
public class EmpAction extends ActionSupport {
private List<Emp> empList;
public List<Emp> getEmpList() {
return empList;
}
public void setEmpList(List<Emp> empList) {
this.empList = empList;
}
@Override
public String execute() throws Exception {
empList = new ArrayList<>();
empList.add(new Emp(9527,"zxc"));
empList.add(new Emp(9528,"zxx"));
empList.add(new Emp(9529,"amiba"));
return SUCCESS;
}
}
Emp类:
package net.amiba.bean;
public class Emp {
private Integer empno;
private String ename;
public Emp() {
}
public Emp(Integer empno, String ename) {
this.empno = empno;
this.ename = ename;
}
public Integer getEmpno() {
return empno;
}
public void setEmpno(Integer empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
@Override
public String toString() {
return "Emp{" +
"empno=" + empno +
", ename=" + ename +
'}';
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--装配spring-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--struts2过滤器-->
<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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--自动扫描注解类-->
<context:component-scan base-package="net.amiba"></context:component-scan>
</beans>
struts.xml:
<?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>
<!--默认值也是spring,struts的对象由spring创建-->
<constant name="struts.objectFactory" value="spring"/>
<package name="emp" namespace="/emp" extends="struts-default">
<action name="list" class="net.amiba.action.EmpAction" method="execute">
<result name="success">/emp/list.jsp</result>
</action>
</package>
</struts>
list.jsp:
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>员工信息</title>
</head>
<body>
员工信息<br/>
<s:iterator value="empList" var="emp" status="st">
<s:property value="#st.getIndex()+1"></s:property> <s:property value="#emp.empno"></s:property> <s:property value="#emp.ename"></s:property><br/>
</s:iterator>
<s:debug></s:debug>
</body>
</html>
三、启动项目并访问
http://localhost:8080/emp/list
四、总结
1.项目启动时,加载加载struts2过滤器及spring容器。
2.spring配置中采用自动扫描组件,EmpAction采用注解的形式@Controller会被spring容器扫描到。
3.struts2中配置采用spring创建对象。
五、jar包及GitHub仓库地址
其中个别jar包暂时用不到!
仓库地址:https://github.com/binjiewang/csdn_struts2_spring.git