开发环境:MyEclipse Blue 6.5 + Tomcat 6.x + JDK 1.6.x
1、引入所有的jar。
有两种方法:
1)将所有包复制到project的WEB-INF/lib文件夹中,刷新项目即可发现所有的包以被项目引用。
手动添加xml文件,并更改相应的文件名为 applicationContext.xml、struts.xml、hibernate.cfg.xml(可选)、applicationContext.properties(可选)、struts.properties(可选),*.properties文件只是将相应的xml文件中的某些属性单独放到一个文件,使目录结构更清晰。
2)复制struts2包Struts-2.2.3\apps\struts2-blank\WEB-INF\lib下面的所有JAR包到项目,使用MyEclipse中Add Hibernate Capabilities..添加Hibernate相关的jar,使用MyEclipse中Add Spring Capabilities..添加Spring相关jar,这种方法添加的jar不完善,更具需要完善(commons-dbcp、commons-pool)
2、配置文件,使SSH2环境整合
1)struts.xml、struts.properties
<?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>
<!-- 将Action的创建交给spring来管理,可以在properties文件设置 -->
<!--
<constant name="struts.objectFactory" value="spring" />
-->
<!-- 更改struts2请求Action的后缀名,默认为action。若想去掉后缀,设为","即可,,可以在properties文件设置 -->
<!--
<constant name="struts.action.extension" value="do"></constant>
-->
<!-- package的name可以自定义 -->
<package name="ssh2" extends="struts-default">
<!-- 配置拦截器 必须有defaultStack? 开始 -->
<!--
<interceptors>
<interceptor name="myInterceptor" class="myInterceptor"></interceptor>
<interceptor-stack name="myDefult">
<interceptor-ref name="myInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
-->
<!-- 配置拦截器 结束 -->
<!-- 处理请求的action,class对应的值是applicationContext.xml文件中配置的bead.id -->
<action name="doRegister" class="springUserRegisterAction">
<result name="success">ok.jsp</result>
<result name="error">error.jsp</result>
</action>
</package>
</struts>
struts.devMode=true
struts.action.extension=php
struts.enable.DynamicMethodInvocation=true
struts.i18n.reload=true
struts.ui.theme=simple
struts.custom.i18n.resources=globalMessages
struts.locale=zh_CN
struts.i18n.encoding=GBK
struts.objectFactory=spring
struts.objectFactory.spring.autoWire=name
struts.serve.static.browserCache=false
struts.url.includeParams=none
struts.multipart.parser=jakarta
struts.multipart.maxSize=1048576000
2)applicationContext.xml、spring.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="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>com.microsoft.sqlserver.jdbc.SQLServerDriver</value>
</property>
<property name="url">
<value>jdbc:sqlserver://localhost:1433;databaseName=firetree</value>
</property>
<property name="username">
<value>sa</value>
</property>
<property name="password">
<value>******</value>
</property>
</bean>
<!-- 连接池 结束 -->
<!-- 缓冲区 开始 缓存SQL语句,映射数据,如果连接多个数据库,需要创建多个sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 配置hibernate映射文件 开始 >>>>>>>>>>> -->
<property name="mappingResources">
<list>
<value>com/firetree/bean/User.hbm.xml</value>
<value>com/firetree/bean/Users.hbm.xml</value>
</list>
</property>
<!-- 配置hibernate映射文件 结束 <<<<<<<<<<< -->
<!-- 配置hibernate的属性,可以在spring.properties文件中设置 开始 -->
<!--
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
-->
<!-- 配置hibernate的属性 结束 -->
</bean>
<!-- 缓冲区 结束 -->
<!-- 业务处理类 开始 -->
<bean id="userDao" class="com.firetree.dao.impl.UserDaoImpl">
<!-- spring注入的参数,可以类中直接用属性获取 -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 业务处理类 结束 -->
<bean id="userService" class="com.firetree.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="springUserRegisterAction" class="com.firetree.action.UserRegisterAction">
<property name="service" ref="userService"></property>
</bean>
</beans>
hibernate.dialect=org.hibernate.dialect.SQLServerDialect
hibernate.jdbc.batch_size=25
hibernate.jdbc.fetch_size=50
hibernate.show_sql=true
hibernate.connection.release_mode=after_transaction
3)xml<?xml version="1.0" encoding="UTF-8"?>
<?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">
<!-- 上下文参数 开始 -->
<context-param>
<!-- 内容配置文件位置 开始 -->
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
<!-- 更多的配置文件可以在这里加载 classpath前缀,会在/WEB-INF/classes 目录下开始查找
classpath:coreContext.xml
classpath:components.xml
-->
<!-- 没有classpath前缀,要用一下形式 -->
<!-- /WEB-INF/classes/applicationContext.xml -->
</param-value>
<!-- 内容配置文件位置 结束 -->
</context-param>
<!-- 上下文参数 结束 -->
<!-- 配置CharacterEncoding,设置字符集 -->
<filter>
<filter-name>characterEncodingFilter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<!-- characterEncodingFilter应用的url -->
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 设置struts,struts入口,如果没配置相当于没有struts -->
<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,spring入口,如果没配置相当于没有spring -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 页面session配置 -->
<session-config>
<session-timeout>20</session-timeout>
</session-config>
<!-- 错误页面 -->
<error-page>
<error-code>404</error-code>
<location>/error404.html</location>
</error-page>
</web-app>
ssh框架的互相配置:
设置struts,将action的创建交由spring管理
设置spring,将sessionFactory交由hibernate管理,并接管设置所有hibernate相关的配置文件(hibernate 无需单独设置,所有一切在spring中配置)