index.jsp
<body> <form action="demo.action" method="post"> 姓名: <input type="text" name="name"><br/> 密码: <input typr="text" name="password"><br/> <input type="submit" value="提交"> </form> </body> </html>web.xml
<!-- 请求参数 的字符编码 --> <filter> <filter-name>encoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping>
使用具体类型去接受参数
DemoController.java( 基本参数类型)
@Controller // 注解加载 类 public class DemoController { // 只有被容器管理了,才能使用注解 @RequestMapping("demo.action") public String demo(String name,String password){ 接受的参数,希望是某种基本参数类型,就用你希望的基本参数类型去接受 System.out.println("请求demo.action被处理"); System.out.println("name is "+name+" password is "+name); return "index.jsp"; // 负责解析HandlerMethod返回值 } }使用具体对象去接受参数
使用具体对象去接受参数,一定要保证传入的参数名称和对象的属性相同
@Controller public class DemoController { // 只有被容器管理了,才能使用注解 @RequestMapping("demo.action") public String demo(People people){ System.out.println("请求demo.action被处理"); System.out.println("name is "+people.getName()+" password is "+people.getPassword()); return "index.jsp"; // 负责解析HandlerMethod返回值 }当参数传递进来是null或 " "空字符串 时候
@RequestParam(defaultValue="20")String pageSize
name属性,与接受的参数名称不一致的时候
当传入的 姓名: <input type="text" name="name"><br/> name属性,与接受的参数名称不一致的时候 如: 姓名: <input type="text" name="name1"><br/>而方法demo(String name,String password) name属性就无法 接收了
public class DemoController { // 只有被容器管理了,才能使用注解 @RequestMapping("demo.action") public String demo(People people,@RequestParam(value="name1")String name,String password,@RequestParam(defaultValue="20")String pageSize,@RequestParam(defaultValue="1")String pageStart){ System.out.println("请求demo.action被处理"); System.out.println("对象作请求容器"+people); System.out.println("基本类型作请求容器"+"name :"+name+" password :"+password); System.out.println("没有参数就使用默认值 "+pageSize+" "+pageStart); return "index.jsp"; // 负责解析HandlerMethod返回值 } }
无法解决 对象属性名不匹配的问题。
某个参数必须传递
1.3.2 如果请求中没有这个参数时报400
1.3.2.1 400状态码表示请求参数类型有问题.
@RequestParam(required=true)String name
可能原因:1.前端表格中没有 name="name" 属性
2.SpringMvc 参数无法正确的转换 @RequestParam(required=true)int name
传递一个集合的时候:
<form action="demo.action" method="post"> 姓名: <input type="text" name="name"><br/> 密码: <input type="text" name="password"><br/> 爱好: <input type="checkbox" value="学习" name="like">学习 <input type="checkbox" value="篮球" name="like">篮球 <input type="checkbox" value="LOL" name="like">LOL<br/> <input type="submit" value="提交"> </form>
public class DemoController { // 只有被容器管理了,才能使用注解 @RequestMapping("demo.action") public String demo(People people,@RequestParam("like")List<String> list){ System.out.println("对象作请求容器"+people); for(String string :list){ System.out.println(string); } return "index.jsp"; // 负责解析HandlerMethod返回值 } }
@RequestParam()的使用:
1.defaultValue = :在接受集合的值的时候,会用到(默认值)。 2.value=:前端页面传递的参数和 实际去接受的参数 的命长不一样的时候。
时间参数的接受:
<form action="demo.action" method="post"> 时间: <input type="text" name="time"> <input type="submit" value="提交"> </form>
public class DemoController { // 只有被容器管理了,才能使用注解 @RequestMapping("demo.action") public String demo(@DateTimeFormat(iso=ISO.DATE) Date time){// 转换类 ISO(Date转换类) DATE:YYYY-HH-DD System.out.println(time); return "index.jsp"; // 负责解析HandlerMethod返回值 } }