spring整合struts2
----------------------------------------------xml方式-------------------------------------------------------------
- 有spring整合hibernate的beans.xml文件(在src下)
- struts.xml(src下)
<?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> <!-- Action对象交给spring容器创建 --> <constant name="struts.objectFactory" value="spring"/> <package name="aa" namespace="/test" extends="struts-default"> <action name="emp_*" class="empAction" method="{1}"> <result name="list">/emplist.jsp</result> <result name="addUI">/addUI.jsp</result> <result type="redirectAction" name="success">emp_list</result> </action> </package> </struts>
- WEB-INF下的web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>SSH</display-name> <!-- 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> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- spring的配置 --> <!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:beans.xml</param-value> </context-param> <!-- 对Spring容器进行实例化 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
- **Action类
package cn.wyzc.action; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import com.opensymphony.xwork2.ActionContext; import cn.wyzc.entity.Emp; import cn.wyzc.service.EmpService; @Controller //控制类自动生成对象 public class EmpAction { private int id; private String name; private String pass; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } @Resource private EmpService empService; public void setEmpService(EmpService empService) { this.empService = empService; } public String list(){ List<Emp> emps = empService.findAll(); ActionContext.getContext().put("emps", emps); return "list"; } public String addUI(){ return "addUI"; } public String add(){ Emp emp = new Emp(); emp.setId(id); emp.setName(name); emp.setPass(pass); empService.addEmp(emp); return "success"; } }
- jar包:spring的jar包 + struts2最少依赖jar包 + struts2-spring-plugin-2.1.8.jar + JSTL的jar包
注:
- <a href="/SSH/test/emp_list.action">查看</a> <!--/项目名/空间名/actionName_方法名.action-->
- 引用JSTL标签需要导入 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
-
${} 和 foreach应用
-
<c:forEach var="emp" items="${emps }"> <tr> <td>${emp.id }</td> <td>${emp.name }</td> <td>${emp.pass }</td> </tr> </c:forEach>
public String list(){ List<Emp> emps = empService.findAll(); ActionContext.getContext().put("emps", emps); return "list"; }
- 表单中输入的值直接在类action中可以获取
<form action="/SSH/test/emp_add.action"> ID:<input type="text" name="id"><br> Name:<input type="text" name="name"><br> Pass:<input type="text" name="pass"><br> <input type="submit" value="提交"><br> </form>
@Controller public class EmpAction { private int id; private String name; private String pass; //get&set省略 @Resource private EmpService empService; //set省略 public String add(){ Emp emp = new Emp(); emp.setId(id); emp.setName(name); emp.setPass(pass); empService.addEmp(emp); return "success"; } }
----------------------------------------------注解方式-------------------------------------------------------------
spring的xml中: xml配置方式基础上新增2个, 去掉所有类的bean配置,通过注解完成,util保留
<context:annotation-config></context:annotation-config> <context:component-scan base-package="org.ssh_manage"></context:component-scan>
<util:map id="map" map-class="java.util.HashMap">
<entry key="teacherDao" value-ref="teacherDao"></entry>
<!-- 可以有n多个dao的bean -->
</util:map>
action类中:添加一个全局注解,每个方法添加一个@Action注解
@Controller @ParentPackage(value="struts-default") @Namespace(value="/app1") @Results(value={ @Result(name="welcome",location="/WEB-INF/welcome.jsp"), @Result(name="index",type="redirect",location="/index.jsp",params={"msgState","${msgState}"}) }) public class App1Action extends BasicAction {
@Resource(name="service")
private Service service;
get/set(...)
..... @Action(value="app1Action_fn1") public String fn1() {...} }
struts.xml中为空:
<struts> </struts>
service类
@org.springframework.stereotype.Service public class Service { @Resource(name="map") private Map<String, Dao> daoMap;
dao类
@Repository(value="teacherDao") public class TeacherDao implements Dao{

浙公网安备 33010602011771号