整合Spring2.5+Struts2.1+Hibernate3.2
2008-10-25 23:49
441 查看
在MyEclipse中新建一个项目,基于j2ee5,按以下步骤:
1、类库:
Struts:Commons-logging-x.x.x.jar、freemarker-x.x.x.jar、ognl-x.x.x.jar、xwork-x.x.x.jar、struts-2-core-x.x.x.jar、struts2-spring-plugin-x.x.x.jar
Hibernate:Hibernate3.2 Core Libraries、Hibernate 3.2 Annotations & Entity Manager(删除asm.jar、asm-attrs.jar、cglib-x.x.x.jar)
Spring:Spring 2.5 Aop Libraries、 Spring 2.5 Core Libraries、 Spring 2.5 Persistence Core Libraries、 Spring 2.5 Web Libraries
Database pool: commons-pool.jar、commons-dbcp.jar 或者 c3p0-x.x.x.jar
Database driver: mysql-connector-java-x.x.x.jar
2、web.xml
添加以下内容:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>characterEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>actionContextCleanUp</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter-mapping>
<filter-name>actionContextCleanUp</filter-name>
<url-pattern>/*</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>
3、applicationContext.xml及jdbc.properties
在/WEB-INF下新建applicationContext.xml及jdbc.properties,内容如下:
<?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="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/jdbc.properties"/>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/com/test/domain/hbm/</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.generate_statistics">true</prop>
</props>
</property>
</bean>
</beans>
jdbc.properties的内容如下:
jdbc.driverClassName=org.gjt.mm.mysql.Driver
jdbc.url=jdbc:mysql://localhost:3306/testssh
jdbc.username=root
jdbc.password=
4、src下添加login4j.properties、messageResource.properties、struts.properties、struts.xml
其中struts.properties的内容可以从struts2-core-x.x.x.jar的org.apache.struts2下的default.properties下拷过来,修改以下几项
struts.locale=zh_CN
struts.devMode = true #开发时高为true可以在调试时打印出更多信息,部署时设为false
struts.custom.i18n.resources=messageResource #国际化文件为messageResource.properties
其他采用默认即可
struts.xml的模板如下:(可直接从struts2-core-x.x.x.jar根目录下拷struts-default.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="testssh" namespace="/" extends="struts-default">
<action name="login" class="login">
<result name="success">/success.jsp</result>
<result name="input">/index.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
log4j.properties内容如下:(只定义了输出Debug或以上级别信息到控制台)
#1区
#### Use two appenders, one to log to console, another to log to a file
log4j.rootLogger = debug, stdout
#2区
#Print only messages of priority WARN or higher for your category
#log4j.logger.TestLog4j= , R
#log4j.logger.TestLog4j.TestLog4j2=WARN
#3区
#### First appender writes to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
#4区
#### Second appender writes to a file
#log4j.appender.R=org.apache.log4j.RollingFileAppender
#log4j.appender.R.File=F://nepalon//classes//TestLog4j//example.log
# Control the maximum log file size
#log4j.appender.R.MaxFileSize=100KB
# Archive log files (one backup file here)
#log4j.appender.R.MaxBackupIndex=1
#log4j.appender.R.layout=org.apache.log4j.PatternLayout
#log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd hh:mm:ss}:%p %t %c - %m%n
5、src下的com.test.domain.hbm存放hibernate的表对象映射文件,如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>
<class name="com.test.domain.User" table="test" catalog="testssh">
<id name="id" type="java.lang.Integer">
<column name="Id"/>
<generator class="increment"/>
</id>
<property name="username" type="java.lang.String">
<column name="name"/>
</property>
<property name="password">
<column name="password"/>
</property>
</class>
</hibernate-mapping>
类的Dao操作继承Spring的HibernateDaoSupport,并使用getHibernateTemplate()方法取得HibernateTemplate对象进行各种增删改查操作。
6、Struts2的Xwork验证框架配置文件,如LoginAction-validate.xml(ActionName-validation.xml),与相应Action放在同个目录下,在Jsp页面的<s:form />标签加上"validate=true"进行前台较验。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="name">
<field-validator type="requiredstring">
<message key="login.username.empty"/>
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<message key="login.password.empty"/>
</field-validator>
</field>
</validators>
Ps:struts2的Xwork验证框架有些时候可以用有些时候又不行,感觉有些奇怪,就是无论输入什么值,都提示输入为空,而且在IE下前台无法自动生成正确的Js脚本。不知道是我配置有错还是Struts本身的问题,网上又找不到类似的问题。
1、类库:
Struts:Commons-logging-x.x.x.jar、freemarker-x.x.x.jar、ognl-x.x.x.jar、xwork-x.x.x.jar、struts-2-core-x.x.x.jar、struts2-spring-plugin-x.x.x.jar
Hibernate:Hibernate3.2 Core Libraries、Hibernate 3.2 Annotations & Entity Manager(删除asm.jar、asm-attrs.jar、cglib-x.x.x.jar)
Spring:Spring 2.5 Aop Libraries、 Spring 2.5 Core Libraries、 Spring 2.5 Persistence Core Libraries、 Spring 2.5 Web Libraries
Database pool: commons-pool.jar、commons-dbcp.jar 或者 c3p0-x.x.x.jar
Database driver: mysql-connector-java-x.x.x.jar
2、web.xml
添加以下内容:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>characterEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>actionContextCleanUp</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter-mapping>
<filter-name>actionContextCleanUp</filter-name>
<url-pattern>/*</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>
3、applicationContext.xml及jdbc.properties
在/WEB-INF下新建applicationContext.xml及jdbc.properties,内容如下:
<?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="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/jdbc.properties"/>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/com/test/domain/hbm/</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.generate_statistics">true</prop>
</props>
</property>
</bean>
</beans>
jdbc.properties的内容如下:
jdbc.driverClassName=org.gjt.mm.mysql.Driver
jdbc.url=jdbc:mysql://localhost:3306/testssh
jdbc.username=root
jdbc.password=
4、src下添加login4j.properties、messageResource.properties、struts.properties、struts.xml
其中struts.properties的内容可以从struts2-core-x.x.x.jar的org.apache.struts2下的default.properties下拷过来,修改以下几项
struts.locale=zh_CN
struts.devMode = true #开发时高为true可以在调试时打印出更多信息,部署时设为false
struts.custom.i18n.resources=messageResource #国际化文件为messageResource.properties
其他采用默认即可
struts.xml的模板如下:(可直接从struts2-core-x.x.x.jar根目录下拷struts-default.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="testssh" namespace="/" extends="struts-default">
<action name="login" class="login">
<result name="success">/success.jsp</result>
<result name="input">/index.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
log4j.properties内容如下:(只定义了输出Debug或以上级别信息到控制台)
#1区
#### Use two appenders, one to log to console, another to log to a file
log4j.rootLogger = debug, stdout
#2区
#Print only messages of priority WARN or higher for your category
#log4j.logger.TestLog4j= , R
#log4j.logger.TestLog4j.TestLog4j2=WARN
#3区
#### First appender writes to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
#4区
#### Second appender writes to a file
#log4j.appender.R=org.apache.log4j.RollingFileAppender
#log4j.appender.R.File=F://nepalon//classes//TestLog4j//example.log
# Control the maximum log file size
#log4j.appender.R.MaxFileSize=100KB
# Archive log files (one backup file here)
#log4j.appender.R.MaxBackupIndex=1
#log4j.appender.R.layout=org.apache.log4j.PatternLayout
#log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd hh:mm:ss}:%p %t %c - %m%n
5、src下的com.test.domain.hbm存放hibernate的表对象映射文件,如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>
<class name="com.test.domain.User" table="test" catalog="testssh">
<id name="id" type="java.lang.Integer">
<column name="Id"/>
<generator class="increment"/>
</id>
<property name="username" type="java.lang.String">
<column name="name"/>
</property>
<property name="password">
<column name="password"/>
</property>
</class>
</hibernate-mapping>
类的Dao操作继承Spring的HibernateDaoSupport,并使用getHibernateTemplate()方法取得HibernateTemplate对象进行各种增删改查操作。
6、Struts2的Xwork验证框架配置文件,如LoginAction-validate.xml(ActionName-validation.xml),与相应Action放在同个目录下,在Jsp页面的<s:form />标签加上"validate=true"进行前台较验。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="name">
<field-validator type="requiredstring">
<message key="login.username.empty"/>
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<message key="login.password.empty"/>
</field-validator>
</field>
</validators>
Ps:struts2的Xwork验证框架有些时候可以用有些时候又不行,感觉有些奇怪,就是无论输入什么值,都提示输入为空,而且在IE下前台无法自动生成正确的Js脚本。不知道是我配置有错还是Struts本身的问题,网上又找不到类似的问题。
相关文章推荐
- struts2.2+hibernate3.2+spring2.5整合所需要的包
- ssh框架搭建Struts2.06+spring2.5+hibernate3.2整合实例代码教程步骤
- ssh框架搭建Struts2.06+spring2.5+hibernate3.2整合实例代码教程步骤
- struts2.1.8+spring2.5+hibernate3.2+ext3.0框架整合笔记
- Struts2.1.6 + Spring2.5+Hibernate3.2整合
- OA学习笔记-005-Spring2.5与struts2.1整合
- myeclipse整合Struts1.2+Hibernate3.2+Spring2.5
- MyEclipse8.0构建S2SH(struts2.1+spring2.5+hibernate3.2)项目
- Struts2.1.6 + Spring2.5+Hibernate3.2整合
- Struts2.1+Spring2.5+tomcat6.0连接池整合过程详解
- Spring2.5、Struts2.1、Hibernate3.2、sitemesh、freemarker整合开发常见问题及解决方案
- struts2.1+spring2.5+hibernate3.3整合之第一步(spring2.5+hibernate3.3)
- SSH框架搭建Struts2.06+spring2.5+hibernate3.2整合实例代码教程步骤
- struts2.1.8+spring2.5+hibernate3.2+ext3.0框架整合笔记
- 学习笔记ssh2(struts2+hibernate3.2+spring2.5)整合
- 整合struts1.2+spring2.5+hibernate3.2实现简单登录
- 基于Annotation的Struts2.1+Hibernate3.3+Spring2.5整合开发
- 在Myeclipse7中整合struts2+spring2.5+hibernate3.2
- 框架整合之--------spring2.5+struts2.1+hibernate3.6
- web学习:Spring2.5+Hibernate3.3+Struts1.3整合小例子