SpringBoot 学习笔记之mvc的支持

 一、先附上application.properties配置文件,指定了端口和路径

server.port=8888
server.servlet.context-path=/HelloWorld

二、一些标签 涉及SpringMvc

@RequestMapping 配置 url 映射

@Controller 处理 http 请求

@RestController 处理 ajax 请求

@PathVariable 获取 url 参数

@RequestParam 获取请求参数

 

三、用freemarker,先添加支持

在红箭头处添加,鼠标右键-》Spring-》EditStarters-》勾选FreeMarker-》OK就自动添加进来了如图

controller包下新建一个FreeMarkerController

新建freemarkername的模板文件,新建个freemarkername.html,在webapp目录下

将其移到templates目录下,并将后缀名html改为ftl

FreeMarker 前台获取后台数据类似el表达式

地址输入…8888/HelloWorld/freemarker/say运行

请求@Controller(“/ freemarker”) 映射到 @RequestMapping(“/say”)

然后转发视图到前台。

 

、 @RestController 处理 ajax 请求  是后续版本推出的,

src/main/webapp 下新建个index.html 相当于一个默认页面

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="http://jquery.min.js"></script>
<script type="text/javascript">
 
      function show(){
               $.post("ajax/hello",{},function(result){//请求ajax无参数,有返回函数
                         alert(result);
               });
      }
 
</script>
</head>
<body>
<button onclick="show()">请求ajax</button>
</body>
</html>

后台写个AjaxController

package com.guo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
public class AjaxController {
@RestController
@RequestMapping("/ajax")
public class HelloWorldAjaxController{
         @RequestMapping("/helloa")
         public String say(){
                   return "{'m1':'ajax请求返回','m2','json数据'}";
         }
}
}

@RestController可以直接输入http:localhost:8888/HelloWorld就行了,不需加映射后缀了

 

五、 @PathVariable 获取 url 参数

1、新建个FilmController

package com.guo.controller;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
 
@Controller
@RequestMapping("/film")
public class FilmController {
      @RequestMapping("/{id}")
      public ModelAndView show(@PathVariable("id") Integer id){
           ModelAndView mav=new ModelAndView();
           mav.addObject("id",id);
           mav.setViewName("blog");
           return mav;
           
      }
}

2、再新建个film.ftl

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

3.在index.jsp添加个链接

<a href="/HelloWorld/film/22">电影网</a>

运行http://localhost:8888/HelloWorld/film/22

六、.@RequestParam 获取请求参数,可以使get参数也可以是post

1.模拟下搜索,这里有个q参数123456,在index.html添加个链接

<a href="/HelloWorld/film/query?q=123456">搜索</a>

2.在FilmController中添加

@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;
}

@RequestParam(value="q",required=false)String q) 参数也可以是(String q)但是如果q为空的话(String q)会报错,(@RequestParam(value="q",required=false)String q) 有个required=false表示q不一定非要传

 

3.在写个query.ftl

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

4.运行结果

localhost://8888/HelloWorld

点击搜索

本博客为博主的学习笔记,不作任何商业用途。
原文地址:https://www.cnblogs.com/guo7533/p/8700415.html