我已经开始学习Spring,并创建了简单的Spring MVC应用程序。这是我的配置和控制器:
SpringCongig:
@Configuration
@EnableWebMvc
@ComponentScan("exhibition")
public class SpringConfig implements WebMvcConfigurer {
private final ApplicationContext applicationContext;
@Autowired
public SpringConfig(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Bean
public SpringResourceTemplateResolver templateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setApplicationContext(applicationContext);
templateResolver.setPrefix("/WEB-INF/views/");
templateResolver.setSuffix(".html");
return templateResolver;
}
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
templateEngine.setEnableSpringELCompiler(true);
return templateEngine;
}
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
registry.viewResolver(resolver);
}
}
SpringServletInitializer:
public class SpringServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] {SpringConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[] {"/"};
}
}
控制器:
@Controller
@RequestMapping("/halls")
public class HallController {
@GetMapping()
public String index(){
return "index";
}
}
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>halls</title>
</head>
<body>
<h2>
Hello world!
</h2>
</body>
</html>
但是当我跟踪“/霍尔斯”时,我总是得到404错误。但早些时候,当我使用相同的配置,一切都很好。idk发生了什么事。服务器设置是相同的,IDE也是。我用的是Tomcat 9.0.65。
2条答案
按热度按时间d7v8vwbk1#
尝试在请求URL的末尾添加'/'
3hvapo4f2#
尝试在应用程序启动时在日志中列出所有注册的请求Map。