一、ssd整合(spring+struts+jdbc)
步骤:
- 导入入struts+spring整合需要的jar文件(struts2-spring-plugin-2.3.1.2)
- 配置web.xml
(1)
(2)
<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文件
源码:
<?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">
<!-- 配置struts -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
<!-- struts.xml如在src根目录下不用配置,不在则做如下配置 -->
<init-param>
<!-- config名不能改 -->
<param-name>config</param-name>
<param-value>struts-default.xml,struts-plugin.xml,config/struts.xml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置spring ,整合struts时如果applicationContext.xml在web-info下则不用配置-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--如果applicationContext.xml不在web-info下则要配置 -->
<context-param>
<!-- contextConfigLocation名不能改 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/applicationContext.xml</param-value>
</context-param>
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
- 配置struts文件
源码:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="struts" extends="struts-default">
<!-- 前面是action类名(com.hw.controller.UserAction) ,后面是方法名
,如果和spring整合class里也可以用控制层注解 -->
<action name="user_*" class="userController" method="{1}">
<result name="add">/WEB-INF/user/add.jsp</result>
<result name="update">/WEB-INF/user/update.jsp</result>
<!-- struts中默认为转发即 type="dispatcher",redirect为重定向-->
<result name="success" type="redirect">user_list</result>
<result name="list">/WEB-INF/user/list.jsp</result>
</action>
</package>
</struts>