struts2整合spring环境配置
上篇文章详细记录了struts2环境配置的步骤,此篇文章主要记录strtus2整合spring环境配置
- 添加包
- web.xml中相关配置
- 创建spring配置文件
- 编写测试类
1.添加包(并build path)
包链接:
2.在web.xml中设置监听器,并指明随后创建的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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
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">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>jsp/index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置 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>
<!-- 指明spring配置文件在何处 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!--设置监听器 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
3.spring的配置文件(applicationContext.xml在WEB-INF目录下)
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="testAction" class="com.adu.action.TestAction" >
<property name="testService" ref="testService"></property>
</bean>
<bean id="testService" class="com.adu.service.TestService"/>
</beans>
在applicationContext.xml注册bean
4.service类
package com.adu.service;
public class TestService {
public void service() throws Exception {
System.out.println("this is service function ");
}
}
并在aciton中调用
package com.adu.action;
import com.adu.service.TestService;
import com.opensymphony.xwork2.Action;
public class TestAction implements Action {
private TestService testService;
public TestService getTestService() {
return testService;
}
public void setTestService(TestService testService) {
this.testService = testService;
}
public String execute() throws Exception {
System.out.println("execute Struts2 Action ...... ");
testService.service();
return "ok";
}
}
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>
<package name="action" namespace="/" extends="struts-default">
<action name="HWAction" class="com.adu.action.HelloWorld">
<result name="success">jsp/first.jsp</result>
</action>
<action name="TestAction" class="testAction">
<result name="ok">jsp/first.jsp</result>
</action>
</package>
</struts>
浙公网安备 33010602011771号