最近尝试了一下strut2.3.14和hibernate4版本的整合,附近图片中有所需要的jar包。一下是整个过程
1、eclipse中建立一个web项目,同时eclipse中tomcat和jdk已经设置好,不在多说。
2、官网下载struts2.3.14版本,解压打开apps目录,winRar解压struts2-blank.war包,将解压demo下的lib目录
导入项目lib下。
3、下载hibernate4.1.2final版本,解压将required下的所有jar包导入项目lib下,同时导入encache下的slf4j-api-1.6.1jar,同时可根据自己的需求选择slf4J的日志实现,本人使用的是log4j,所有就必须下载slf4j-log4j12-1.7.5.jar,同时下载log4j-1.2.17.jar导入项目lib下
4、添加mysql驱动jar包,此时所需jar包已齐全,但会发现javassist的两个包重复,删掉低版本就ok了。
5、web.xml内容
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Struts Blank</display-name> <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> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
struts.xml内容
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="struts2" namespace="/" extends="struts-default"> <action name="personAction" class="org.hibernate.test.PersonAction" method="savePerson"> <result name="success">/listAll.jsp</result> </action> </package> </struts>
log4j.properties
log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n log4j.rootLogger=info, stdout log4j.logger.org.hibernate.tool.hbm2ddl=trace log4j.logger.org.hibernate.testing.cache=debug
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url"> jdbc:mysql://localhost:3306/hibernate</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- <property name="hbm2ddl.auto">create</property>--> <mapping resource="org/hibernate/model/Person.hbm.xml"/> </session-factory> </hibernate-configuration>