最近在学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 ”?
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 ”?
<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>
<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";
}