[英]Spring MVC TomCat configuration to run jsp and html pages
在 Eclipse 中运行 TomCat。 我在浏览器中输入 http://localhost:8080/springmvc/index.jsp 并看到“Hello World.”。
但是如果我输入 http://localhost:8080/springmvc/hello,我会看到 404 错误
我究竟做错了什么?
我的 Controller
@Controller
public class WebController {
@GetMapping("/hello")
public String hello() {
return "hello";
}
}
我的配置
@Configuration
@ComponentScan("ru.neutrino")
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
private final ApplicationContext applicationContext;
@Autowired
public WebConfig(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);
}
}
您在 controller class 级别缺少 @RequestMapping 注释。
@Controller
@RequestMapping("/web")
public class WebController {
@GetMapping("/hello")
public String hello() {
return "hello";
}
}
尝试访问 http://localhost:8080/springmvc/web/hello
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.