Spring与Struts2整合

Spring与Struts2集成:

 准备工作,集成Struts2:

 拷贝jar包

 导入\struts-2.3.24\apps\struts2-blank\WEB-INF\lib下的所有jar包

 导入\struts-2.3.24\lib下名为struts2-spring-plugin-2.3.24的jar包、

 导入开发web的Servlet-api.jar

 并作buildpath。

 注意:其中apps\struts2-blank\WEB-INF\lib下的javassist-3.11.0.GA.jar与Hibernate中的重复,所以去掉这个jar包。

 2.配置前端控制器(web.xml)

复制代码
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns="http://java.sun.com/xml/ns/javaee"
 3   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 5                       http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 6   version="3.0">
 7   
 8   <!-- 配置加载文件的路径,告诉监听器去哪里找Spring的配置文件 -->
 9   <context-param>
10       <param-name>contextConfigLocation</param-name>
11       <param-value>classpath:applicationContext.xml</param-value>
12   </context-param>
13   
14   <!-- 配置监听器,Tomcat启动,就开始启动并初始化Spring容器 -->
15   <listener>
16       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
17   </listener> 
18<!-- OSIV模式 -->
19<filter>20 <filter-name>OSIV</filter-name>21 <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>22 </filter>23 <filter-mapping>24 <filter-name>OSIV</filter-name>25 <url-pattern>/*</url-pattern>26 </filter-mapping>
2728 <!-- 前端控制器 -->29 <filter>30 <filter-name>struts2</filter-name>31 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 32 </filter>33 <filter-mapping>34 <filter-name>struts2</filter-name>35 <url-pattern>/*</url-pattern>36 </filter-mapping>37 </web-app>
复制代码

 3.编写Action和配置Action(Struts.xml文件)

复制代码
 1 package com.wanglei.ssh.web.action;
 2 
 3 import lombok.Setter;
 4 import com.opensymphony.xwork2.ActionContext;
 5 import com.opensymphony.xwork2.ActionSupport;
 6 import com.wanglei.ssh.service.IEmployeeService;
 7 
 8 public class EmployeeAction extends ActionSupport{
 9     private static final long serialVersionUID = 1L;
10     
11     @Setter
12     private IEmployeeService employeeService;
13     
14     public String execute() throws Exception {
15         ActionContext.getContext().put("employee",employeeService.listAll());
16         return "list";
17     }
18 
19 }
复制代码
复制代码
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 
 6 <struts>
 7 
 8     <constant name="struts.devMode" value="true" />
 9 
10     <package name="default" namespace="/" extends="struts-default">
11         <action name="employee" class="employeeAction">
12             <result name="list">/WEB-INF/views/employee/list.jsp</result>
13         </action>
14     </package>
15 </struts>
复制代码

4.在applicationContext.xml中增加配置Action(配好之后,在struts.xml文件中改为<action name="employee" class="employeeAction">)

复制代码
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 4     xmlns:p="http://www.springframework.org/schema/p"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xmlns:aop="http://www.springframework.org/schema/aop"
 7     xmlns:tx="http://www.springframework.org/schema/tx"
 8     xsi:schemaLocation="
 9         http://www.springframework.org/schema/beans
10         http://www.springframework.org/schema/beans/spring-beans.xsd
11         http://www.springframework.org/schema/aop
12         http://www.springframework.org/schema/aop/spring-aop.xsd
13         http://www.springframework.org/schema/tx
14         http://www.springframework.org/schema/tx/spring-tx.xsd
15         http://www.springframework.org/schema/context
16         http://www.springframework.org/schema/context/spring-context.xsd">
17         <!-- 属性占位符 -->
18         <context:property-placeholder location="classpath:db.properties"/>
19         <!-- 配置连接池 -->
20           <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
21         <property name="driverClassName" value="${jdbc.driverClassName}" />
22         <property name="url" value="${jdbc.url}" ></property>
23         <property name="username" value="${jdbc.username}" ></property>
24         <property name="password" value="${jdbc.password}" />
25     </bean>
26         <!-- 配置SessionFactory -->
27         <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
28             <!-- 管理连接池 -->
29             <property name="dataSource" ref="dataSource"></property>
30             <!-- hibernate属性配置 -->
31             <property name="hibernateProperties">
32                 <value>
33                     hibernate.dialect = org.hibernate.dialect.MySQLDialect
34                     hibernate.show_sql = true
35                     hibernate.hbm2ddl.auto = update
36                 </value>
37             </property>
38             <!-- 映射文件的位置 -->
39             <property name="mappingLocations" value="classpath:com/wanglei/ssh/domain/*.hbm.xml"></property>
40         </bean>
41         <!--what 配置事务管理器 -->
42         <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
43             <property name="sessionFactory" ref="sessionFactory"></property>
44         </bean>
45         <!--when 通用的crud增强  -->
46         <tx:advice id="cruidAdvice" transaction-manager="txManager">
47             <tx:attributes>
48                 <tx:method name="get*" read-only="true"/>
49                 <tx:method name="list*" read-only="true"/>
50                 <tx:method name="query*" read-only="true"/>
51                 <tx:method name="*" propagation="REQUIRED"/>
52             </tx:attributes>
53         </tx:advice>
54         <!--where 切入点语法  -->
55         <aop:config>
56             <aop:pointcut expression="execution(* com.wanglei.ssh.service.*Service.*(..))" id="crudPoint"/>
57             <aop:advisor advice-ref="cruidAdvice" pointcut-ref="crudPoint"/>
58         </aop:config>
59         
60         <!-- 配置DAO -->
61         <bean id="employeeDAO" class="com.wanglei.ssh.dao.impl.EmployeeDAOImpl">
62             <property name="sessionFactory" ref="sessionFactory"></property>
63         </bean>
64         <!-- 配置Service -->
65         <bean id="employeeService" class="com.wanglei.ssh.service.impl.EmployeeServiceImpl">
66             <property name="employeeDAO" ref="employeeDAO"></property>
67         </bean>
68         <!-- 配置Action -->
69         <bean id="employeeAction" class="com.wanglei.ssh.web.action.EmployeeAction" scope="prototype">
70             <property name="employeeService" ref="employeeService"></property>
71         </bean>
72 </beans>
73     
复制代码

5.新建list.jsp页面

复制代码
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ taglib prefix="s" uri="/struts-tags" %>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11      <s:debug></s:debug>
12 </body>
13 </html>
复制代码

6.新建一个domain类Department.java和Department.hbm.xml

domain.Department
复制代码
 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5 
 6 <!--
 7 
 8 -->
 9 
10 <hibernate-mapping package="com.wanglei.ssh.domain">
11     <class name="Employee" table="employee">
12         <id name="id">
13             <generator class="native"></generator>
14         </id>
15         <property name="name"></property>
16         <property name="age"></property>
17         <many-to-one name="dept" column="dept_name"></many-to-one>
18     </class>
19 </hibernate-mapping>
复制代码

7.在web.xml文件中加入如下代码(OSIV问题,目的是让session在请求的时候打开,在响应完毕后再关闭,不能在业务层内关闭。)

复制代码
复制代码
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns="http://java.sun.com/xml/ns/javaee"
 3   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 5                       http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 6   version="3.0">
 7   
 8   <!-- 配置加载文件的路径,告诉监听器去哪里找Spring的配置文件 -->
 9   <context-param>
10       <param-name>contextConfigLocation</param-name>
11       <param-value>classpath:applicationContext.xml</param-value>
12   </context-param>
13   
14   <!-- 配置监听器,Tomcat启动,就开始启动并初始化Spring容器 -->
15   <listener>
16       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
17   </listener> 
18<!-- OSIV模式 -->
19<filter>
20 <filter-name>OSIV</filter-name>
21 <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
22 </filter>
23 <filter-mapping>
24 <filter-name>OSIV</filter-name>
25 <url-pattern>/*</url-pattern>
26 </filter-mapping>
27
28 <!-- 前端控制器 -->
29
<filter>
30
<filter-name>struts2</filter-name>
31 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
32 </filter>
33 <filter-mapping>
34
<filter-name>struts2</filter-name>
35 <url-pattern>/*</url-pattern>
36
</filter-mapping>
37 </web-app>
复制代码

8.运行后数据库再创建除了Department的表,在该表内赋值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值