标签:encoding content return control throw params 映射 ref ping
1、@RequestParam(重要):映射请求参数
1 @RequestMapping("/springmvc") 2 @Controller 3 public class SpringMVCTest { 4 private static final String SUCCESS = "success"; 5 @RequestMapping("/testRequestParam") 6 public String testRequestParam(@RequestParam(value = "username") String un, @RequestParam(value = "age") int age) { 7 System.out.println("testRequestParam,username:" + un + ",age:" + age); 8 return SUCCESS; 9 }
若请求参数age不是必须的,那么代码可以改为:
1 @RequestMapping("/springmvc") 2 @Controller 3 public class SpringMVCTest { 4 private static final String SUCCESS = "success"; 5 6 /* 7 * @RequestParam来映射请求参数 value值即为请求参数的参数名 required:确定该参数是否必须,默认为true 8 * defaultValue 请求参数的默认值 9 */ 10 @RequestMapping("/testRequestParam") 11 public String testRequestParam(@RequestParam(value = "username") String un, 12 @RequestParam(value = "age",required=false,defaultValue="0") int age) { 13 System.out.println("testRequestParam,username:" + un + ",age:" + age); 14 return SUCCESS; 15 }
index.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <!-- 超链接 --> 11 <a href="springmvc/testRequestParam?username=atguigu&age=11">Test RequestParam</a>
整体效果实现的其他代码参见HelloWorld实例。
2、@RequestHeader:映射请求头信息
1 @RequestMapping("/springmvc") 2 @Controller 3 public class SpringMVCTest { 4 private static final String SUCCESS = "success"; 5 @RequestMapping("/testRequestHeader") 6 public String testRequestHeader(@RequestHeader(value="Accept-Language")String rl){ 7 System.out.println("testRequestHeader,Accept-Language:"+rl); 8 return SUCCESS; 9 }
index.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <!-- 超链接 --> 11 <a href="springmvc/testRequestHeader">Test RequestHeader</a> 12 </body> 13 </html>
整体效果实现的其他代码参见HelloWorld实例。
下面例子两个注解都有用到,供参考:
1 package com.atguigu.springmvc.handlers; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestHeader; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.RequestMethod; 7 import org.springframework.web.bind.annotation.RequestParam; 8 9 @RequestMapping("/springmvc") 10 @Controller 11 public class SpringMVCTest { 12 private static final String SUCCESS = "success"; 13 /** 14 * 了解:可以使用params和headers来更加精确的映射请求,params和headers支持简单的表达式 15 */ 16 @RequestMapping(value="/testParamsAndHeaders", 17 params={"usernamexxxxx","age!=10"},headers={"Accept-Language=zh-CN,zh;q=0.8"}) 18 public String testParamsAndHeaders(){ 19 System.out.println("testParamsAndHeaders"); 20 return SUCCESS; 21 } 22 23 @RequestMapping(value="/testParamsAndHeaders2") 24 public String testParamsAndHeaders2(@RequestParam("usernamex") String username, @RequestHeader("Host") String host){ 25 System.out.println(username + "-->" + host); 26 return SUCCESS; 27 } 28 }
3、@PathVariable
可以将URL中占位符参数绑定到控制器处理方法的入参中,例如:
1 @RequestMapping("/springmvc") 2 @Controller 3 public class SpringMVCTest { 4 private static final String SUCCESS = "success"; 5 /** 6 * @PathVariable可以映射URL中的占位符到目标方法的参数中 7 */ 8 @RequestMapping("/testPathVariable/{id}") 9 public String testPathVariable(@PathVariable("id") Integer id) { 10 System.out.println("testPathVariable:" + id); 11 return SUCCESS; 12 }
index.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <!-- 超链接 --> 11 <a href="springmvc/testPathVariable/1">Test PathVariable</a> 12 </body> 13 </html>
4、@RequestBody (用的很少)
1 @RequestMapping(value = "/something", method = RequestMethod.PUT) 2 public void handle(@RequestBody String body, Writer writer) throws IOException { 3 writer.write(body); 4 }
整体效果实现的其他代码参见HelloWorld实例。
总结: @PathVariabl Path http://localhost:8080/third-ssmm/springmvc/testPathVariable/1 (全路径)
springMVC @RequestHeader http几种域:header eg: http://localhost:8080/third-ssmm/springmvc/testRequestHeader
@RequestParam Query http://localhost:8080/third-ssmm/springmvc/testRequestParam?username=atguigu&age=11
@RequestBody Body (?前面是路径?后面是访问参数 )
标签:encoding content return control throw params 映射 ref ping
原文地址:http://www.cnblogs.com/wangna----558169/p/6033232.html