Spring boot学习笔记之三:SpringBoot之Controller使用

本节主要了解如下注解,基本和之前的spring mvc注解的使用一样,我们再使用spring boot稍微复习一下

@Controller 处理http请求@RestController Spring4之后新加的注解,原来返回json需要@ResponseBody配合@Controller@RequestMapping 配置url映射@PathVariable 获取url中的数据@RequestParam 获取请求参数中的值

1. 前言

项目前后台交互的话 无非两种方式

  • 一种普通整体页面提交,比如form提交;

  • 还有一种局部刷新,或者叫做异步刷新,ajax提交;

@Controller就是整体页面刷新提交的处理注解

@RestController就是ajax提交,一般返回json格式,相当于我们经常使用的@ResponseBody配合@Controller组合

2 .编码

这里我们分别来演示上面两种交互。请求后台,必须返回一个视图,以前我们一般用Jsp,但是SpringBoot不推荐我们使用jsp,主要是强调前后台分离;官方推荐的是这几种模版视图引擎,我一般推荐Freemarker和Velocity;

添加freemarker支持,在pom.xml文件添加对应的依赖


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

然后我们在Controller包下新建一个新的Controller类 HelloWorldFreemakerController

package com.jd.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by Administrator on 2018/6/24.
 */
@Controller
@RequestMapping("/freemarker")
public class HelloWorldFreemakerController {

    /**
     * 设置数据,返回到freemarker视图
     * @return
     */
    @RequestMapping("/say")
    public ModelAndView say(){
        ModelAndView mav=new ModelAndView();
        mav.addObject("message", "SpringBoot 大爷你好!");
        mav.setViewName("helloWorld");
        return mav;
    }
}

对应的,我们在templates下新建一个helloWorld.ftl模版文件(Freemaker的后缀问ftl)

helloWorld.ftl模版文件内容:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
show:${message}
</body>
</html>

我们测试下,启动SpringbootDemoApplication

然后浏览器输入:http://localhost:8888/HelloWorld/freemarker/say

页面显示结果:

我们再演示下@RestController,ajax方式

我们新建一个HelloWorldAjaxController类

package com.jd.controller;
 
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
 
/**
 * 返回ajax json格式
 * @author user
 *
 */
@RestController
@RequestMapping("/ajax")
public class HelloWorldAjaxController {
 
    @RequestMapping("/hello")
    public String say(){
        return "{'message1': 'SpringBoot你大爷','message2','SpringBoot你大爷2'}";
    }
}

返回json串

这里我们用的是jquery,随便找个jquery.js进行引用,这个jquery ajax的使用一定要熟练我这里就不说了

index.html代码,一个ajax请求


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="jQuery.js"></script> <!--自己找一个jquery,在线的也可以-->
<script type="text/javascript">
    function show(){
        $.post("ajax/hello",{},
                function(result){
                    alert(result);
                }
            );
    }
     
</script>
</head>
<body>
<button onclick="show()">你大爷</button>
</body>
</html>

启动HelloWorldApplication类

页面先请求index.html

浏览器输入:http://localhost:8888/HelloWorld/

当然这里的json比较简单,所以我直接返回; 实际项目Json格式复杂,要借助于一些json框架,比如Json-lib,gson等;

当然还有一些比如

@PathVariable 获取url中的数据,获取路径中的值

@RequestParam 获取请求参数中的值

简单的写个例子,了解一下即可

package com.jd.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/blog")
public class BlogController {

    /**
     * @PathVariable 配合 @RequestMapping使用可以获取到路径中的参数
     * http://localhost:8888/HelloWorld/blog/21  则 id=21
     * @param id
     * @return
     */
    @RequestMapping("/{id}")
    public ModelAndView show(@PathVariable("id") Integer id){
        ModelAndView mav=new ModelAndView();
        mav.addObject("id", id);
        mav.setViewName("blog");
        return mav;
    }

    /**
     * RequestParam 获取提交的参数
     * 
     * http://localhost:8888/HelloWorld/blog/query?q=123456 则q = 123456
     * @param q
     * @return
     */
    @RequestMapping("/query")
    public ModelAndView query(@RequestParam(value="q",required=false)String q){
        ModelAndView mav=new ModelAndView();
        mav.addObject("q", q);
        mav.setViewName("query");
        return mav;
    }
}



猜你喜欢

转载自blog.csdn.net/eeeeasy/article/details/80808805