1.RequestParam
1.1使用说明
1.2使用示例
如下参数名称不一致,则获取不到参数。
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <%--常用注解--%> <a href="anno/testRequestParam?name=哈哈">RequestParam</a> </body> </html>
package cn.itcast.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; /** * 常用的注解 */ @Controller @RequestMapping("/anno") public class AnnoController { @RequestMapping("/testRequestParam") public String testRequestParam(String username){ System.out.println("执行了..."); System.out.println(username); return "success"; } }
解决方法:
package cn.itcast.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; /** * 常用的注解 */ @Controller @RequestMapping("/anno") public class AnnoController { @RequestMapping("/testRequestParam") public String testRequestParam(@RequestParam(name = "name") String username){ System.out.println("执行了..."); System.out.println(username); return "success"; } }
2. RequestBody
2.1使用说明
2.2使用示例
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <%--常用注解--%> <form action="anno/testRequestBody" method="post"> 用户姓名:<input type="text" name="username" /><br/> 用户年龄:<input type="text" name="age" /><br/> <input type="submit" value="提交" /> </form> </body> </html>
package cn.itcast.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; /** * 常用的注解 */ @Controller @RequestMapping("/anno") public class AnnoController { @RequestMapping("/testRequestBody") public String testRequestBody(@RequestBody String body){ //注解描述拿到的是请求体,而不是body属性 System.out.println("执行了..."); System.out.println(body); return "success"; } }
3.PathVaribale
3.1使用说明
3.2使用示例
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <%--常用注解--%> <a href="anno/testPathVariable/10">testPathVariable</a> </body> </html>
package cn.itcast.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; /** * 常用的注解 */ @Controller @RequestMapping("/anno") public class AnnoController { @RequestMapping("/testPathVariable/{sid}") public String testPathVariable(@PathVariable(name = "sid") String id){ //注解描述拿到的是请求体,而不是body属性 System.out.println("执行了..."); System.out.println(id); return "success"; } }
4.RequestHeader
4.1使用说明
4.2使用示例
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <%--常用注解--%> <a href="anno/testRequestHeader">testRequestHeader</a> </body> </html>
package cn.itcast.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; /** * 常用的注解 */ @Controller @RequestMapping("/anno") public class AnnoController { @RequestMapping("/testRequestHeader") public String testRequestHeader(@RequestHeader(value = "accept") String header) { //注解描述拿到的是请求体,而不是body属性 System.out.println("执行了..."); System.out.println(header); return "success"; } }
5.CookieValue
5.1使用说明
5.2使用示例
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <%--常用注解--%> <a href="anno/testCookieValue">testCookieValue</a> </body> </html>
package cn.itcast.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; /** * 常用的注解 */ @Controller @RequestMapping("/anno") public class AnnoController { @RequestMapping("/testCookieValue") public String testCookieValue(@CookieValue(value="JSESSIONID") String cookieValue) { //注解描述拿到的是请求体,而不是body属性 System.out.println("执行了..."); System.out.println(cookieValue); return "success"; } }
6. ModelAttribute
6.1 使用说明
6.2使用示例
1.方法有返回值
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <%--常用注解--%> <form action="anno/testModelAttribute" method="post"> 用户姓名:<input type="text" name="uname" /><br/> 用户年龄:<input type="text" name="age" /><br/> <input type="submit" value="提交" /> </form> </body> </html>
package cn.itcast.controller; import cn.itcast.domain.User; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import java.util.Date; /** * 常用的注解 */ @Controller @RequestMapping("/anno") public class AnnoController { @RequestMapping("/testModelAttribute") public String testModelAttribute(User user) { //注解描述拿到的是请求体,而不是body属性 System.out.println("testModelAttribute执行了..."); System.out.println(user); return "success"; } }
因为只封装了两个参数,第三个参数为空。
如果,第三个参数我想要用数据库里面已经存在的参数,可以如下操作:
package cn.itcast.controller; import cn.itcast.domain.User; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import java.util.Date; /** * 常用的注解 */ @Controller @RequestMapping("/anno") public class AnnoController { @RequestMapping("/testModelAttribute") public String testModelAttribute(User user) { //注解描述拿到的是请求体,而不是body属性 System.out.println("testModelAttribute执行了..."); System.out.println(user); return "success"; } @ModelAttribute public User showUser(String uname){ System.out.println("showUser执行了..."); // 通过用户查询数据库(模拟) User user = new User(); user.setUname(uname); user.setAge(20); user.setDate(new Date()); return user; } }
2.方法没有返回值
package cn.itcast.controller; import cn.itcast.domain.User; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import java.util.Date; import java.util.Map; /** * 常用的注解 */ @Controller @RequestMapping("/anno") public class AnnoController { @RequestMapping("/testModelAttribute") public String testModelAttribute(@ModelAttribute("abc") User user) { //注解描述拿到的是请求体,而不是body属性 System.out.println("testModelAttribute执行了..."); System.out.println(user); return "success"; } @ModelAttribute public void showUser(String uname, Map<String,User> map){ System.out.println("showUser执行了..."); // 通过用户查询数据库(模拟) User user = new User(); user.setUname(uname); user.setAge(20); user.setDate(new Date()); map.put("abc",user); } }
7.SessionAttribute
7.1使用说明
7.2使用示例
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <%--常用注解--%> <a href="anno/testSessionAttributes">testSessionAttributes</a> <a href="anno/getSessionAttributes">getSessionAttributes</a> <a href="anno/delSessionAttributes">delSessionAttributes</a> </body> </html>
package cn.itcast.controller; 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; /** * 常用的注解 */ @Controller @RequestMapping("/anno") @SessionAttributes(value={"msg"}) // 把msg=美美存入到session域对中 public class AnnoController { @RequestMapping(value = "/testSessionAttributes") public String testSessionAttributes(Model model) { System.out.println("testSessionAttributes..."); // 底层会存储到request域对象中 model.addAttribute("msg", "美美"); return "success"; } @RequestMapping(value = "/getSessionAttributes") public String getSessionAttributes(ModelMap modelMap) { System.out.println("getSessionAttributes..."); String msg = (String) modelMap.get("msg"); System.out.println(msg); return "success"; } @RequestMapping(value = "/delSessionAttributes") public String delSessionAttributes(SessionStatus status) { System.out.println("delSessionAttributes..."); status.setComplete(); return "success"; } }