Spring:默认的html错误页面没有正确显示?

时间:2017-10-10 08:07:56

标签: java spring spring-mvc error-handling custom-error-pages

我想在404,406等错误时将我的网站重定向到默认错误页面。我已经按照一个示例在控制器中编写了以下方法。

@Controller
public class HomeController {
private static final String PATH = "/error-page";

        public String getErrorPath() {
            return PATH;
        }

        @RequestMapping(method=RequestMethod.GET, value=PATH, produces="text/plain")
        @ResponseBody
        public Map<String, Object> handleGet(HttpServletRequest request) {

            Map<String, Object> map = new HashMap<String, Object>();
            map.put("status", request.getAttribute("javax.servlet.error.status_code"));
            map.put("reason", request.getAttribute("javax.servlet.error.message"));

            return map;
        }

        @RequestMapping(method=RequestMethod.POST, value=PATH, produces="text/plain")
        @ResponseBody
        public Map<String, Object> handlePost(HttpServletRequest request) {

            Map<String, Object> map = new HashMap<String, Object>();
            map.put("status", request.getAttribute("javax.servlet.error.status_code"));
            map.put("reason", request.getAttribute("javax.servlet.error.message"));

            return map;
        }
}

在上述路径中有一个html页面为error-page.html。虽然我没有错误,但是在收到错误时我无法显示此错误页面。而是显示通常的错误消息。这是什么问题?

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

你必须重定向到其他控制器
例如 NSMutableString *message = [NSMutableString string]; NSString *title = [dictionary valueForKey:@"REVIEW"]; [message appendString:title]; [message appendString:@"\n"]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Review" message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alert setValue:[self getLabelWithMessage:message withTitle:title] forKey:@"accessoryView"]; [alert show];
  在那个特定的控制器之后你应该编写代码来加载require jsp页面。

[alert setValue:[self getLabelWithMessage:message  withTitle:title] forKey:@"accessoryView"];

在你的案例中,我没有看到任何捕捉错误的方法,你怎么有这个方法

return "redirect:/controllerPath"

如果在错误后的某个时刻调用此方法,请执行此操作

@RequestMapping("/controllerPath")
public String loadErrorPage(){
 return "errorPage";
}

希望这会有所帮助。

答案 1 :(得分:1)

我不知道是否显示&#34; ...通常会显示错误消息&#34;你的意思是白标错误页面。但是如果你想要停用它并显示你自己的错误页面,你必须放入application.properties或yml:

var resultsCount = 30; var sampleSize = 10; // Create a random set of indexes var rnd = new Random(); var indexes = Enumerable.Range(0, resultsCount-1); var randomSet = indexes.OrderBy(r => rnd.NextDouble()).Take(sampleSize).ToList(); randomSet.OrderBy(r => r).Dump("Random set of indexes"); // Simulated resultset // for convenience simulate db items from index set var db = indexes.Select(x => "Result" + x.ToString()); // The query var sampleOfResults = db.Zip(indexes, (x, y)=> Tuple.Create(x,y)) .Where(tuple => randomSet.Any(r => r == tuple.Item2)) .Take(sampleSize) // for good measure, finish when all samples taken .Select(t => t.Item1); // clear the indexes sampleOfResults.ToList().Dump("Sample");

然后像这样添加server.error.whitelabel.enabled=false

ErrorController