在Springboot中,可以通過修改配置、或者在static文件夾下添加error文件夾引入個性化的404模版。但是如果需要針對不同url地址規則,返回不同樣式的404頁面,則難以實現了。針對這個問題,可以參考如下內容。
例如有兩種類型的url:
/admin開頭的是后台管理,其他url為常規訪問,不考慮安全性的情況下,想返回兩種樣式的404頁面。
Springboot中的錯誤頁面均是由BasicErrorController控制,
繼承BasicErrorController,重寫其中方法即可實現自定義錯誤頁面。
package com.haramasu.daomin2.error;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.Map;
/**
* @Auther: DingShuo
* @Date: 2018/7/23 12:25
* @Description:
*/
@Controller
@RequestMapping(value = "/error")
public class MyBasicErrorController extends BasicErrorController {
Logger logger =LoggerFactory.getLogger(MyBasicErrorController.class);
public MyBasicErrorController(ErrorAttributes errorAttributes, ErrorProperties errorProperties) {
super(errorAttributes, errorProperties);
}
@Override
public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
HttpStatus status = getStatus(request);
Map<String, Object> model = Collections.unmodifiableMap(getErrorAttributes(
request, isIncludeStackTrace(request, MediaType.TEXT_HTML)));
response.setStatus(status.value());
ModelAndView modelAndView = resolveErrorView(request, response, status, model);
if(modelAndView!=null){
return modelAndView;
}else {
String path=model.get("path").toString();
logger.debug("該地址無法訪問:"+path);
if(path.startsWith("/admin/")){
return new ModelAndView("error/adminError", model);
}else {
return new ModelAndView("error/homeError", model);
}
}
}
}
55
1
package com.haramasu.daomin2.error;
2
3
import org.slf4j.Logger;
4
import org.slf4j.LoggerFactory;
5
import org.springframework.boot.autoconfigure.web.ErrorProperties;
6
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
7
import org.springframework.boot.web.servlet.error.ErrorAttributes;
8
import org.springframework.http.HttpStatus;
9
import org.springframework.http.MediaType;
10
import org.springframework.stereotype.Controller;
11
import org.springframework.web.bind.annotation.RequestMapping;
12
import org.springframework.web.servlet.ModelAndView;
13
14
import javax.servlet.http.HttpServletRequest;
15
import javax.servlet.http.HttpServletResponse;
16
import java.util.Collections;
17
import java.util.Map;
18
19
/**
20
* @Auther: DingShuo
21
* @Date: 2018/7/23 12:25
22
* @Description:
23
*/
24
@Controller
25
@RequestMapping(value = "/error")
26
public class MyBasicErrorController extends BasicErrorController {
27
Logger logger =LoggerFactory.getLogger(MyBasicErrorController.class);
28
29
public MyBasicErrorController(ErrorAttributes errorAttributes, ErrorProperties errorProperties) {
30
super(errorAttributes, errorProperties);
31
}
32
33
@Override
34
public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
35
HttpStatus status = getStatus(request);
36
Map<String, Object> model = Collections.unmodifiableMap(getErrorAttributes(
37
request, isIncludeStackTrace(request, MediaType.TEXT_HTML)));
38
response.setStatus(status.value());
39
ModelAndView modelAndView = resolveErrorView(request, response, status, model);
40
if(modelAndView!=null){
41
return modelAndView;
42
}else {
43
String path=model.get("path").toString();
44
logger.debug("該地址無法訪問:"+path);
45
if(path.startsWith("/admin/")){
46
return new ModelAndView("error/adminError", model);
47
}else {
48
return new ModelAndView("error/homeError", model);
49
}
50
51
}
52
}
53
54
}
55
需要注意,繼承BasicErrorController后的構造函數,會提示errorProperties的bean未被初始化。
可以在@Configration注解的類中初始化bean
package com.haramasu.daomin2.conf;
import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Auther: DingShuo
* @Date: 2018/7/23 12:08
* @Description:
*/
@Configuration
public class BeanConfig {
@Bean
public ErrorProperties errorProperties(){
return new ErrorProperties();
}
}
1
20
1
package com.haramasu.daomin2.conf;
2
3
import org.springframework.boot.autoconfigure.web.ErrorProperties;
4
import org.springframework.context.annotation.Bean;
5
import org.springframework.context.annotation.Configuration;
6
7
/**
8
* @Auther: DingShuo
9
* @Date: 2018/7/23 12:08
10
* @Description:
11
*/
12
@Configuration
13
public class BeanConfig {
14
15
@Bean
16
public ErrorProperties errorProperties(){
17
return new ErrorProperties();
18
}
19
}
20
