前段时间吧SSM框架搭建了,现在来吧SSH也搭建起来吧。
第一、
先把Struts整合进来:
1. 导入核心jar包
commons-fileupload-1.3.1.jar commons-io-2.2.jar commons-lang3-3.2.jar commons-logging-1.1.3.jar freemarker-2.3.22.jar javassist-3.11.0.GA.jar
ognl-3.0.6.jar struts2-core-2.3.24.1.jar xwork-core-2.3.24.1.jar
2. 配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>TestSSH</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 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>
</web-app>
3. 在src下建立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>
<!-- 指定默认编码集,作用于HttpServletRequest的setCharacterEncoding方法 -->
<constant name="struts.i18n.encoding" value="UTF-8" />
<!-- 设置浏览器是否缓存静态内容 -->
<constant name="struts.xserve.static.browserCache" value="false" />
<constant name="struts.devMode" value="true" />
<!-- 当struts的配置文件修改后,系统是否自动重新加载该文件 -->
<constant name="struts.configuration.xml.reload" value="true" />
<!-- <constant name="struts.objectFactory" value="spring" /> -->
<package name="defaultPackage" extends="struts-default">
<action name="success"></action>
</package>
<!-- 所有的action都应该放在对应的package下 -->
</struts>
OK了。
第二、添加Hibernate框架(这个比较独立)
1. jar包,基于Annotation的。
2. 建立hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">create</property><!--不写的话不会自动生成表 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/Hibernate_Annotation</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="myeclipse.connection.profile">MySqlDriver</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
</session-factory>
</hibernate-configuration>
1. 增加相应的jar包,这里就不说了,百度下。
2 src下新建applicationContext.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" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
<span style="white-space:pre"> </span><!-- 配置bean -->
</beans>
3. 由于需要,在src下还要新建一个resources/db.properties用于存放数据库的配置
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/SSH
jdbc.username=root
jdbc.password=root
jdbc.maxActive = 2
jdbc.maxIdle =5
jdbc.minIdle=1
jdbc.initialSize =3
jdbc.maxWait =3000
第三、整合Struts2和Spring(即Spring的AOP,管理Struts2中的action已经action之间的引用)
(1)、需要增加web.xml配置,有spring来管理
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
<!-- 数据库配置文件位置 必须写上classpath:才能相对于classes目录 -->
<context:property-placeholder location="classpath:resources/db.properties" />
<!-- 配置dbcp数据源 -->
<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}" />
<!-- 队列中的最小等待数 -->
<property name="minIdle" value="${jdbc.minIdle}"></property>
<!-- 队列中的最大等待数 -->
<property name="maxIdle" value="${jdbc.maxIdle}"></property>
<!-- 最长等待时间,单位毫秒 -->
<property name="maxWait" value="${jdbc.maxWait}"></property>
<!-- 最大活跃数 -->
<property name="maxActive" value="${jdbc.maxActive}"></property>
<property name="initialSize" value="${jdbc.initialSize}"></property>
</bean>
<!-- 配置Hibernate SessionFactoryBean -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>ray.bean</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.format_sql">true</prop>
<!--<prop key="hibernate.hbm2ddl.auto">create</prop>
--></props>
</property>
</bean>
<!-- 事务配置 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 管理bean -->
<bean id="loginAction" class="ray.action.LoginAction" />
</beans>
(3)、添加相应的jar包,struts2-spring-plugin-2.1.8.1.jar
并把struts2中的action交给spring容器管理,在struts.xml中action的class等于sping.xml中的bean的id。
applicationContext.xml中配置:
<!-- 管理bean -->
<bean id="loginAction" class="ray.action.LoginAction" />
在struts.xml中修改:
<package name="defaultPackage" extends="struts-default">
<action name="login" class="loginAction">
<result name="success">/WEB-INF/jsp/common/succ.jsp</result>
</action>
</package>
ok,大功告成。