spring boot 入门第一节
时间:2021-09-29 10:08:04 +0800 CST 浏览:351

spring boot 控制器区别

配置

application.properties

#热部署文件,页面不产生缓存,及时更新
spring.thymeleaf.cache=false
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**

spring.thymeleaf.prefix = classpath:/templates/
spring.thymeleaf.suffix = .html
spring.thymeleaf.mode = HTML
spring.thymeleaf.encoding = UTF-8

pox.xml

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

控制器

// @Controller
@RestController
public class HomeController {
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model) {
        model.addAttribute("hello", "Hello, Spring Boot!");
        return "list";
    }

    @RequestMapping(value = "/list2", method = RequestMethod.GET)
    public ModelAndView list2(Model model) {
        model.addAttribute("hello", "Hello, Spring Boot!");
        return new ModelAndView("list");
    }
}

说明

模板引擎使用不同控制器的区别:

RestController控制器使用ModelAndView返回html(/list2)

Controller控制器使用view相对路径(/list)



如果这篇文章对你有所帮助,可以通过下边的“打赏”功能进行小额的打赏。

本网站部分内容来源于互联网,如有侵犯版权请来信告知,我们将立即处理。


来说两句吧