首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

Struts2 Spring2.5 hibernate3.3调整开发环境搭建全过程

2012-10-06 
Struts2Spring2.5hibernate3.3整合开发环境搭建全过程Struts2? Spring2.5? hibernate3.3整合开发环境搭建

Struts2 Spring2.5 hibernate3.3整合开发环境搭建全过程

Struts2? Spring2.5? hibernate3.3整合开发环境搭建全过程

<?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><!--spring的配置指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找--><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:beans.xml</param-value></context-param><!-- 对Spring容器进行实例化 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- struts2的配置 --><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><!--使用Spring解决struts2的中文乱码问题 --><filter><filter-name>encoding</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param></filter><filter-mapping><filter-name>encoding</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- 使用Spring解决hibernate因session关闭导致的 延迟加载例外问题 --><filter><filter-name>OpenSessionInViewFilter</filter-name><filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class><init-param><!--指定org.springframework.orm.hibernate3.LocalSessionFactoryBean在spring配置文件中的名称,默认值为sessionFactory.如果LocalSessionFactoryBean在spring中的名称不是sessionFactory,该参数一定要指定,否则会出现找不到sessionFactory的例外--><param-name>sessionFactoryBeanName</param-name><param-value>sessionFactory</param-value></init-param></filter><filter-mapping><filter-name>OpenSessionInViewFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>

?

dhsfactory.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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><!-- 数据库连接池 --><bean id="dataSource" value="${jdbc.driverClass}" /><property name="jdbcUrl" value="${jdbc.url}" /><property name="user" value="${jdbc.user}" /><property name="password" value="${jdbc.password}" /><!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 --><property name="initialPoolSize" value="1" /><!--连接池中保留的最小连接数。--><property name="minPoolSize" value="1" /><!--连接池中保留的最大连接数。Default: 15 --><property name="maxPoolSize" value="300" /><!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --><property name="maxIdleTime" value="60" /><!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --><property name="acquireIncrement" value="5" /><!--每60秒检查所有连接池中的空闲连接。Default: 0 --><property name="idleConnectionTestPeriod" value="60" /></bean><bean id="sessionFactory"ref="dataSource" /><!-- 引入映射文件.hbm.xml --><property name="mappingResources"><list><value>cn/dxl/domain/User.hbm.xml</value></list></property><!-- hiberante属性的配置 --><property name="hibernateProperties"><props><prop key="hibernate.dialect">${hibernate.dialect}</prop><prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop><prop key="hibernate.show_sql">${hibernate.show_sql}</prop><prop key="hibernate.format_sql">${hibernate.format_sql}</prop></props></property></bean><!-- 分散配置信息 --><bean id="propertyPlaceholderConfigurer"name="code">hibernate.dialect=org.hibernate.dialect.MySQL5Dialecthibernate.hbm2ddl.auto=updatehibernate.show_sql=truehibernate.format_sql=falsejdbc.driverClass=com.mysql.jdbc.Driverjdbc.url=jdbc\:mysql\://localhost\:3306/s2sh?useUnicode\=true&characterEncoding\=UTF-8jdbc.user=rootjdbc.password=123

Struts.xml<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"><struts><!-- objectFactory spring --><constant name="struts.objectFactory" value="spring" /><!-- 国际化 --><constant name="struts.custom.i18n.resources" value="dxl"/><!-- 开发的模式 --><constant name="struts.devMode" value="false"/><!-- 默认的主题视图 --><constant name="struts.ui.theme" value="xhtml"/><!-- 包含其他的配置文件 以模块的方式 --><include file="cn/dxl/config/xml/struts-user.xml"/></struts>

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN""http://struts.apache.org/dtds/struts-2.1.7.dtd"><struts><package name="dxl" extends="json-default" namespace="/csdn"><global-results><result name="input">/index.jsp</result></global-results><!—采用通配符-- ><action name="user_*" method="{1}"><result name="insert" type="chain">user_list</result><!-- 当执行index.jsp中的插入用户时用——》<result name="list">/WEB-INF/user/list.jsp</result>否则用下边的--><result name="list">/WEB-INF/user/list.jsp</result><result name="update" type="chain">user_list</result><result name="delete" type="chain">user_list</result><result name="edit">/WEB-INF/user/update.jsp</result><result name="add">/WEB-INF/user/insert.jsp</result></action></package></struts>

User.javapublic class User implements Serializable {/** * @author DXL_xiaoli */private static final long serialVersionUID = 1L;private Integer id;private String name;private String pass; private Date rdate;public User() {super();// TODO Auto-generated constructor stub}public User(Integer id, String name, String pass) {super();this.id = id;this.name = name;this.pass = pass;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPass() {return pass;}public void setPass(String pass) {this.pass = pass;}public Date getRdate() {return rdate;}public void setRdate(Date rdate) {this.rdate = rdate;}}

User.hbm.xml<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.dxl.domain"><class name="User" catalog="s2sh" table="users"><id name="id"><generator type="string" length="30" /><property name="pass" type="string" length="12" /><property name="rdate" type="date" /></class></hibernate-mapping>

以上是环境搭建的全过程,以后我将继续我的项目开发,希望能够与你分享成功的时刻……….never give up? !!

热点排行