【问题标题】:How to convert springboot project to spring mvc如何将spring boot项目转换为spring mvc
【发布时间】:2019-12-16 03:48:17
【问题描述】:

我正在做一个春季启动项目
我想把它转换成spring web mvc
我尝试使用将其转换为动态 Web 模块 3.0
我为调度程序 servlet 添加了 WebAppInitializer 和视图解析器
但是当我部署战争时,它会成功部署,但我得到HTTP 404 错误

我还有什么需要做的吗?

【问题讨论】:

    标签: spring spring-boot spring-mvc gradle


    【解决方案1】:

    首先,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 应用程序,现在视图解析器怎么样?好吧,当我使用 Spring MVC 编写代码时,Spring 会管理这个问题,我实现 Thymeleaf 以执行视图,所以我还添加:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    

    此视图 (html) 保存在文件夹资源/模板中

    thadaaaaaa 我在控制器中唯一要做的就是将字符串返回到该模板,例如

    @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,它也应该可以返回您的视图的字符串,但只需将其保存在我提到的文件夹中

    【讨论】:

      猜你喜欢
      • 2018-04-04
      • 1970-01-01
      • 1970-01-01
      • 2019-02-18
      • 2015-06-04
      • 2022-01-22
      • 2016-09-01
      • 2017-03-11
      • 2019-06-18
      相关资源
      最近更新 更多