之前讲的内容已经或多或少和整合挂上钩了,当然,之前的内容就不过多的重复。现在在把这个钩真正的挂上。废话不多说。GO!
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>
<!-- 把Struts2交给Spring管理 -->
<constant name="struts.objectFactory" value="spring"></constant>//必须的
<package name="xxx" extends="struts-default" namespace="">
<action name="LoginAc" class="bean的id">//这里的class不是全路径,只是Spring的Bean组件而已了。例如有个Bean的id为adminLogin,则class="adminLogin"。
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<context:component-scan base-package="com.lrl"></context:component-scan>
<!--数据源的定义-->//数据源需commons-pool.jar、commons-dbcp.jar
<bean id="bbsDateSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/bbspro">
</property>
<property name="username" value="root"></property>
<property name="password" value="123"></property>
</bean>
<!--sessionFactory的定义-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="bbsDateSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="packagesToScan">
<value>com.bbs.entity</value>//这里写Entity的包路径,也就是实体类的包路径
</property>
</bean>
<bean id="testDao" class="com.junit.test.TestDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
之前用的是Annotation的Entity,代码如上,下面的代码可以配置使用xxx.hbm.xml的SessionFactory。
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="mySqlDateSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/lrl/entity/User.hbm.xml</value>//这里是xml
</list>
</property>
</bean>
web.xml的配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
//字符编码
<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>
</web-app>
基本配置就如此而已。如果用MyEclipse开发。把JavaEE5.0的jar拷出,导入lib里。避免部分冲突。详情本博客的其他文章。。
总结:
(1)主要开发过程:applicationContext.xml+struts.xml+web.xml
(2)spring管理Hibernate,那么hibernate.cfg.xml就不需要了,在applicationContext配置数据源等信息即可