struts2中异常处理(demo)

关于Struts2处理全局异常和局部异常:

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>
	<!--
		1、禁用动态方法调用
		,这些参数的默认值可以在struts2-core-2.1.8.1.jar/org.apache.struts2/default.properties中查看
	-->
	<constant name="struts.enable.DynamicMethodInvocation" value="true" />
	<!-- 2、struts.devMode是否设置为开发模式 -->
	<constant name="struts.devMode" value="true" />


	<package name="test" namespace="/test" extends="struts-default">
	
		<!--3、全局返回结果  -->
		<global-results>
			<result name="globalException">/WEB-INF/pages/globalException.jsp</result>
		</global-results>
		<!-- 4、全局异常 -->
		<global-exception-mappings>
			<exception-mapping result="globalException" exception="java.lang.Exception"></exception-mapping>
		</global-exception-mappings>


		<action name="exceptionaction" class="com.demo.ExceptionAction">
		 	<!--5、配置局部的IOException  -->
			<exception-mapping result="partException" exception="java.io.IOException"></exception-mapping>
			<!--6、测试返回 ftl 类型的页面 -->
			<result name="success" type="freemarker">/WEB-INF/pages/success.ftl</result>
			<result name="partException">/WEB-INF/pages/partexception.jsp</result>
		</action>	
			<action name="*">
			<result>/{1}.jsp</result>
		</action>
	</package>

</struts>

 参数详解:

1、<global-result>标签:返回全全局通用的展现的视图,或者当出现异常是的处理途径

2、<exception-mapping>标签:result表示需要返回的结果视图,exception:需要捕获的异常类型,当Action中抛出异 常异常时在局部无法处理异常的时候就寻找全局异常来捕获该异常。

3、<result>标签中type属性可以设置返回页面的类型,在该处返回类型为.ftl

   自己做的小demo,先存起来以后兴许会用到吧,希望朋友们多多指教!!!

猜你喜欢

转载自yuxisanren.iteye.com/blog/1707508
今日推荐