ITeye博客

SpringBoot支持MVC

andrew7676 · 2019-02-14 09:27 · SpringBoot · 编程语言 · Markdown

[size=medium][b]1) @RequestMapping配置url映射[/b][/size] [size=medium][b]2) @Controller处理http请求[/b][/size] [code="说明"] 在pom.xml中添加Freemarker支持 pom.xml org.springframework.boot spring-boot-starter-freemarker [/code] [code="java"] 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 Insert title here show: ${message} http://localhost:8000/HelloWorld/freemarker/say 运行结果: show: springboot你好啊! [/code] [size=medium][b]3) @RestController处理ajax请求[/b][/size] [code="说明"] @RestController相当于@Controller+@ResponseBody [/code] [code="java"] src/main/webapp/jquery-1.4.4.js src/main/webapp/index.html Insert title here 获取消息 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第二条消息'} [/code] [size=medium][b]4) @PathVariable获取url参数[/b][/size] [code="java"] src/main/webapp/index.html 博客 src/main/resourse/templates/blog.ftl Insert title here 博客id:${id} 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 [/code] [size=medium][b]5) @RequestParam获取请求参数[/b][/size] [code="java"] src/main/webapp/index.html 搜索 src/main/resourse/templates/query.ftl Insert title here q: ${q} 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 [/code]

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。