前天,帮同学写程序。他们公司用struts2.0,而我也没用过struts2,索性借此学习一下struts2吧。发现struts2结合spring搭建,不像struts那样要在struts配置spring插件,貌似已经在filter做好了。先将配置文件展出,供初学者参考。
1.web.xml
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
2.spring.xml
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/temp</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>yoyi</value>
</property>
</bean>
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 加载数据源 -->
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<!-- hibernate相应信息配置 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">
true
</prop>
</props>
</property>
<!-- 配置pojo中*.hbm.xml加载 -->
<property name="mappingResources">
<list>
<value>com/bookmanager/LendBook/pojo/Book.hbm.xml</value>
<value>com/bookmanager/LendBook/pojo/LendBook.hbm.xml</value>
<value>com/bookmanager/LendBook/pojo/User.hbm.xml</value>
</list>
</property>
</bean>
<!-- 配置spring管理hibernate模板 -->
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- DAO注入配置 -->
<bean id="lendBookDAO" class="com.bookmanager.LendBook.dao.LendBookDAO" abstract="true"/>
<bean id="lendBookDAOImpl" class="com.bookmanager.LendBook.dao.impl.LendBookDAOImpl" parent="lendBookDAO">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate"/>
</property>
</bean>
<!-- Service注入配置 -->
<bean id="lendBookService" class=" com.bookmanager.LendBook.service.LendBookService" abstract="true"/>
<bean id="lendBookServiceImpl" class="com.bookmanager.LendBook.service.impl.LendBookServiceImpl" parent="lendBookService">
<property name="lendBookDAOImpl">
<ref bean="lendBookDAOImpl"/>
</property>
</bean>
<!-- 登陆 -->
<bean name="loginAction" class="com.bookmanager.LendBook.action.LoginAction">
<property name="lendBookServiceImpl">
<ref bean="lendBookServiceImpl"/>
</property>
</bean>
3.struts.xml
<constant name="struts.devMode" value="true"></constant>
<package name="struts2" extends="struts-default">
<!-- action交给spring管理,class对应spring配置中的bean ,制定method后action可以不必继承ActionSupport-->
<action name="login" class="loginAction" method="login">
<result name="success">/jsp/index.jsp</result>
<result name="error">/jsp/error.jsp</result>
</action>
</package>
本文详细介绍了如何使用Struts2与Spring框架进行整合,包括配置文件展示、Spring核心配置、Struts2配置以及相关组件的配置。通过实例演示,帮助初学者快速上手。

被折叠的 条评论
为什么被折叠?



