`

SpringBoot支持MVC

阅读更多
1) @RequestMapping配置url映射

2) @Controller处理http请求

在pom.xml中添加Freemarker支持

pom.xml

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


src/main/java/com/andrew/controller/HelloWorldFreemarkerController.java

package com.andrew.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/freemarker")
public class HelloWorldFreemarkerController {
    @RequestMapping("/say")
    public ModelAndView say() {
        ModelAndView mv = new ModelAndView();
        mv.addObject("message", "springboot你好啊!");
        mv.setViewName("helloWorld");
        return mv;
    }
}

新建helloworld.html,将src/main/webapp下的helloworld.html移动到src/main/resourse/templates下,修改文件类型为helloworld.ftl
helloworld.ftl

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

http://localhost:8000/HelloWorld/freemarker/say
运行结果:
show: springboot你好啊!


3) @RestController处理ajax请求

@RestController相当于@Controller+@ResponseBody


src/main/webapp/jquery-1.4.4.js

src/main/webapp/index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="jquery-1.4.4.js"></script>
<script type="text/javascript">
    function show(){
        $.post("ajax/hello",{},function(result){
            alert(result);
        });
    }
</script>
</head>
<body>
<button onclick="show()">获取消息</button>
</body>
</html>

src/main/java/com/andrew/controller/AjaxController.java

package com.andrew.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/ajax")
public class AjaxController {
    @RequestMapping("/hello")
    public String say(){
        return "{'message1':'SpringBoot第一条消息','message2','SpringBoot第二条消息'}";
    }
}

http://localhost:8000/HelloWorld/index.html
运行结果:
click 获取消息
alert {'message1':'SpringBoot第一条消息','message2','SpringBoot第二条消息'}


4) @PathVariable获取url参数

src/main/webapp/index.html

<a href="/HelloWorld/blog/21">博客</a>

src/main/resourse/templates/blog.ftl

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

src/main/java/com/andrew/controller/BlogController.java

package com.andrew.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 {
    @RequestMapping("/{id}")
    public ModelAndView show(@PathVariable("id") String id){
        ModelAndView mv = new ModelAndView();
        mv.addObject("id", id);
        mv.setViewName("blog");
        return mv;
    }
}

http://localhost:8000/HelloWorld/index.html
运行结果:
click 博客
http://localhost:8000/HelloWorld/blog/21
博客id:21


5) @RequestParam获取请求参数

src/main/webapp/index.html

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

src/main/resourse/templates/query.ftl

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

src/main/java/com/andrew/controller/BlogController.java

package com.andrew.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 {
    @RequestMapping("/query")
    public ModelAndView query(@RequestParam(value="q",required=false)String q){
        ModelAndView mv = new ModelAndView();
        mv.addObject("q", q);
        mv.setViewName("query");
        return mv;
    }
}

http://localhost:8000/HelloWorld/index.html
运行结果:
click 搜索
http://localhost:8000/HelloWorld/blog/query?q=123456
q: 123456
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics