spring boot学习(三)---Controller

来源:互联网 发布:ps4辐射4美女捏脸数据 编辑:程序博客网 时间:2024/06/06 12:51

三 Controller

1. 常用注解

@Controller 接收http请求
@RestController 是@Controller和@ResponseBody的结合
@RequestMapping url地址映射
@PathVariable 获取url参数
@RequestParam 获取请求参数的值
@GetMapping 组合注解 简化get方法

2. 模板配置

在resources目录下新建templates,在下面新建html模板(模板中随便写点东西,便于观察)
这里写图片描述
pom.xml中添加配置

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

HelloController改成如下配置(主要修改类上的注解,和方法上的返回值)

package com.boot.web;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;@Controllerpublic class HelloController {    @Value("${stu.name}")    private String cupSize;//配置文件注入    @Value("${stu.context}")    private String context;    @RequestMapping(value="/hello" ,method = RequestMethod.GET) public String say(){         return "index";        //return "spring boot!"; }}

在浏览器中输入localhost:8080/hello 可以看到模板

原创粉丝点击