一。加入struts2
1.在pom.xml中加入struts2的依赖
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.13</version>
</dependency>
2.在web.xml中加入过滤器
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
二。加入spring框架,整合spring+struts2
1.加入spring的依赖,和struts与spring整合的依赖
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.5.13</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.9.RELEASE</version>
</dependency>
<!-- hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.5.Final</version>
</dependency>
<!-- 数据库连接池jar -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>
2.在web.xml中加入spring监听,以及告诉监听spring的配置文件
<!-- 创建Spring的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring的监听器可以通过这个上下文参数来获取application.xml文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:application.xml</param-value>
</context-param>
<!-- Spring中提供了 org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
这个类来完成OpenSessionInViewer的操作 -->
<!-- 这个过滤器是配合hibernate中级联操作的延迟加载来使用的,一旦用了这个过滤器,就必须使用spring的事务管理 -->
<filter>
<filter-name>openSessionInViewerFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInViewerFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3.struts.xml文件修改
<struts>
<package>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<!-- //2.5的版本后动态访问url必须打开 -->
<constant name="struts.objectFactory" value="spring" />
<!-- //此处说明action由spring管理 -->
<package name="my" namespace="/" extends="struts-default">
<global-allowed-methods>regex:.*</global-allowed-methods>
<!-- //2.5版本后访问非execute方法要加入该配置 -->
<action name="*_*" class="{1}Action" method="{2}">
<result name="success">${successURL}</result>
<result name="input">${inputURL}</result>
</action>
</package>
</struts>
4.appliaction.xml也就是spring核心配置文件
5.各层用注解完成依赖注入
@Repository:DAO层的注解申明
@Service:业务层注解
@Controller:控制层注解 @Controller("userAction")此处的名字要和struts.xml中Action的class属性一致
实例:整合ssh,并完成前台发送请求后台,后台返回数据到前台的过程
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>SSHjava</groupId> <artifactId>SSHjava</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <build> <sourceDirectory>src</sourceDirectory> <resources> <resource> <directory>src</directory> <excludes> <exclude>**/*.java</exclude> </excludes> </resource> </resources> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.0.0</version> <configuration> <warSourceDirectory>WebContent</warSourceDirectory> </configuration> </plugin> </plugins> </build> <dependencies> <!-- struts2 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.5.13</version> </dependency> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.1.9.RELEASE</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>2.5.13</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>4.1.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.1.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.1.9.RELEASE</version> </dependency> <!-- hibernate --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.3.5.Final</version> </dependency> <!-- 数据库连接池jar --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-dbcp2</artifactId> <version>2.1.1</version> </dependency> <!-- jstl --> <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> - </dependencies> </project>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>SSHjava06</display-name> <!-- 创建Spring的监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Spring的监听器可以通过这个上下文参数来获取application.xml文件的位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:application.xml</param-value> </context-param> <!-- Spring中提供了 org.springframework.orm.hibernate4.support.OpenSessionInViewFilter 这个类来完成OpenSessionInViewer的操作 --> <!-- 这个过滤器是配合hibernate中级联操作的延迟加载来使用的,一旦用了这个过滤器,就必须使用spring的事务管理 --> <filter> <filter-name>openSessionInViewerFilter</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>openSessionInViewerFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- struts2的过滤器 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.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.htm</welcome-file> <welcome-file>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> </web-app>
jdbc.properties
jdbc.driverClassName = oracle.jdbc.driver.OracleDriver
jdbc.url = jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username = java
jdbc.password = java123
application.xml
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 打开Spring的Annotation的支持 --> <context:annotation-config /> <!-- 设定Spring去哪些包中查找Annotation --> <context:component-scan base-package="com" /> <!-- 告诉spring${}取值的资源文件路径 --> <context:property-placeholder location="classpath:jdbc.properties" /> <!-- spring管理数据库链接 --> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 配置连接池的初始值 --> <property name="initialSize" value="1" /> <!-- 连接池的最大值 --> <!-- <property name="maxActive" value="500"/> --> <!-- 最大空闲时,当经过一个高峰之后,连接池可以将一些用不到的连接释放,一直减少到maxIdle为止 --> <!-- <property name="maxIdle" value="2"/> --> <!-- 当最小空闲时,当连接少于minIdle时会自动去申请一些连接 --> <property name="minIdle" value="1" /> <property name="maxTotal" value="100" /> <property name="maxIdle" value="20" /> <property name="maxWaitMillis" value="1000" /> </bean> <!-- 创建Spring的SessionFactory工厂 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" > <!-- 注入数据源 --> <property name="dataSource" ref="dataSource" /> <!-- 设置Spring去哪个包中查找相应的实体类 --> <property name="packagesToScan"> <value>com.pojo</value> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <!-- <prop key="hibernate.hbm2ddl.auto">update</prop>自动根据实体类生成表结构 --> </props> </property> </bean> <!-- 配置Spring的事务处理 声明式事务--> <!-- 创建事务管理器 --> <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 打开事务注解开关 可以在代码中直接声明事务 必须放在AOP的配置前面才有效果--> <tx:annotation-driven transaction-manager="txManager"/> <!-- 配置AOP,Spring是通过AOP来进行事务管理的 --> <aop:config> <!-- 设置PointCut表示那些方法需要加入到事务管理 --> <aop:pointcut id="allMethods" expression="execution(* com.service.*.*(..))"/> <!-- 通过advisor来确定具体要加入事务控制的方法 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="allMethods"/> </aop:config> <!-- 配置哪些方法需要加入事务控制 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <!-- 让所有方法都加入事务管理,为了提高效率,可以把一些查询之类的方法设置为只读的事务 --> <tx:method name="*" propagation="REQUIRED" read-only="true"/> <!-- 以下方法都是可能涉及修改的方法,就无法设置为只读 --> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="del*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="modify*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> </beans>
struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <!-- //2.5的版本后动态访问url必须打开 --> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <!-- //此处说明action由spring管理 --> <constant name="struts.objectFactory" value="spring" /> <package name="my" namespace="/" extends="struts-default"> <!-- //2.5版本后访问非execute方法要加入该配置 --> <global-allowed-methods>regex:.*</global-allowed-methods> <action name="*_*" class="{1}Action" method="{2}"> <result name="success">${successURL}</result> <result name="input">${inputURL}</result> </action> </package> </struts>
student.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html> <html> <head> <base href="<%=basePath%>"/> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <a href="stu_getall">查询</a> <table> <c:forEach items="${list }" var="stu"> <tr> <td>${stu.studentName }</td> <td>${stu.classinfo.className}</td> </tr> </c:forEach> </table> </body> </html>
StudentAction.java
package com.control; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor; import com.pojo.Student; import com.service.IStudentService; @Controller("stuAction") public class StudentAction { @Autowired IStudentService stuSer ; String successURL;//跳转到成功结果的url路径 public String getSuccessURL() { return successURL; } public void setSuccessURL(String successURL) { this.successURL = successURL; } public void setStuSer(IStudentService stuSer) { this.stuSer = stuSer; } public String getall(){ List<Student> list = stuSer.getAllStu();
Map req = (Map) ActionContext.getContext().get("request"); req.put("list", list);
successURL="/jsp/student.jsp";
return "success"; } }
其他层代码省略,另外BaseDao中方法替换
@Override public Session getsSession(){ //如果使用spring来管理事务,一定要用getCurrentSession()来获取session return sessionFactory.getCurrentSession(); }