搭建一个struts2+spring+hibernate框架
来源:互联网 发布:淘宝刷单被骗案例 编辑:程序博客网 时间:2024/05/17 13:13
我们使用的开发工具是Myeclipse,数据库是mysql
1、创建一个web工程:
建好的项目是:
2、引入jar包
首先我们引入struts2的jar包
在我们下载的struts2的包中struts-2.3.15.3\apps\struts2-blank.war\WEB-INF\lib\*.jar里面存放。
使用JSon的包
struts-2.3.15.3\lib\struts2-json-plugin-2.3.15.3.jar
struts-2.3.15.3\lib\struts2-spring-plugin-2.3.15.3.jar
配置文件:需要配置web.xml和struts.xml
web.xml
<span style="font-family:KaiTi_GB2312;font-size:18px;"><!-- 配置struts2核心过滤器 --> <filter> <filter-name>struts2</filter-nam <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></span>
* struts.xml
因为自己建立的项目中没有所以需要找到一个Struts.xml文件复制过来。可以在自己下载的struts2的包中查找。
在项目中的结果
第二个是Spring
* jar包:
Spring3.2 开发最基本jar包
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.apache.log4j-1.2.15.jar
AOP开发
spring-aop-3.2.0.RELEASE.jar
spring-aspects-3.2.0.RELEASE.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
Spring Jdbc开发
spring-jdbc-3.2.0.RELEASE.jar
spring-tx-3.2.0.RELEASE.jar
Spring事务管理
spring-tx-3.2.0.RELEASE.jar
Spring整合其他ORM框架整合Hibernate
spring-orm-3.2.0.RELEASE.jar
Spring在web中使用
spring-web-3.2.0.RELEASE.jar
Spring整合Junit测试
spring-test-3.2.0.RELEASE.jar
拷贝一个applicationContext.xml可以在Spring开发的jar包里面查找。
拷贝的位置
* 配置文件:web.xml和applicationConte.xml
* web.xml
<span style="font-family:KaiTi_GB2312;font-size:18px;"> <!-- 配置Spring的核心监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <!-- 全局的参数 --> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param></span>* applicationContext.xml
* log4j.properties
* Hibernate:
* jar包:
* hibernate-distribution-3.6.10.Final\hibernate3.jar
* hibernate-distribution-3.6.10.Final\lib\required\*.jar
* hibernate-distribution-3.6.10.Final\lib\jpa\*.jar
* slf4j-log4j整合的jar包 :
* 数据库驱动:
* 连接池:(c3p0连接池)
* 配置文件:Hibernate的配置文件是可以被整合到spring中的。
* 没有hibernate的核心配置文件的方式整合:
* 映射文件:是必须要有的。
解释一下这里的映射文件是指:
配置基本配置信息: C3P0连接池:和事物管理以及一些Hibernate的一些信息,这些都可以在spring中进行的。
* C3P0连接池:
* 引入外部属性文件:
* jdbc.properties
内容:
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql:///shop
jdbc.user = root
jdbc.password =root
* 配置连接池:在spring的文件applicationContext.xml中进行配置。
<span style="font-family:KaiTi_GB2312;font-size:18px;"><!-- 配置连接池: --><!-- 引入外部属性文件 --><context:property-placeholder location="classpath:jdbc.properties"/><!-- 配置C3P0连接池: --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driver}"/><property name="jdbcUrl" value="${jdbc.url}"/><property name="user" value="${jdbc.user}"/><property name="password" value="${jdbc.password}"/></bean><span style="font-family: KaiTi_GB2312; background-color: rgb(255, 255, 255);"></span><span style="font-family: KaiTi_GB2312; background-color: rgb(255, 255, 255);"> </span>* Hibernate相关信息:也是在applicationContext.xml<pre name="code" class="html"><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><!-- 注入连接池 --><property name="dataSource" ref="dataSource"/><!-- 配置Hibernate的其他的属性 --><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><prop key="hibernate.show_sql">true</prop><prop key="hibernate.format_sql">true</prop><prop key="hibernate.connection.autocommit">false</prop><prop key="hibernate.hbm2ddl.auto">update</prop></props></property><!-- 配置Hibernate的映射文件 如果有映射文件的话可以在这里进行配置 <property name="mappingResources"><span style="white-space:pre"></span> <list><span style="white-space:pre"></span> <value>cn/itcast/shop/user/vo/User.hbm.xml</value> </list><span style="white-space:pre"></span></property> --></bean></span>
* 事务管理:也是在applicationContext.xml
<span style="font-family:KaiTi_GB2312;font-size:18px;"><!-- 事务管理: --><!-- 事务管理器 --><bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory"/></bean> <!-- 开启注解事务 --><tx:annotation-driven transaction-manager="transactionManager"/></span>
3.引入页面:
引入静态页面:我们的页面一般不会直接放到webroot 下面因为在这里可以直接被浏览器访问到,我们一般放到web-inf中
我们可以编写一个JSP页面,可以创建一个默认的JSP命名为Index。
* 在WEB-INF/jsp/
* 将静态页面复制到jsp下.
* 将css/js 放到 WebRoot下.
下面只是一个例子图片
7. 访问首页:只是完成了页面的跳转
建包的格式:
* cn.itcast.shop.index
* action
* cn.itcast.shop.user
* action
* service
* dao
* vo
下面图片只是一个例子
4. 编写Action:
* IndexAction
<span style="font-family:KaiTi_GB2312;font-size:18px;">package cn.itcast.shop.index.action;import com.opensymphony.xwork2.ActionSupport;public class IndexAction extends ActionSupport {public String execute(){return "index";}}</span>
5. 配置Action
* 配置applicationContext.xml 这是使用Spring来管理我们的Action
<span style="font-family:KaiTi_GB2312;font-size:18px;"><!-- Action的配置 将我们的Action交给Spring管理 ===========================--><!-- 首页访问的Action --><bean id="indexAction" class="cn.itcast.shop.index.action.IndexAction" scope="prototype"><property name="categoryService" ref="categoryService"/><property name="productService" ref="productService"/></bean></span>
配置struts.xml
<span style="font-family:KaiTi_GB2312;font-size:18px;"><?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts><constant name="struts.devMode" value="false" /><package name="shopTest" extends="struts-default" namespace="/"><!-- 配置首页访问的Action --><action name="index" class="indexAction"><result name="index">/WEB-INF/jsp/index.jsp</result></action></package></struts></span>整个项目的目录结构
接下来我们就可以在浏览器中访问了。ip:端口号/项目名字/需要跳转的Action
在这个项目中如果我们没有在mysql中创建一个mysql数据库,暂时不用建表,那么我们的hibernate配置就没有使用到,所以在启动的时候要将hibernate配置注释掉。
咱们框架搭建好了之后接下来会详细的解析的。
- 搭建一个struts2+spring+hibernate框架
- Struts2+Hibernate+Spring框架搭建
- 搭建一个简单的Struts2+Spring+Hibernate框架
- 框架搭建(Struts2 + Spring + Hibernate)
- Struts2+Hibernate+Spring框架搭建(一)
- Struts2+Hibernate+Spring框架搭建(二)
- Struts2+Hibernate+Spring框架搭建(三)
- Struts2 + Spring + Hibernate 框架搭建 步骤
- Struts2 + spring + hibernate 框架环境搭建
- 整合Struts2 Spring Hibernate,搭建SSH框架
- 轻松搭建Struts2、Spring和Hibernate框架
- SSH(Struts2+Spring+Hibernate)框架搭建流程
- spring+hibernate+struts2; spring +mybaits+struts2框架分析和搭建
- 搭建Struts2+Spring+Hibernate
- Struts2+Spring+Hibernate搭建
- Struts2+spring+hibernate框架
- Struts2+Spring框架搭建
- struts2+spring框架搭建
- %运算符 1
- (4)shell替换
- MySQL教程-原理篇-B+Tree索引
- 14 screenShotLikeQQ
- 【大话设计模式】六大原则
- 搭建一个struts2+spring+hibernate框架
- 导入AndroidStudio的项目卡死,如何快速导入AndroidStudio的项目
- 【三层学习】——初识
- ubuntu下apache2 安装 配置 卸载 CGI设置 SSL设置
- BZOJ 1013: [JSOI2008]球形空间产生器sphere
- HDU-1026 Ignatius and the Princess I (BFS)
- bzoj2333 棘手的操作 可并堆or dfs序列+线段树
- 弱语言变量作用域
- Maven主要插件汇总