Spring MVC 调度程序无法找到控制器处理程序方法
Spring MVC Dispatcher unable to find controller handler method
我遇到了有史以来最奇怪的错误。在我的日志中,它验证我已经为 "TrainingRequest"
创建了一个映射
018-10-23 21:58:43,900 [main] [INFO] o.s.w.s.m.m.a.RequestMappingHandlerMapping - 映射“{[/TrainingRequest],methods=[GET]}”
但是当我尝试访问页面 http://localhost:8080/WEB/TrainingRequest 时,
我收到 404 页面,日志显示调度程序无法找到映射。如下所示
2018-10-23 22:00:17,129 [http-nio-8080-exec-1] [DEBUG] o.s.w.s.m.m.a.RequestMappingHandlerMapping - 未找到 [/TrainingRequest] 的处理程序方法
2018-10-23 22:00:17,132 [http-nio-8080-exec-1] [WARN] o.s.web.servlet.PageNotFound - 在 DispatcherServlet[ 中找不到带有 URI [/WEB/TrainingRequest] 的 HTTP 请求映射
http://localhost:8080/WEB/ works properly, so I doubt it is an issue of configuration. However, here are the snippets below. If you think the issue could be file structure, here is my github link
WebConfig.Java
@EnableWebMvc
@Configuration
@ComponentScan(basePackages = "com.project.LNUProject.config")
@Slf4j
public class WebConfig implements WebMvcConfigurer {
// == constants ==
public static final String RESOLVER_PREFIX = "/WEB-INF/view/";
public static final String RESOLVER_SUFFIX =".jsp";
// == bean methods
@Bean
public ViewResolver viewResolver() {
UrlBasedViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix(RESOLVER_PREFIX);
viewResolver.setSuffix(RESOLVER_SUFFIX);
return viewResolver;
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
log.info("registry add properly");
registry.addViewController("/").setViewName(ViewNames.HOME);
}
}
ConstantsController.java
@Controller
@Slf4j
public class ConstantsController {
// == handler methods ==
// http://localhost:8080/WEB/
@GetMapping("/")
public String home() {
log.info("Home template map");
return ViewNames.HOME;
}
}
RequestTraining.java
@Controller
@Slf4j
public class RequestTrainingController {
final String NAME = "TrainingRequest";
// == handler methods ==
// http://localhost:8080/WEB/TrainingRequest
@GetMapping(NAME)
public String TableView() {
log.info("Training VIEW being called");
return NAME;
}
@GetMapping(Mappings.HOME + NAME + Mappings.EDIT)
public String EditTableView() {
return NAME + Mappings.EDIT;
}
@GetMapping(Mappings.HOME + NAME + Mappings.DELETE)
public String DeleteTableView() {
return NAME + Mappings.DELETE;
}
@GetMapping(Mappings.HOME + NAME + Mappings.ADD)
public String AddTableView() {
return NAME + Mappings.ADD;
}
}
在您的 addViewController
方法中缺少 TrainingRequest
的映射
做这样的事情
@Override
public void addViewControllers(ViewControllerRegistry registry) {
log.info("registry add properly");
registry.addViewController("/").setViewName(ViewNames.HOME);
registry.addViewController("/TrainingRequest").setViewName("TrainingRequest");
}
我遇到了有史以来最奇怪的错误。在我的日志中,它验证我已经为 "TrainingRequest"
创建了一个映射018-10-23 21:58:43,900 [main] [INFO] o.s.w.s.m.m.a.RequestMappingHandlerMapping - 映射“{[/TrainingRequest],methods=[GET]}”
但是当我尝试访问页面 http://localhost:8080/WEB/TrainingRequest 时, 我收到 404 页面,日志显示调度程序无法找到映射。如下所示
2018-10-23 22:00:17,129 [http-nio-8080-exec-1] [DEBUG] o.s.w.s.m.m.a.RequestMappingHandlerMapping - 未找到 [/TrainingRequest] 的处理程序方法 2018-10-23 22:00:17,132 [http-nio-8080-exec-1] [WARN] o.s.web.servlet.PageNotFound - 在 DispatcherServlet[ 中找不到带有 URI [/WEB/TrainingRequest] 的 HTTP 请求映射
http://localhost:8080/WEB/ works properly, so I doubt it is an issue of configuration. However, here are the snippets below. If you think the issue could be file structure, here is my github link
WebConfig.Java
@EnableWebMvc
@Configuration
@ComponentScan(basePackages = "com.project.LNUProject.config")
@Slf4j
public class WebConfig implements WebMvcConfigurer {
// == constants ==
public static final String RESOLVER_PREFIX = "/WEB-INF/view/";
public static final String RESOLVER_SUFFIX =".jsp";
// == bean methods
@Bean
public ViewResolver viewResolver() {
UrlBasedViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix(RESOLVER_PREFIX);
viewResolver.setSuffix(RESOLVER_SUFFIX);
return viewResolver;
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
log.info("registry add properly");
registry.addViewController("/").setViewName(ViewNames.HOME);
}
}
ConstantsController.java
@Controller
@Slf4j
public class ConstantsController {
// == handler methods ==
// http://localhost:8080/WEB/
@GetMapping("/")
public String home() {
log.info("Home template map");
return ViewNames.HOME;
}
}
RequestTraining.java
@Controller
@Slf4j
public class RequestTrainingController {
final String NAME = "TrainingRequest";
// == handler methods ==
// http://localhost:8080/WEB/TrainingRequest
@GetMapping(NAME)
public String TableView() {
log.info("Training VIEW being called");
return NAME;
}
@GetMapping(Mappings.HOME + NAME + Mappings.EDIT)
public String EditTableView() {
return NAME + Mappings.EDIT;
}
@GetMapping(Mappings.HOME + NAME + Mappings.DELETE)
public String DeleteTableView() {
return NAME + Mappings.DELETE;
}
@GetMapping(Mappings.HOME + NAME + Mappings.ADD)
public String AddTableView() {
return NAME + Mappings.ADD;
}
}
在您的 addViewController
方法中缺少 TrainingRequest
的映射
做这样的事情
@Override
public void addViewControllers(ViewControllerRegistry registry) {
log.info("registry add properly");
registry.addViewController("/").setViewName(ViewNames.HOME);
registry.addViewController("/TrainingRequest").setViewName("TrainingRequest");
}