Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 处理异常时从Spring返回ModelAndView_Java_Spring_Exception Handling_Servlet 3.0_Modelandview - Fatal编程技术网

Java 处理异常时从Spring返回ModelAndView

Java 处理异常时从Spring返回ModelAndView,java,spring,exception-handling,servlet-3.0,modelandview,Java,Spring,Exception Handling,Servlet 3.0,Modelandview,在spring项目中处理异常处理 我已经按照规定处理了例外情况 一切都进行得很顺利,但我希望返回ModelAndView,而不是使用PrintWriter对象编写文本/html 以下是我采用的方法 ExceptionHandler.java package com.test.controller; 导入java.io.IOException; 导入javax.servlet.ServletException; 导入javax.servlet.annotation.WebServlet; 导入ja

在spring项目中处理异常处理

我已经按照规定处理了例外情况

一切都进行得很顺利,但我希望返回ModelAndView,而不是使用PrintWriter对象编写文本/html

以下是我采用的方法

ExceptionHandler.java

package com.test.controller;
导入java.io.IOException;
导入javax.servlet.ServletException;
导入javax.servlet.annotation.WebServlet;
导入javax.servlet.http.HttpServlet;
导入javax.servlet.http.HttpServletRequest;
导入javax.servlet.http.HttpServletResponse;
导入org.springframework.web.servlet.ModelAndView;
/**
*Servlet实现类ExceptionHandler
*/
@WebServlet(“/ExceptionHandler”)
公共类ExceptionHandler扩展了HttpServlet{
私有静态最终长serialVersionUID=1L;
/**
*@参见HttpServlet#doGet(HttpServletRequest请求,HttpServletResponse响应)
*/
受保护的void doGet(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
//TODO自动生成的方法存根
//response.getWriter().append(“服务于:”).append(request.getContextPath());
//processError(请求、响应);
handleException(请求、响应);
}
/**
*@请参阅HttpServlet#doPost(HttpServletRequest请求,HttpServletResponse响应)
*/
受保护的void doPost(HttpServletRequest请求、HttpServletResponse响应)引发ServletException、IOException{
//TODO自动生成的方法存根
//processError(请求、响应);
handleException(请求、响应);
}
//我希望返回此ModelAndView,并显示在ModelAndView对象中设置的页面。
私有ModelAndView handleException(HttpServletRequest请求、HttpServletResponse响应)引发IOException{
ModelAndView模型=新的ModelAndView(“错误页面”);
addObject(“errMsg”,“这是Exception.class”);
收益模型;
}
//我不想要这种方法。
/*private void processError(HttpServletRequest请求,
HttpServletResponse(响应)引发IOException{
//分析servlet异常
Throwable-Throwable=(Throwable)请求
.getAttribute(“javax.servlet.error.exception”);
整数状态码=(整数)请求
.getAttribute(“javax.servlet.error.status_code”);
字符串servletName=(字符串)请求
.getAttribute(“javax.servlet.error.servlet_name”);
if(servletName==null){
servletName=“未知”;
}
字符串请求URI=(字符串)请求
.getAttribute(“javax.servlet.error.request_uri”);
if(requestUri==null){
requestUri=“未知”;
}
//设置响应内容类型
response.setContentType(“text/html”);
PrintWriter out=response.getWriter();
填写(“例外/错误详细信息”);
如果(状态代码!=500){
输出。写入(“错误详细信息”);
输出。写入(“状态代码”:“+statusCode+”
”; out.write(“请求的URI:”+requestUri); }否则{ 填写(“例外情况详情”); out.写出(“
  • Servlet名称:“+servletName+”
  • ”); out.write(“
  • 异常名称:“+throwable.getClass().getName()+”
  • ”; out.write(“
  • 请求的URI:+requestUri+”
  • ”; out.write(“
  • 异常消息:“+throwable.getMessage()+”
  • ”; 写出“
”; } 写出(“

”); 请写出(“”); 请写出(“”); }*/ }
在web.xml中,我添加了以下标记来设置错误的位置

 <error-page>
 <error-code>404</error-code>
 <location>/ExceptionHandler</location>
 </error-page>

404
/例外处理程序
编辑:我知道void不能返回ModelAndView对象,但是正在寻找一种方法来在URL无效的情况下加载ErrorPage

 <error-page>
 <error-code>404</error-code>
 <location>/ExceptionHandler</location>
 </error-page>