@EnableWebMvc 破坏了自动配置提供的默认映射,因此我添加了 InternalResourceViewResolver ,但这无助于获取视图。当我输入:
http://localhost:8090/logIn
我在控制台中收到此消息:
No mapping for GET /resources/views/home.jsp
也许DispatcherServlet
映射到/*
,如果是,如何将嵌入式/
的映射更改为dispatcherServlet
。
项目结构:
main
+- java
| +- app
| +- Application.java
| +- web
| +- LogInController.java
| +- config
| +- ViewResolverConfig.java
+- resources
+- views
+- home.jsp
+- application.properties
Application.java
@SpringBootApplication
@Import(ViewResolverConfig.class)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}
ViewResolverConfig.java
@Configuration
@EnableWebMvc
public class ViewResolverConfig implements WebMvcConfigurer {
@Bean
public ViewResolver internalResourceViewResolver() {
InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setViewClass(JstlView.class);
bean.setPrefix("resources/views/");
bean.setSuffix(".jsp");
return bean;
}
}
LogInController.java
@Controller
@RequestMapping("/logIn")
public class LogInController {
@GetMapping
public String enterPage(Model model){
return "home";
}
}