第一节:@RequestMapping 配置url 映射
第二节:@Controller 处理http 请求
转发到一个页面,以前是转发到jsp页面,现在使用freemarker;
在pom.xml页面右键,spring-edit starters , 添加freemarker支持:spring-boot-starter-freemarker
pom.xml:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
com.cy.controller.HelloWorldFreemarkerController.java:
package com.cy.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 mav=new ModelAndView(); mav.addObject("message", "springboot你好!"); mav.setViewName("helloWorld"); return mav; } }
freemarker模板是ftl为后缀的;src/main/resources/templates/helloWorld.ftl:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> show: ${message} </body> </html>
浏览器http://localhost:8888/HelloWorld/freemarker/say:显示:show: springboot你好!
第三节:@RestController 处理ajax 请求
第四节:@PathVariable 获取url 参数
第五节:@RequestParam 获取请求参数
例子如下:
com.cy.controller.HelloWorldAjaxController.java:
package com.cy.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/ajax") public class HelloWorldAjaxController { @RequestMapping("/hello") public String say(){ return "{'message1':'SpringBoot你好','message2','Spring你好2'}"; } }
com.cy.controller.BlogController.java:
package com.cy.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") Integer id){ ModelAndView mav=new ModelAndView(); mav.addObject("id", id); mav.setViewName("blog"); return mav; } @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; } }
webapp/index.html:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <script src="resources/jquery/jquery.min.js"></script> <script type="text/javascript"> function show(){ $.post("ajax/hello",{},function(result){ alert(result); }); } </script> </head> <body> <button onclick="show()">点击</button> <a href="/HelloWorld/blog/21">博客</a> <a href="/HelloWorld/blog/query?q=123456">搜索</a> </body> </html>
src/main/resoureces/templates/blog.ftl:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> 博客id:${id} </body> </html>
src/main/resources/templates/query.ftl:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> q: ${q} </body> </html>
测试:
浏览器:http://localhost:8888/HelloWorld/index.html
点击按钮:alert: {'message1':'SpringBoot你好','message2','Spring你好2'}
点击博客链接:转发页面,显示博客id:21
点击搜索,到页面http://localhost:8888/HelloWorld/blog/query?q=123456,显示q: 123456