spring整合struts2

来源:互联网 发布:淘宝能开刻章店吗 编辑:程序博客网 时间:2024/06/02 05:55

 

一、jar包:

 

二、web.xml配置

 

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" 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_2_5.xsd">   <!-- struts2 filter  --><filter><filter-name>ss</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>ss</filter-name><url-pattern>*</url-pattern></filter-mapping>    <!-- spring ioc启动 start -->  <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>   <!-- spring ioc启动 end -->     <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>


三、applicationContext配置

<?xml version="1.0" encoding="utf-8" ?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><bean id="person" class="com.spring.web.bean.Person"><property name="name" value="孔圣人"></property></bean><bean id="personService" class="com.spring.web.service.PersonService"></bean><bean id="personAction" class="com.spring.web.action.PersonAction"><property name="personService" ref="personService"></property></bean></beans>


四、struts.xml配置

 

<!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts><package name="test" extends="struts-default"><action name="addPer" class="personAction"><result name="succ">/success.jsp</result></action></package></struts>

五、action

public class PersonAction {private PersonService personService;public void setPersonService(PersonService personService) {this.personService = personService;}public String execute() { System.out.println("execute ...");personService.addPerson(new Person());return "succ";}}


六、service

public class PersonService {public void addPerson(Person p){System.out.println("this is personService of addPerson()...");}}


 

七、jsp页面

 

<%@ page language="java" contentType="text/html; charset="UTF-8"   pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><a href="addPer.action">添加人员</a></body></html>

 


点击添加人员就会调用到execute()方法。跳转到success页面,说明整合成功
总结:

1. Spring 如何在 WEB 应用中使用 ?1). 需要额外加入的 jar 包:spring-web-4.0.0.RELEASE.jarspring-webmvc-4.0.0.RELEASE.jar2). Spring 的配置文件, 没有什么不同3). 如何创建 IOC 容器 ? ①. 非 WEB 应用在 main 方法中直接创建②. 应该在 WEB 应用被服务器加载时就创建 IOC 容器: 在 ServletContextListener#contextInitialized(ServletContextEvent sce) 方法中创建 IOC 容器.③. 在 WEB 应用的其他组件中如何来访问 IOC 容器呢 ?在 ServletContextListener#contextInitialized(ServletContextEvent sce) 方法中创建 IOC 容器后, 可以把其放在ServletContext(即 application 域)的一个属性中. ④. 实际上, Spring 配置文件的名字和位置应该也是可配置的! 将其配置到当前 WEB 应用的初始化参数中较为合适. <!-- 配置 Spring 配置文件的名称和位置 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param>需要在 web.xml 文件中加入如下配置:<!-- 启动 IOC 容器的 ServletContextListener --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>2. Spring 如何整合 Struts2 ?1). 整合目标 ? 使 IOC 容器来管理 Struts2 的 Action!2). 如何进行整合 ? ①. 正常加入 Struts2②. 在 Spring 的 IOC 容器中配置 Struts2 的 Action注意: 在 IOC 容器中配置 Struts2 的 Action 时, 需要配置 scope 属性, 其值必须为 prototype<bean id="personAction" class="com.atguigu.spring.struts2.actions.PersonAction"scope="prototype"><property name="personService" ref="personService"></property></bean>③. 配置 Struts2 的配置文件: action 节点的 class 属性需要指向 IOC 容器中该 bean 的 id<action name="person-save" class="personAction"><result>/success.jsp</result></action> ④. 加入 struts2-spring-plugin-2.3.15.3.jar3). 整合原理: 通过添加 struts2-spring-plugin-2.3.15.3.jar 以后, Struts2 会先从 IOC 容器中获取 Action 的实例.if (appContext.containsBean(beanName)) {    o = appContext.getBean(beanName);} else {    Class beanClazz = getClassInstance(beanName);    o = buildBean(beanClazz, extraContext);}


 

 

 

0 0