关于返回文件类型为.page时异常Exception返回页面的问题
来源:6-3 接口请求全局异常处理-设计与验证

Lqs阿木
2019-03-27
页面异常信息:
HTTP Status 404 - /pagesException.jsp
type Status report
message /pagesException.jsp
description The requested resource is not available.
Apache Tomcat/7.0.85
代码如下:
TextController类
@RequestMapping("/hello.page")
@ResponseBody
public JsonData hello(){
logger.info("info");
throw new RuntimeException();
// return JsonData.success("hello,permission");
}
}
SpringExceptionResolver类:
public class SpringExceptionResolver implements HandlerExceptionResolver{
private static org.slf4j.Logger Logger = LoggerFactory.getLogger(SpringExceptionResolver.class);
/*
*
* @param request:获取客服端的请求全路径
*
* @param ModelAndView:存储异常页面或者数据反馈到客服端
*
* @class Logger:异常日志以及异常发生的路径
* */
@Override
public ModelAndView resolveException(HttpServletRequest request,
HttpServletResponse response,
Object handle, Exception e) {
//获取请求全网路径;
String url = request.getRequestURL().toString();
ModelAndView modelAndView;
String defaultMsg = "System error";
//只允许返回类型为.json;
if(url.endsWith(".json")){
//当所抓取的异常是自己定义的异常类型;
if (e instanceof PermissionException){
JsonData result = JsonData.fail(e.getMessage());
modelAndView = new ModelAndView("jsonView", result.toMap());
//当所抓取的异常不是自己定义的异常类型;
}else {
Logger.error("unknow jsonException,url:"+url,e);
JsonData result = JsonData.fail(defaultMsg);
modelAndView = new ModelAndView("jsonView",result.toMap());
}
//只允许返回的类型为.page;
}else if (url.endsWith(".page")){
Logger.error("unknow exception,url:"+url,e);
JsonData result = JsonData.fail(defaultMsg);
modelAndView = new ModelAndView("Exception",result.toMap());
//当返回的类型既不是.json也不是.page结尾的文件类型做全局处理;
}else {
Logger.error("unknow Exception,url:"+url,e);
JsonData result = JsonData.fail(defaultMsg);
modelAndView = new ModelAndView("jsonView",result.toMap());
}
return modelAndView;
}
为甚么返回不是exception.jsp的页面而是pageException.jsp页面;
3回答
-
Jimin
2019-03-28
配置还真的有问题。
<property name="prefix" value="/WEB-INF/pages"/>
这里value应该是/WEB-INF/pages/,你差了最后一个/,因此要找的页面就前面就多了pages,而且少了一层路径。这里本质上是拼上代码里给的页面名称,最终组成一个路径下的一个页面。
另外,课程里使用的是views,路径和你的pages不同,因此你本地寻找的路径和课程里的是不一样的,注意好区别00 -
Lqs阿木
提问者
2019-03-28
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--onfiguration : Annotations to take effect-->
<context:annotation-config/>
<!--SpringMvc:Start the annotation function -->
<mvc:annotation-driven/>
<!-- Start package scanning -->
<context:component-scan base-package="com.mmall.controller"/>
<context:component-scan base-package="com.mmall.service"/>
<!--RequestMappingHandlerMapping: Maybe,tell us that the annotation which interface to handle the request-->
<!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>-->
<bean class="com.mmall.common.SpringExceptionResolver"/>
<!-- BeanNameViewResolver:Specify the type of return file-->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
<!-- MappingJackson2JsonView:Use this class when returning the file format to json-->
<bean id="jsonView" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>
<!-- InternalResourceViewResolver:Use this class when returning the file format to jsp-->
<!-- View resolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
上述问题已经纠正,这是spring-servlet.xml的内容,返回前端页面仍然是pageexception.jsp
00 -
Jimin
2019-03-27
你好,从你的代码里我发现了几个问题,你看一下:
1、/hello.page 这个请求如果是期望返回页面,而不是json数据的话,不应该添加 @ResponseBody 这个注解
2、modelAndView = new ModelAndView("Exception",result.toMap()); 这里不应该是 Exception而应该是 exception,这样按照课程里的逻辑才会去找 exception.jsp
3、暂时没看到你对页面视图配置的代码(spring-servlet.xml里),我猜测是和课程里使用的有些出入,导致寻找的页面不符合预期
012019-03-28
相似问题