springmvc支持提交参数类型

1. 参数类型: 基本数据类型
2. POJO、类的支持级联、实体类List、实体类Map,不支持直接提交List,Map,List,Map必须位于包装类中
3. 传递数组: 

   应用批量删除、checkbox


HTML页面:

 <!--  测试@RequestParam -->
   <a href="helloworldfinal">helloworldfinal</a> <br>
   <a href="helloworldfinal2">helloworldfinal2</a>
   <hr>
   <br>
   <!-- 接收Integer类型 -->
   <form action="helloworld" method="post">
    用户Id: <input type="text"  name="usernameId"/> <br>
    
    <input type="submit" value="提交" />

   </form>
   
   <br>
   <hr>
   接收javaben
   
      <form action="helloworld2" method="post">
    用户Id: <input type="text"  name="id"/> <br>
      用户名: <input type="text"  name="name"/> <br>
          用户年级: <input type="text"  name="age"/> <br>  
          用户地址: <input type="text" name="address.location" />
    <input type="submit" value="提交" />

   </form>
   <hr>
   <!-- 传递数组,比如批量删除 -->
   <form action="helloworld3" method="post">
    1:   <input type="checkbox"  name="ids" value="1"/>
    2: <input type="checkbox"  name="ids" value="2"/>
    3: <input type="checkbox"  name="ids" value="3"/>
    <input type="submit" value="提交" />
   </form>
   <hr>
   <!-- 传递List集合,不能直接传递List,List要放在包装类中,用途比如批量插入用户List<User> -->
   
      <form action="helloworld4" method="post">
    用户地址1: <input type="text"  name="addresss[0].location"/> <br>
      用户地址2: <input type="text"  name="addresss[1].location"/> <br>
    
    <input type="submit" value="提交" />

   </form>
   <hr>
   <!-- 传递Map集合,不能直接传递Map集合,需要放在包装类中 ,和实体类一致-->
   
    <form action="helloworld4" method="post">
    用户名: <input type="text"  name="maps['username']"/> <br>
      用户年龄: <input type="text"  name="maps['age']"/> <br>
    
    <input type="submit" value="提交" />

   </form>

  sprignmvc控制器代码:

@Controller
public class HelloWorld {

	//这里使用 RequestParam指定参数默认值,是否必须
	// 默认支持GET|POST请求, value中可以写多个路径,也可以支持通配符
	@RequestMapping(value={"/helloworldfinal","/helloworldfinal2"})
	  public String hello9(@RequestParam(defaultValue="xiaoming",required=true) String username){
		System.out.println("调用了这个方法-------:"+username);
		  return "Success";
	  }
	
@RequestMapping("/helloworld")
  public String hello(Integer usernameId){
	System.out.println("usernameId:"+usernameId);
	  return "Success";
  }

@RequestMapping("/helloworld2")
public String hello2(User user){
	System.out.println(user);
	  return "Success";
}

@RequestMapping("/helloworld3")
public String hello3(Integer[] ids){
	System.out.println(Arrays.toString(ids));
	  return "Success";
}

@RequestMapping("/helloworld4")
public String hello4(User user){
	System.out.println(user);
	  return "Success";
}
}

猜你喜欢

转载自blog.csdn.net/dreams_deng/article/details/80936126
今日推荐