一、导包
struts2 需要用到的包:
commons-fileupload.jar、commons-io.jar、commons-logging.jar、freemarker.jar、ognl.jar、struts2-core.jar、xwork-core.jar
二、项目引入spring 功能、hibernate 功能
三、改写配置文件
a. web.xml
由于spring采用注入式管理对象方式,我们也必须将struts2中自己编写的action交给spring管理。而spring通过解析applicationContext.xml文件控制反转实例化所有的bean。所以为了能够在应用初始化时对applicationContext.xml进行解析,修改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">
<!-- 配置spring的监听器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<!-- 开启监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置OpenSessionInViewFilter,必须在struts2监听之前 -->
<filter>
<filter-name>lazyLoadingFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>lazyLoadingFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- 设置监听加载上下文 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
b.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>
<constant name="struts.objectFactory" value="spring" />
<include file="/com/time/ssh/action/struts.xml"></include>
</struts>
struts.xml配置需要注意几点:1.文件存放位置 必须是src目录下 2.加入<constant name="struts.objectFactory" value="spring"/> 表示实例化对象交给spring管理 3.多配置文件:开发过程中struts配置文件是经常改动的,为了便于团队开发,有时候我们必须对不同的模块建立不同的struts配置文件。struts2中多配置文件使用非常简单使用<include file="子配置文件路径"></include>
下面看个具体的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="cs" namespace="/test" extends="struts-default">
<action name="login" class="login">
<result name="success">/success.jsp</result>
<result name="false">/fail.jsp</result>
</action>
</package>
</struts>
这里需要注意的是 由于现在action实例是交给spring管理 所以在<action name="login" class="
login"/> 这里不能再给该类路径了 应该是给 在applicationContext.xml中对应配置的bean的id ,为了便于理解在这里将spring配置文件中的代码也一起贴出来:
<bean id="
login" class="com.time.ssh.action.Login">
c applicationContext.xml
关于applicationContext.xml配置问题不是很大 因为就是一些简单的bean配置 只是要注意其中的一些引用,下面介绍一下关于spring多配置文件的写法:spring多配置文件写法大致有两种:一种是在 web.xml配置中 使用
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/context1.xml,
/WEB-INF/classes/context2.xml,
/WEB-INF/classes/context3.xml
</param-value>
</context-param>
利用上下文参数,其中分隔符可以是",",也可以是" "等 ,也可以用通配符application-*,这样配置的要求是,你的Spring配置文件必须是applicationContext-*****.xml这样的形式存在,*号代表通配符,具体就不说了。
第二种方法是在一个application.xml中配置其他多个使用:
<import resource="其它文件路径"/>
这样就可以实现多个struts和spring配置文件的SSH整合了!
分享到:
相关推荐
Struts2.3.16.1+Hibernate3.6.10+Spring3.2.8整合 能够运行,没有任何问题 另外附带applicationContext.xml、struts.xml、hibernate.cfg.xml
SSH整合示例(详情见我博客专栏)之前的博客我们总结了spring基础、spring分别整合struts2、hibernate、mybatis等,今天我们来同时整合下 struts、spring、hibernate,也就是所谓的 ssh 。 整合流程: 1 首先整合...
这里是使用spring配置文件管理hibernate。 先配置mysql数据驱动,打开eclipse database explorer perspective,选择tie数据库中的user表,创建hibernate Reverse Engineering 生成vo,还可以生成dao。 5. 这样...
使用了Struts+Hibernate+Spring来构建整个网站。 布局使用了CSS和DIV。 部署前请修改以下文件: NewReleseSystem\WebRoot\WEB-INF下面的applicationContext.xml文件 修改里面的数据库的用户名和密码为你所安装的...
13、Spring的配置文件applicationContext.xml的的配置,bean的配置,数据库事物的配置,和切面配置 14、 可以拿这个Demo和Struts2_Test 和 Struts2_Hibernate_Test比较一下,认真看的话,你会发现更多有用的信息,...
简单的ssh项目,泛型BaseAction配置,简单注解配置,applicationContext.xml配置,jar包地址 链接:https://pan.baidu.com/s/14NkywuhG6mESyhE1VMkhOQ 密码:en19
2、 Spring配置提供了多种可选整合方案:详见applicationContext.xml的配置 (1) 提供支持三种数据源方案:C3P0、DBCP、JNDI,三选一,请注释或删除其他方案 (2) 提取四种Hibernate整合方案:四选一,请注释或...
本例主要是实现了struts2+spring3+hibernate3的 基本框架搭建的注册登录,以及用户增删改查,适于初学者学习。 包括:注册 登录功能 分页的实现 前端校验 验证码的实现 注册时有ajax 校验,登录时 后台从数据库...
<br>使用方法: (1)在oracle数据库中运行数据库脚本文件cnc.sql. (2)用eclipse(或其它开发工具)直接导入工程. (3)将spring配置文件applicationContext.xml中数据库连接地址改为您自已的地址。...
用EXT+struts2+spring+hibernate做...另外因包太多无法上传,所以请大家自己在myeclipse里面加上spring和hibernate,struts2运行的jar包,jar列表请看根目录下面的txt文件。另外,不会使用的不要再后面骂人,很无趣的!
在本文档中,我们可以看到如何使用MyEclipse来建立web项目、添加struts、hibernate和spring框架的文件、配置各种配置文件等。 6.FreeMarker模板引擎 FreeMarker是一个模板引擎,用于生成Web页面。在本文档中,我们...
•装载Spring的applicationContext.xml文件 •建立业务层和DAO之间的依赖关系 •将Spring应用到Struts中 简介 这个例子是建立一个简单的web应用,叫MyUsers,完成用户管理操作,包含简单的数据库增,删,...
OA办公自动化管理系统是一个基于Struts1.2、Hibernate3.0、Spring2和DWR技术实现的Java Web应用项目。该系统采用了MVC设计模式,将业务逻辑、数据访问和表示层分离,提高了代码的可维护性和可扩展性。Struts1.2作为...
①在web.xml中配置ApplicationContext.xml,并使用ContextLoader监听器实例化spring容器 ②把action交给spring管理,即在spring配置文件中定义action Bean并使用依赖注入功能在action中注入业务Bean,同时修改作用域...
最新 struts2 spring3 hibernate4 环境增删查改web开发 在开发环境:eclipse4+tomcat7+struts2+spring3+hibernate4+SQL2005 增删查改web开发简单示例,所括所有的包。下载后修改applicationContext.xml连接数据库的...
Struts2.0_Hibernate3_spring2.0 部署Web应用请按如下步骤进行: 1. 进入reg_login路径下,将mysql.sql脚本中的语句导入MySQL数据库。 2. 修改reg_login\WEB-INF路径下的applicationContext.xml文件,将其中的...
本系统采用struts+spring+hibernate架构,重在讲述三者结合在一起的应用开发,所有源程序都 有详细的注释,供入门者学生参考,未经作者允许不得用于商业用途,只能用于教学或个人学习。 程序只是整个系统中的一个...
好不容易配出来的 Hibernate的cfg文件不用配,直接Spring的applicationContext-Common.xml里改下数据库连接信息即可部署到Tomcat运行 Jar包都有了 强烈推荐
使用方法: (1)在oracle数据库中运行数据库脚本文件cnc.sql. (2)用eclipse(或其它开发工具)直接导入工程. (3)将spring配置文件applicationContext.xml中数据库连接地址改为您自已的地址。 (4)部署到...