标签:www cti eth schema javaee xmlns 12px 配置文件 space
1:用spring自己的监听器
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <!-- 配置 Spring 配置文件的名称和位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 启动 IOC 容器的 ServletContextListener --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置 Struts2 的 Filter --> <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>
2:加入jar包。获取spring配置文件的名称和位置用:classpath:就行了。spring自动帮我们去加载了。并放到servletContext中了。
3:写类
package com.struts.spring.beans; public class Person { private String username; public void setUsername(String username) { this.username = username; } public void hello(){ System.out.println("My name is " + username); } }
package com.struts.spring.services; public class PersonService { public void save(){ System.out.println("测试service层"); } }
package com.struts.spring.actions; import com.struts.spring.services.PersonService; public class PersonAction { private PersonService personService; public void setPersonService(PersonService personService) { this.personService = personService; } public String execute(){ System.out.println("action层"); personService.save(); return "success"; } }
4:配置applicationContext.xml
<bean id="person" class="com.struts.spring.beans.Person"> <property name="username" value="陆伟"></property> </bean> <bean id="personService" class="com.struts.spring.services.PersonService"/> <bean id="personAction" class="com.struts.spring.actions.PersonAction" scope="prototype"> <property name="personService" ref="personService"></property> </bean>
知识点:注意: 在 IOC 容器中配置 Struts2 的 Action 时, 需要配置 scope 属性, 其值必须为 prototype。会每次都重新生成一个新的对象给请求方。
5:写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.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <!-- Spring 整合 Struts2 时, 在 Struts2 中配置的 Spring 的 Action 的 class 需要指向 IOC 容器中该 bean 的 id --> <action name="person-save" class="personAction"> <result>/success.jsp</result> </action> </package> </struts>
标签:www cti eth schema javaee xmlns 12px 配置文件 space
原文地址:http://www.cnblogs.com/bulrush/p/8001413.html