struts2中处理自定义异常的问题,求大神帮忙

先上代码:
struts.xml

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
    "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
    <!-- 请求参数的编码方式 -->
    <constant name="struts.i18n.encoding" value="UTF-8" />
    <!-- 当struts.xml改动后,是否重新加载。默认值为false(生产环境下使用),开发阶段最好打开 -->
    <constant name="struts.configuration.xml.reload" value="true" />
    <!-- 设置浏览器是否缓存静态内容。默认值为true(生产环境下使用),开发阶段最好关闭 -->
    <constant name="struts.serve.static.browserCache" value="false" />
    <!-- 将struts2委托Spring管理 -->
    <constant name="struts.objectFactory" value="spring" />
    <!-- 是否开启动态方法调用 -->
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <!-- 是否使用struts的开发模式。开发模式会有更多的调试信息。默认值为false(生产环境下使用),开发阶段最好打开 -->
    <constant name="struts.devMode" value="true" />
    <!-- 设置默认的父包,一般我们都设置一个default包继承自struts-default。大部分类再继承default。如果有特殊的类需要特殊的包,只能在action中再指定父包了。 -->
    <constant name="struts.convention.default.parent.package"
        value="default" />

    <!-- 默认配置包路径包含action,actions,struts,struts2的所有包都会被struts作为含有Action类的路径来搜索。通过设置struts.convention.package.locators属性来修改这个配置。 -->
    <constant name="struts.convention.package.locators" value="action,actions,controller,web" />
    <!-- 约定Action类名:要求Action的命名必须以Action为后缀,可以使用下面常量来修改后缀限制 -->
    <constant name="struts.convention.action.suffix" value="Action" />


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

        <global-results>
            <!-- <result name="error1">/commons/error1.jsp</result> -->
            <result name="error2">/commons/error2.jsp</result>
            <result name="login">/login/login.jsp</result>
        </global-results>

        <global-exception-mappings>
            <!-- <exception-mapping result="error1" exception="java.lang.Exception"/> --> 
            **<exception-mapping result="error2" exception="com.lz.shop.web.action.MyException" />**
            <exception-mapping result="sqlException" exception="java.sql.SQLException" />

        </global-exception-mappings>

    </package>


</struts>


这一句为什么会无效呀?试了很多办法都不行

求大神解答!

自定义异常类:
MyException.java

 package com.lz.shop.web.action;

public class MyException extends Exception {

    /**
     * 
     */
    private static final long serialVersionUID = -4567535714727601736L;

    public MyException() {
        super();
    }

    private String message="bmddddd";

    public MyException(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }
}

error2.jsp

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>ERROR1</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

</head>

<body>
    <span>ERROR2</span>
    <s:fielderror></s:fielderror>
    <s:property value="exception.message" />
</body>
</html>

Action测试类:

LoginAction.java

package com.lz.shop.web.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.lz.shop.entity.base.User;
import com.lz.shop.web.BaseSupport;

@Scope
@Controller
@Namespace("/user")
@ParentPackage("default")
public class LoginAction extends BaseSupport {

    /**
     * 
     */
    private static final long serialVersionUID = 6336185736252504861L;

    private User user;

    /**
     * 验证登陆
     * 
     * @return
     */
    @Action(value = "loginAction", results = {
            @Result(name = "success", location = "/index.jsp", type = "redirect"),
            @Result(name = "input", location = "/login/login.jsp") })
    public String login() {
//抛个空指针异常来测试 
        if (user.getUsername() == "1"
                && user.getPassword() == "1") {
            System.out.println("success");
            return "success";
        } else {
            System.out.println("input");
            return "input";
        }
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

}

struts2版本是2.2.1
求大神支招!!!

2个回答


这一句怎么弄也没有效果,求大神解答!

 <exception-mapping result="error2" exception="com.lz.shop.web.action.MyException" />

这一句!

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!