1、在Eclipse中新建WEB工程
2、添加所需jar
3、配置Struts2:拷贝一个struts.xml模板到src目录,并适当修改,在web.xml添加Struts2所需的配置代码
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
<struts>
<!-- 配置为开发模式 -->
<constant name="struts.devMode" value="true" />
<!-- 配置扩展名为action -->
<constant name="struts.action.extension" value="action" />
<!-- 当struts配置文件修改时,系统是否自动重新加载该文件,默认false,开发阶段最好打开 -->
<constant name="struts.configuration.xml.reload" value="true" />
<!-- 全局视图 -->
<package name="default" namespace="/" extends="struts-default">
<global-results>
<result name="error">/error.jsp</result>
<result name="ajax">/ajax.jsp</result>
<result name="noLogin">/login.jsp</result>
</global-results>
</package>
</struts>
web.xml添加如下代码:
<!-- 配置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>*.action</url-pattern>
</filter-mapping>
4、配置Spring:拷贝一个applicationContext.xml模板到 WEB-INF目录下,配置SessionFactory,并在web.xml添加Spring所需的配置代码,这里把数据库连接相关的信息配置在Hibernate中。
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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee-4.2.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!-- 添加sessionFactory bane ,注意,该类是Spring提供的 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" scope="prototype">
<!-- 注入Hibernate 配置文件路径,前面要加上 classpath: -->
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
</beans>
在web.xml中添加Spring支持:
<!-- 配置Spring监听器 -->
<listener>
<!--配置Spring的上下文加载监听 -->
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
5、配置Hibernate,拷贝一个hibernate.cfg.xml模板到src目录,并适当修改,在web.xml添加Hibernate所需的配置代码
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
<hibernate-configuration>
<session-factory>
<!-- 数据库连接配置 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/staff?</property>
<!-- 设置默认的数据库连接池 -->
<property name="connection.pool_size">5</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- 根据schema更新数据表的工具 -->
<property name="hbm2ddl.auto">update</property>
<!-- 数据表映射配置文件 -->
<mapping resource="xxx.hbm.xml"/>
<event type="pre-insert"/>
</session-factory>
</hibernate-configuration>
在web.xml中添加Hibernate支持:
<!-- hibernate数据访问session -->
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>