Struts2与Hibernate整合: 1. 创建struts2和hibernate用户类库 2. 导入struts2与hibernate的jar包 3. 配置web.xml文件(加入struts2的过滤器) <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> // struts2过滤器 </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> // 过滤所有请求 </filter-mapping> 4. 创建struts.xml WEB-INF/classes/struts.xml --> src/struts.xml <package name="default" namespace="/" extends="struts-default"></package> 5. 配置hibernate.cfg.xml(hibernate的主配置文档) src/hibernate.cfg.xml <hibernate-configuration> <session-factory> <property name="connection.username">root</property> <property name="connection.password"></property> <property name="connection.driver_class">com.mysql.jbdc.Driver</property> <property name="connection.url">jdbc:mysql:///test?useUnicode=true&characterEncoding=UTF-8</property> <property name="dialect">org.hibernate.dialet.MySQLDialect</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hbm2dd1.auto">update</property> <property name="hibernate.current_session_context_class">thread</property>// 使用getCurrentSession方式打开会话 </session-factory> </hibernate-configuration>
重点 重点
Struts2与Hibernate整合: 1. 创建struts2和hibernate用户类库 2. 导入struts2与hibernate的jar包 3. 配置web.xml文件(加入struts2的过滤器) <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> // struts2过滤器 </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> // 过滤所有请求 </filter-mapping> 4. 创建struts.xml WEB-INF/classes/struts.xml --> src/struts.xml <package name="default" namespace="/" extends="struts-default"> </package> 5. 配置hibernate.cfg.xml(hibernate的主配置文档) src/hibernate.cfg.xml <hibernate-configuration> <session-factory> <property name="connection.username">root</property> <property name="connection.password"></property> <property name="connection.driver_class">com.mysql.jbdc.Driver</property> <property name="connection.url">jdbc:mysql:///test?useUnicode=true&characterEncoding=UTF-8</property> <property name="dialect">org.hibernate.dialet.MySQLDialect</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hbm2dd1.auto">update</property> <property name="hibernate.current_session_context_class">thread</property>// 使用getCurrentSession方式打开会话 </session-factory> </hibernate-configuration>
配置web.xml
struts2与hibernate整合步骤
1、配置hibernate.cfg.xml文件 <property name="connection.username">root</property> <property name="connection.password"></property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql:///test?useUnicode=true&characterEncoding=UTF-8</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hbm2ddl.auto">update</property> <property name="hibernate.current_session_context_class">thread</property>
struts2的示例空项目放在tomcat运行后,在web-inf下的classes找struts.xml复制到自己的项目
这个<filter-class>在struts2的核心jar包可以找到,
struts2-core-***.jar
struts2示例里lib下的jar包可以认为是struts2核心jar包,可复制到自己的项目
把struts2的.war文件放在tomcat的webapps目录下,启动tomcat,会自动解压之
解压apps下的示例:struts2-blank