spring+struts2.0整合

本文介绍了如何在项目中配置Spring与Struts框架的整合,包括web.xml的配置细节、Struts.xml的设置以及错误页面的统一管理。通过具体示例展示了如何加载Spring配置文件、配置监听器和过滤器,并在Struts中定义全局结果和异常映射。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spring配置文件都放到src/conf/spring目录中,struts.xml放在src目录下
1、web.xml配置如下

<context-param>
<param-name>webAppRootKey</param-name>
<param-value>b2b.root</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/spring/spring*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>false</param-value> <!--设置成false,才能保证在jsp页面调用Bean中的lazy对象时不会出错-->
</init-param>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


修改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>

<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
<constant name="struts.devMode" value="true"/>
<constant name="struts.i18n.encoding" value="UTF8"/>

<package name="default" namespace="/" extends="struts-default">
<global-results>
<result name="businessError">/pages/errors/BusinessErrorPage.jsp</result>
<result name="dbError">/pages/errors/DBErrorPage.jsp</result>
<result name="httpError">/pages/errors/HTTPErrorPage.jsp</result>
<result name="permissionError">/pages/errors/PermissionErrorPage.jsp</result>
<!-- 当Session失效时,统一转到这个页面 -->
<result name="login" type="redirectAction">Login_input</result>
<result name="json" type="json"></result>
<result name="error">/pages/bp/errorPage.jsp</result>
</global-results>

<global-exception-mappings>
<exception-mapping result="businessError" exception="XXXException"></exception-mapping>
<exception-mapping result="httpError" exception="XXXXException"></exception-mapping>
</global-exception-mappings>

</package>
<include file="struts_*.xml"/>
</struts>


所有action类都继承 com.opensymphony.xwork2.ActionSupport
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值