web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>action</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>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
重要的是红色字体的代码,就是将spring配置文件在启动时加载进来。
struts-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<data-sources />
<form-beans>
<form-bean name="messageForm"
type="org.apache.struts.validator.DynaValidatorActionForm">
<form-property name="mContent" type="java.lang.String" />
<form-property name="userId" type="java.lang.String" />
<form-property name="mTitle" type="java.lang.String" />
</form-bean>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>
<action attribute="messageForm" input="/form/message.jsp"
name="messageForm" path="/addMessage" scope="request"
type="org.springframework.web.struts.DelegatingActionProxy" />
</action-mappings>
<message-resources
parameter="com.winkee.struts.ApplicationResources" />
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
</plug-in>
</struts-config>
注意红色字体的代码,他是将struts中的action转到springz中。
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.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/mydb?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8"></property> //解决乱码问题
<property name="username" value="root"></property>
<property name="password" value="123"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="datasource" />
</property>
<property name="mappingResources">
<list>
<value>com/winkee/dao/Message.hbm.xml</value>
<value>com/winkee/dao/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
</bean>
<bean id="UserDAO" class="com.winkee.dao.UserDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="MessageDAO" class="com.winkee.dao.MessageDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="HibernateServer"
class="com.winkee.server.HibernateServerImp" abstract="false"
lazy-init="default" dependency-check="default">
<property name="messDao">
<ref bean="MessageDAO" />
</property>
<property name="userDao">
<ref bean="UserDAO" />
</property>
</bean>
<bean name="/addMessage"
class="com.winkee.struts.action.MessageAction">
<property name="hibernateServer">
<ref bean="HibernateServer" />
</property>
</bean>
</beans>
第一段红色文字是将hibernate配置文件加入注意路径问题,
第二段红色文字是配置action bean,加入了注入的功能。