SpringMVC异常处理之404及500的处理

跟杨春娟学SpringMVC笔记:SpringMVC异常处理之404及500的处理

完成:第一遍

1.如何实现SpringMVC异常处理之404及500的处理?

先在src/main/webapp/WEB-INF/pages/error下新建两个页面404.jsp和500.jsp

404.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>This is 404 errorPage</h2>
</body>
</html>

500.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>This is 500 errorPage</h2>
</body>
</html>

第一种方式:直接定位JSP页面
在web.xml里直接加error-page标签

    <error-page>
       <error-code>404</error-code>
       <!--第一种方式:直接定位404.jsp页面  -->
       <location>/WEB-INF/pages/error/404.jsp</location>
     </error-page>
     <error-page>
       <error-code>500</error-code>
       <!--第一种方式:直接定位404.jsp页面  -->
       <location>/WEB-INF/pages/error/500.jsp</location>
    </error-page>

第二种方式:controller跳转404.jsp或500.jsp
步骤一:在web.xml里直接加error-page标签


    <error-page>
       <error-code>404</error-code>
       <location>/error/404Page</location>
     </error-page>
     <error-page>
       <error-code>500</error-code>
       <location>/error/500Page</location>
    </error-page>

步骤二:需要写一个新的Controller类

ErrorController类

package com.adbycool.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("error")
public class ErrorController {

	@RequestMapping("404Page")
	public String error404() {	
	//pages下有个error包
		return "error/404";
	}
	
	@RequestMapping("500Page")
	public String error500() {	
		return "error/500";
	}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值