我改变了我完美工作的控制器类,它只是为了实现视图分辨率,如下所示:
@Controller
public class MyController {
@GetMapping("/signup")
public String signupPage() {
return "signup";
}
@GetMapping("/login")
public String loginPage() {
return "login";
}
@GetMapping("/dashboard")
public String dashboardPage() {
return "dashboard";
}
@GetMapping("/logout")
public String logoutPage() {
return "redirect:/";
}
}
对于扩展 WebMvcConfigurer 的类,它具有如下所示的所有视图解析器:
@Configuration
@EnableWebMvc
public class ViewConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/signup").setViewName("signup");
registry.addViewController("/login").setViewName("login");
registry.addViewController("/dashboard").setViewName("dashboard");
registry.addViewController("/logout").setViewName("redirect:/");
}
这感觉更加简洁,干净。
但是,每当我尝试加载这些页面中的任何一个时,这都会给我 405 Method Not Allowed 错误。为什么会这样?spring boot 不支持 WebMvcConfigurer 吗?
这在Spring Boot Documentation 中提到,在 spring mvc 部分下你可以使用 WebMvcConfigurer,但你不需要做 @EnableWebMvc
所以你应该删除@EnableWebMvc 注释!
@Configuration
// @EnableWebMvc Remove this!
public class ViewConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/signup").setViewName("signup");
registry.addViewController("/login").setViewName("login");
registry.addViewController("/dashboard").setViewName("dashboard");
registry.addViewController("/logout").setViewName("redirect:/");
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句