三:搭建Spring
1.添加Spring的Jar包
2.添加Struts2,Spring插件的Jar包
3.添加Oracle10g 数据库的Jar包
4.使用Spring来管理struts2的Action,DAO层,Service层
1)在原来web.xml基础之上添加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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>
<listener>
<!--监听器在default情况下读取的是:/WEB-INF/applicationContext.xml -->
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<!--
<param-value>
/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml
</param-value>
-->
<!-- 监听器启动后会读取classpath目录下面的beans.xml文件 -->
<param-value>classpath:beans.xml</param-value>
</context-param>
</web-app>
2)修改loginAction的内容
package com.wl.struts.action;
import com.opensymphony.xwork2.ActionSupport;
import com.wl.po.Student;
import com.wl.service.studentService;
@SuppressWarnings("serial")
public class loginAction extends ActionSupport {
private studentService stuService;
public studentService getStuService() {
return stuService;
}
public void setStuService(studentService stuService) {
this.stuService = stuService;
}
@Override
public String execute() throws Exception {
Student stu=new Student();
stu.setId("10");
stu.setStuName("Zhangsan");
stu.setStuAge(29);
stu.setStuGrade("A");
stu.setStuNo("201017");
stu.setStuSex("male");
stuService.saveStudent(stu);
return SUCCESS;
}
}
3)在Src目录下新建bean.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:contentx="http://www.springframework.org/schema/content"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/content
http://www.springframework.org/schema/content/spring-content-2.5.xsd">
<!-- configure the sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- manager the dao -->
<bean id="studentDao" class="com.wl.dao.impl.studentDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- manager the service -->
<bean id="studentService" class="com.wl.service.impl.studentServiceImpl">
<property name="stuDao" ref="studentDao"></property>
</bean>
<!-- manager the action in struts2 -->
<bean id="loginAction" class="com.wl.struts.action.loginAction" scope="prototype">
<property name="stuService">
<ref bean="studentService"/>
</property>
</bean>
</beans>
4)这时部署项目到Tomcat,运行Tomcat,结果如下
点击“test Action”操作,报500错
说明未将studentService注入到loginAction中。
A:这个问题的原因是:
loginAction没有真正的纳入Spring的管理当中
Spring的bean.xml配置文件的确对loginAction做了配置
<!-- manager the action in struts2 -->
<bean id="loginAction" class="com.wl.struts.action.loginAction" scope="prototype">
<property name="stuService">
<ref bean="studentService"/>
</property>
</bean>
但是Struts.xml配置文件中
<package name="login" namespace="/" extends="struts-default">
<action name="login" class="com.wl.struts.action.loginAction">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
没有用到Spring管理的loginAction,而是用类名重新引用的。loginAction
B:解决这个问题的方法:
修改Struts.xml的配置信息:
<package name="login" namespace="/" extends="struts-default">
<action name="login" class="loginAction">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
将原来的class="com.wl.struts.action.loginAction" 替换为
class="loginAction",其中的loginAction就是Spring中定义的id为loginAction的bean。
备注:
Spring对Struts的Action管理配置中不能忘记
scope="prototype"
默认情况下,Spring从bean工厂所取得的实例为Singleton(bean的singleton属性) Singleton: Spring容器只存在一个共享的bean实例,默认的配置。 Prototype: 每次对bean的请求都会创建一个新的bean实例;二者选择的原则:有状态的bean都使用Prototype作用域,而对无状态的bean则应该使用singleton作用域。
在ssh2 项目中,struts2的action交由spring管理的时候,spring默认是singleton的,而struts2的action显然是有状态的,所以必须显示设置为scope="prototype",prototype为原型模式,每次action请求过来都会创建一个action但是对那些Dao的实现类推介scope="singleton" ,因为这些类没有状态,用singleton只需维护一个实例,显然性能高一些。
5)重新部署项目,启动Tomcat后,点击“test Action”连接后控制条结果如下:
但是从数据库中查找数据,数据没有添加成功。
A: 问题原因:
缺少对事务的管理,数据未持久化到数据库中
B:解决的方法:添加对事务的管理,在bean.xml文件中添加配置信息
<!-- Transaction configuration Information -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
</tx:attributes>
</tx:advice>
<!-- 执行com.wl.service包及其子包下面的任何类的以Student结尾的任何方法 -->
<aop:config>
<aop:advisor pointcut="execution(* com.wl.service..*.*Student(..))" advice-ref="txAdvice" />
</aop:config>
现在重新启动Tomcat后,点击“test Action”连接后,就会将数据持久化到数据库中。

- 大小: 2.3 KB

- 大小: 1005 Bytes

- 大小: 1.2 KB

- 大小: 9.7 KB

- 大小: 6.8 KB

- 大小: 684 Bytes
分享到:
相关推荐
### Struts2+Hibernate3.2+Spring2.0架构搭建详解 #### 一、环境搭建与配置 在本教程中,我们将详细讲解如何搭建基于Struts2+Hibernate3.2+Spring2.0的软件架构。这个组合常被称为SSH(Struts-Spring-Hibernate)...
Java Web整合开发实战--基于Struts 2+Hibernate+Spring.pdf 1章 Web的工作机制 2章 搭建Java Web开发环境 3章 JSP及其相关技术 2篇 表现层框架Struts技术 4章 Struts快速上手 5章 解密Struts之核心文件 6章 ...
1.struts2+hibernate+spring框架搭建 jar包和数据库都在里面 2.做了个简单的添加数据到数据库的例子,直接解压然后导入,部署就可运行 3.MVC模式,对扩展和维护都有好处 4.扩展的时候直接写配置文件就可以了,很...
整合使用最新版本的三大框架(即Struts2、Spring4和Hibernate4),搭建项目架构原型。 项目架构原型:Struts2.3.16 + Spring4.1.1 + Hibernate4.3.6。 此外,还有:log4j、slf4j、junit4、ehcache等知识点。 项目...
struts2.0+hibernate3.0+spring3.5框架搭建 通俗易懂,适合初学者;里面有struts2.0+hibernate3.0+spring3.5框架搭建的详细配置 详细:http://itba.taobao.com 如有不懂联系:QQ:294647378
Struts2、Hibernate和Spring是Java Web开发中的三大框架,它们各自负责不同的职责,共同构建了一个强大的企业级应用架构。本篇文章将详细讲解如何利用这三个框架进行整合搭建,旨在为初学者提供一个清晰的入门指南。...
### Java8 + Tomcat8 + Struts2.5 + Spring4.3 + Hibernate5.2 框架搭建详细过程 #### 一、环境配置与准备 在搭建一个基于Java8 + Tomcat8 + Struts2.5 + Spring4.3 + Hibernate5.2 的项目前,首先需要对开发环境...
本教程将详细介绍如何使用Eclipse IDE搭建一个基于Struts2.5、Spring5.0和Hibernate5.2的整合框架,提供一个可运行的Demo实例。这个组合是Java企业级开发中常见的技术栈,它们各自负责不同的职责:Struts2作为前端...