当前位置:编程学习 > JAVA >>

struts2异常捕获映射 为什么不行?

最近在学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>
<constant name="struts.devMode" value="false" />
    <constant name="struts.multipart.saveDir" value="/upload"/>
    <constant name="struts.multipart.maxSize" value="1000000000"/>
    <constant name="struts.custom.i18n.encoding" value="UTF-8"/>
    <constant name="struts.i18n.encoding" value="UTF-8" />
    <constant name="struts.custom.i18n.resources" value="messageResource"></constant>

 <package name="companylogin" extends="struts-default">   
 

     <!-- 定义全局结果映射 -->
      <global-results>  
     <!-- 定义两个逻辑异常都对应错误页面error.jsp -->
     <result name="aaa">/error.jsp</result>
     <result name="bbb">/error.jsp</result>
    </global-results>
     
     <!-- 定义全局异常映射 -->
     <global-exception-mappings>
     <!-- 当action中遇到SQLException异常时,系统转入name为sql的结果中 -->
     <exception-mapping result="aaa" exception="com.cxrc.tools.EpmoException" ></exception-mapping>
     <!-- 当action中遇到Exception异常时,系统转入name为root的结果中 -->
     <exception-mapping result="bbb" exception="java.lang.Exception" ></exception-mapping>
     </global-exception-mappings>
     
     

    </package>
</struts>




可是为什么在action中
if(1==1)  
 {  
 throw new Exception("用户名或密码错误,您发现了吧!");  
 }  

抛出异常后没有到“/error.jsp ”?
--------------------编程问答-------------------- 没有配Exception有捕捉吧? --------------------编程问答-------------------- 在web.xml文件中加上
<error-page>
     <error-code>500</error-code>
     <location>error.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>error.jsp</location>
</error-page>
--------------------编程问答-------------------- --------------------编程问答-------------------- error.jsp是否存在,文件位置是否与映射匹配?
--------------------编程问答--------------------
2. <package name="bbs2009-error" extends="struts-default">
    <global-results>
     <result name="error">/admin/error.jsp</result>
    </global-results>
    <global-exception-mappings>
<exception-mapping result="error" exception="Exception"></exception-mapping>
    </global-exception-mappings>

 </package>
3. <package name="admin" namespace="/admin" extends="bbs2009-error" >

你的action包肯定没继承错误包 --------------------编程问答-------------------- 找到没有啊
--------------------编程问答-------------------- 楼上的说的好像都不太对 
1楼 你所谓的没有捕捉是什么意思?

2楼 如果写成这样,不用struts配置就可以实现

4楼 error.jsp路径绝对没有问题

5楼 麻烦仔细看些我贴的代码 继承自 extends="struts-default">   
--------------------编程问答-------------------- 抛出异常的代码:
public String clogin()throws Exception {
if(1==1)  
   {  
     throw new Exception("用户名或密码错误,11111111111!");
   }
return "1";
}
   

--------------------编程问答-------------------- 5楼的正解,就是因为你定义的异常映射在companylogin包下,所以必须继承这个包,而不是继承struts-default
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,