首页 > 编程语言 > 详细

spring boot controller的使用

时间:2017-11-07 18:51:48      阅读:286      评论:0      收藏:0      [点我收藏+]

一、知识点

@Controller 处理http请求(不推荐使用)
@RestController spring4之后新加的注解,原来返回json需要@ResponseBody配合@Controller
@RequestMapping 配置Url映射

二、具体使用讲解

1.@Controller(了解即可,现在的开发基本都是前后端分离,不用再使用模版的方式,采用REST方式返回json数据是主流)

需要配合模版的使用

1)打开pom.xml

添加spring官方的一个模版thymeleaf

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

2)在resources下新建文件夹templates,然后在其中新建一个html,index.html

<h1>hello spring boot!</h1>

3)controller中将@RestController改为@Controller

package com.dechy.girl.girl;

import org.springframework.beans.factory.annotation.Autowired;
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;
import org.springframework.web.bind.annotation.RestController;

@Controller
public class HelloController {

    @Autowired
    private GirlProperties girlProperties;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String say (){
        return "index";
    }
}

4)启动后,访问得到index.html的内容

spring boot controller的使用

原文:http://www.cnblogs.com/knyel/p/7800303.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!