struts2+spring3整合配置文件实例

时间:2022-12-29 21:42:54

这段时间在研究struts2+spring3整合的框架

下面介绍下我研究加上上网找的一些总结:

1、所需的包有:struts需要的包邮struts-core 为了和spring整合 还需要struts2-spring-plugin;

spring需要导入的包有:core,web,context,beans,asm,expression


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">

<!-- 加载struts2核心 -->
<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>
<!-- 指明spring配置文件在何处 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>//文件多个的时候用’,‘隔开;
</context-param>
<!-- 加载spring配置文件applicationContext.xml -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>


2、配完web.xml我们在在classpath路径下新建一个struts.xml文件,文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">

<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.i18n.encoding" value="utf-8" />


<package name="clf" namespace="/" extends="struts-default">
<action name="login" class="com.clf.action.LoginAction">
<result name="error">/WEB-INF/pages/error.jsp</result>
<result name="success">/WEB-INF/pages/welcome.jsp</result>
</action>

<action name="toLogin" class="com.clf.action.IndexAction"
method="toLogin">
<result name="login">/WEB-INF/pages/login.jsp</result>
</action>
</package>

</struts>

3、接着我们配web。xml中指明的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.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<context:annotation-config />//启动spring的自动扫描

<context:component-scan base-package="com.clf.dao" />//扫描路径下的符合@annotation规则的java类作为javabean

<context:component-scan base-package="com.clf.service" />

<context:property-placeholder location="classpath:jdbc.properties" />//加载数据库连接参数文件
//配置数据库信息
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driverClassName}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean>
//创建sessionfactory
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">//使用自动扫描来加载java,还有一种是使用配置文件xml来加载
<list>
<value>com.clf.entity</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=true
</value>
</property>
</bean>
//配置hibernate的事务处理机制
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />
</beans>

5’最后剩下数据库的文件jdbc.properties

driverClassName=org.gjt.mm.mysql.Driver
url=jdbc:mysql://localhost:3306/div
username=root
password=123456