手动整合Struts1.3 +hibernate3.2+Spring2.5
以下是需要的准备工作:
hibernate核心安装包下的:
hibernate3.jar
lib/required/*.jar
lib/optional/ehcache-1.2.3.jar
hibernate 注解安装包下的
lib/test/slf4j-log4j12.jar
Spring安装包下的
dist/spring.jar
dist/modules/spring-webmvc-struts.jar
lib/jakarta-commons/commons-logging.jar、commons-dbcp.jar、commons- pool.jar
lib/aspectj/aspectjweaver.jar、aspectjrt.jar
lib/cglib/cglib-nodep-2.1_3.jar
lib/j2ee/common-annotations.jar
lib/log4j/log4j-1.2.15.jar
Struts
下载struts-1.3.8-lib.zip,需要使用到解压目录下的所有 jar,建议把jstl-1.0.2.jar和standard-1.0.2.jar更换为1.1版本。Spring中已经存在一个antlr- 2.7.6.jar,所以把struts中的antlr-2.7.2.jar删除,避免jar冲突。
数据库驱动jar
在web容器中实例化spring容器,
<!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:beans.xml</param-value>
- </context-param>
- <!-- 对Spring容器进行实例化 -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
在web容器中配置struts
- <servlet>
- <servlet-name>struts</servlet-name>
- <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
- <init-param>
- <param-name>config</param-name>
- <param-value>/WEB-INF/struts-config.xml</param-value>
- </init-param>
- <load-on-startup>0</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>struts</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
自己写个或拷贝个struts配置文件 到web-inf/
Spring配置文件applicatinContext.xml中配置hibernate
- <?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">
- <property name="driverClassName"
- value="com.mysql.jdbc.Driver">
- </property>
- <property name="url"
- value="jdbc:mysql://127.0.0.1:3306/test">
- </property>
- <property name="username" value="root"></property>
- <property name="password" value="123456"></property>
- </bean>
- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource">
- <ref bean="dataSource" />
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">
- org.hibernate.dialect.MySQLDialect
- </prop>
- </props>
- </property>
- <property name="mappingResources">
- <list>
- <value>com/guestbook/model/Guestbook.hbm.xml</value>
- </list>
- </property>
- </bean>
- </beans>
使用spring解决
struts1
.
3
乱 码问题。
- <filter>
- <filter-name>encoding</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>
- </filter>
- <filter-mapping>
- <filter-name>encoding</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
实体bean配置模版.hbm.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="cn.itcast.bean">
- <class name="Person" table="person">
- <id name="id" type="integer" >
- <generator class="native"/>
- </id>
- <property name="name" length="10" not-null="true"/>
- </class>
- </hibernate-mapping>