🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
一.Anno.java代码: ~~~ package com.youge.controller; /** * @author: hcf * @qq: 46914685 * @email: 46914685@qq.com * @date: 2020-09-25 18:41 */ import com.youge.domain.User; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.support.SessionStatus; import java.util.Date; import java.util.Map; /** * 常用注解 */ @Controller @RequestMapping("/anno") @SessionAttributes(value = {"msg"}) //把msg存入到session域对中 public class Anno { /** * RequestParam注解 * * @param name * @param age * @return */ @RequestMapping(value = "/testRequestParam") public String testRequestParam(@RequestParam(value = "username") String name, @RequestParam("age") Integer age) { System.out.println("testRequestParam执行了"); System.out.println(name); System.out.println(age); return "success"; } /** * 获取请求体的内容 username=admin&age=444 * * @param body * @return */ @RequestMapping("/testRequestBody") public String testRequestBody(@RequestBody String body) { System.out.println("testRequestBody执行了"); System.out.println(body); return "success"; } /** * PathVariable注解 restful风格 * * @param id * @return */ @RequestMapping("/testPathVariable/{sid}") public String testPathVariable(@PathVariable("sid") String id) { System.out.println("testPathVariable执行"); System.out.println(id); return "success"; } /** * 获取请求头的值,不常用 * * @param header * @return */ @RequestMapping("/testRequestHeader") public String testRequestHeader(@RequestHeader(value = "Accept") String header) { System.out.println("testRequestHeader执行了"); System.out.println(header); return "success"; } /** * 获取Cookie的值 * * @param cookieValue * @return */ @RequestMapping("/testCookieValue") public String testCookieValue(@CookieValue("JSESSIONID") String cookieValue) { System.out.println("testCookieValue执行了"); System.out.println(cookieValue); return "success"; } /** * ModelAttribute 注解 * * @return */ @RequestMapping("/testModelAttribute") // public String testModelAttribute(User user) { public String testModelAttribute(@ModelAttribute("abc") User user) { System.out.println("testModelAttribute执行了..."); System.out.println(user); return "success"; } /** * 有返回值 * @param name * @return */ // @ModelAttribute // public User showUser(String name){ // System.out.println("showUser执行了..."); // //通过用户查询数据库(模拟) // User user = new User(); // user.setName(name); // user.setAge(20); // user.setDate(new Date()); // return user; // } /** * 无返回值 * * @param name * @return */ // @ModelAttribute // public void showUser(String name, Map<String, User> map) { // System.out.println("showUser执行了..."); // //通过用户查询数据库(模拟) // User user = new User(); // user.setName(name); // user.setAge(20); // user.setDate(new Date()); // map.put("acb",user); // } /** * sessionAttributes注解 * 用于多次执行控制器方法间的参数共享 */ @RequestMapping("/testSessionAttributes") public String testSessionAttributes(Model model){ System.out.println("testSessionAttributes...执行了"); //底层会存储到request域对象中.. model.addAttribute("msg","session共享..."); return "success"; } /** * 获取session * @param modelMap * @return */ @RequestMapping("/getSessionAttributes") public String getSessionAttributes(ModelMap modelMap){ System.out.println("getSessionAttributes..."); String msg = (String) modelMap.get("msg"); System.out.println(msg); return "success"; } /** * 删除session * @param status * @return */ @RequestMapping("/delSessionAttributes") public String delSessionAttributes(SessionStatus status){ System.out.println("delSessionAttributes...执行了"); status.setComplete(); return "success"; } } ~~~ ***** 二.anno.jsp ~~~ <%-- Created by IntelliJ IDEA. User: Administrator Date: 2020-09-25 Time: 18:39 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <%--常用注解--%> <a href="/anno/testRequestParam?username=哈哈哈&age=30">RequestParam测试</a> <br> <h3>RequestBody测试</h3> <form action="/anno/testRequestBody" method="post"> 姓名:<input name="username" type="text" /><br/> 年龄:<input type="text" name="age" /> <input type="submit" value="提交" /> </form> <br> <a href="/anno/testPathVariable/100">PathVariable测试</a><br> <a href="/anno/testRequestHeader">RequestHeader测试</a><br> <a href="/anno/testCookieValue">CookieValue测试</a><br> <h2>ModelAttribute测试</h2> <form action="/anno/testModelAttribute" method="post"> 姓名:<input name="name" type="text" /><br/> 年龄:<input type="text" name="age" /> <input type="submit" value="提交" /> </form> <br> <a href="/anno/testSessionAttributes">SessionAttributes测试</a><br> <a href="/anno/getSessionAttributes">获取Session</a><br> <a href="/anno/delSessionAttributes">删除Session</a><br> </body> </html> ~~~ ***** 三.success.jsp ~~~ <%-- Created by IntelliJ IDEA. User: Administrator Date: 2020-09-24 Time: 21:12 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>成功</title> </head> <body> <h2>入门成功!!!!</h2> <%--${requestScope}--%> ${requestScope.msg} ${msg} ${sessionScope} </body> </html> ~~~