我正在做一个 Spring 启动项目
我想把它转换成spring web mvc
我尝试使用将其转换为动态 Web 模块 3.0
我为调度程序 servlet 添加了 WebAppInitializer
和 View 解析器
但是当我部署 war 时,它会成功部署,但我得到 HTTP 404
错误
还有什么我需要做的吗?
最佳答案
首先,Spring boot 是 Spring MVC 和其他 Spring 模块的包装器。
如果您已经在使用 Spring Boot 并且想要添加 Web 模块(Spring MVC),您只需输入您的 POM 文件并添加:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
这将创建一个 Spring MVC 应用程序,现在 View 解析器怎么样?好吧,当我使用 Spring MVC 编写代码时,Spring 会管理这个问题,我实现 Thymeleaf 以执行 View ,所以我还添加:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
此 View (html) 保存在文件夹资源/模板中
和 thadaaaaaa 我在 Controller 中唯一做的就是将字符串返回到该模板,例如
@GetMapping("/detail/{id}")
private String publicationDetail(@PathVariable Integer id, Model model) {
Optional<CatalogEntity> product = catalogService.getProducById(id);
model.addAttribute("product",product.get());
counter(model);
return "/catalog/detail";
}
但是如果您不使用 Thymeleaf 它也应该可以返回 View 的字符串,但只需将其保存在我提到的文件夹中
关于spring - 如何将spring boot项目转换为spring mvc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57412488/