spring mvc 异常处理

6 篇文章 0 订阅
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
</bean>

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
       <property name="exceptionAttribute" value="ex"></property> <!-- 不指定的话value默认是exception -->
       <property name="exceptionMappings">
        <props>
            <prop key="java.lang.Throwable">error/authExceptionView</prop>
            <!-- 指定YourCustomException异常发生时跳转 -->
            <prop key="com.test.demo.exception.YourCustomException">error/customExceptionView</prop>
        </props>
    </property>
    <property name="defaultErrorView" value="error/genericView"/><!-- 其他异常发生时跳转页面 -->
</bean>


package com.test.demo.exception;
import java.util.Date;

public class YourCustomException extends RuntimeException
{
    private static final long serialVersionUID = 1L;
    
    private Date date;
    private String message;
    
    public YourCustomException(Date date, String message) {
        super();
        this.date = date;
        this.message = message;
    }

    public Date getDate() {
        return date;
    }

    public String getMessage() {
        return message;
    }

    @Override
    public String toString() {
        return "YourCustomException [date=" + date + ", message=" + message + "]";
    }
}


@Controller
@RequestMapping("/test-demo")
public class TestDemoController
{
    @RequestMapping(value="/businessOne", method = RequestMethod.GET)
    public String welcome(Model model)
    {
        throw new YourCustomException(new Date(), "我发生了一个业务错误");
    }
}


采用的是servlet3.0,所以要手动开启使用el表达式(2.4以前的无需手动开始,2.5开始往后的要在页面手动开启isELIgnored=false)
error/customExceptionView.jsp
<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<% page isELIgnored="false"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>error</title>
</head>
<body>
${ex.message}
</body>
</html>


@Controller
@RequestMapping("/test-demo")
public class TestDemoController
{
    @RequestMapping(value="/businessTwo", method = RequestMethod.GET)
    public String welcome2(Model model)
    {
        throw new NullPointerException();
    }
}


error/genericView.jsp
<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<% page isELIgnored="false"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>error</title>
</head>
<body>
发生了一些异常!!
</body>
</html>

遇到的问题:

1.前期以为使用el表达式要引用taglib,在页面使用<%@ taglib prefix="c" uri=http://java.sun.com/jsp/jstl/core %>,结果pom.xml中引入jstl配置
页面老是报The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application异常
以为是没有配置引用jstl jar,经检查后有配置。使用jd-gui打开jstl.jar中的tld文件发现标签引用中uri指定的路径不对,修正uri地址页面不报异常。

2.el表达式不能正常显示,显示为${ex.message}

在页面手动开isELIgnored="false"设置,显示效果达到预期后,干掉了对taglib c的引用


参考:http://www.tuicool.com/articles/FbaMb2

            http://www.open-open.com/lib/view/open1413639779887.html

            http://blog.csdn.net/for_china2012/article/details/9307471

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值