SpringMVC之参数绑定

一、默认支持的类型

HttpServletRequest 、HttpServletResponse 、HttpSession 、Model/ModelMap ,直接在形参上给出这些默认类型的声明,就能直接使用了

@RequestMapping("/defaultParameter")
    public ModelAndView defaultParameter(HttpServletRequest request,HttpServletResponse response,
                            HttpSession session,Model model,ModelMap modelMap) throws Exception{
        request.setAttribute("requestParameter", "request类型");
        response.getWriter().write("response");
        session.setAttribute("sessionParameter", "session类型");
        //ModelMap是Model接口的一个实现类,作用是将Model数据填充到request域
        //即使使用Model接口,其内部绑定还是由ModelMap来实现
        model.addAttribute("modelParameter", "model类型");
        modelMap.addAttribute("modelMapParameter", "modelMap类型");
         
        ModelAndView mv = new ModelAndView();
        mv.setViewName("view/success.jsp");
        return mv;
    }

二、基本数据类型的绑定(不建议使用,最好使用其包装类型) 

基本数据类型,如果从前台页面传递的值为 null 或者 “”的话,那么会出现数据转换的异常,最好将参数数据类型定义成包装类型 

1.byte,占用一个字节,取值范围为 -128-127,默认是“\u0000”,表示空

2.short,占用两个字节,取值范围为 -32768-32767

3.int,占用四个字节,-2147483648-2147483647

4.long,占用八个字节,对 long 型变量赋值时必须加上"L"或“l”,否则不认为是 long 

5.float,占用四个字节,对 float 型进行赋值的时候必须加上“F”或“f”,如果不加,会产生编译错误,因为系统

自动将其定义为 double 型变量。double转换为float类型数据会损失精度。float a = 12.23产生编译错误的,float a = 12是正确的

6.double,占用八个字节,对 double 型变量赋值的时候最好加上“D”或“d”,但加不加不是硬性规定

7.char,占用两个字节,在定义字符型变量时,要用单引号括起来

8.boolean,只有两个值“true”和“false”,默认值为false,不能用0或非0来代替,这点和C语言不同

三、包装数据类型的绑定 

Integer、Long、Byte、Double、Float、Short,(String 类型在这也是适用的

    @RequestMapping("/getDstList_noSecurity")
	public List getDstList(Integer id){
		return totalService.getDstList(id);
	}

 四、POJO(实体类)类型的绑定 

<form action="pojo" method="post">
        用户名:<input type="text" name="username" value="Mark"></br>
        性别:<input type="text" name="sex" value="1"></br>
        出生日期:<input type="text" name="birthday" value="2017-08-25"></br>
        <input type="submit" value="提交">
</form>

 JSP页面:注意输入框的 name 属性值和下面 POJO 实体类的属性保持一致即可映射成功

public class User {
	private String username;

	private String sex;

	private Date birthday;
	//get|set方法省略
}
    @RequestMapping("/getDstList_noSecurity")
	public List getDstList(User user){
		return totalService.getDstList(user);
	}

 五、复合POJO(实体类)类型的绑定

<form action="pojo" method="post">
        用户名:<input type="text" name="username" value="Mark"></br>
        性别:<input type="text" name="sex" value="1"></br>
        出生日期:<input type="text" name="birthday" value="2017-08-25"></br>
        地址:<input type="text" name="contactInfo.address" value="长沙"></br>
        电话:<input type="text" name="contactInfo.tel" value="10086"></br>
        <input type="submit" value="提交">
</form>

  JSP 页面:注意属性name的命名,复合属性名.字段名

public class ContactInfo {
    private String tel;
 
    private String address;
    //get|set方法省略
}
public class User {
	private String username;

	private String sex;

	private Date birthday;

        private ContactInfo contactInfo;
	//get|set方法省略
}
    @RequestMapping("/getDstList_noSecurity")
	public List getDstList(User user){
		return totalService.getDstList(user);
	}

 六、数组类型的绑定 

<c:forEach items="${itemList }" var="item">
<tr>
	<td><input type="checkbox" name="ids" value="${item.id}"/></td>
</tr>
</c:forEach>

 接收jsp页面中多个ids值

1.直接接收

    @RequestMapping("/getDstList_noSecurity")
	public List getDstList(Integer[] ids){
		return totalService.getDstList(ids);
	}

2.定义ItemVo接收

public class QueryVo{
    private Item item;
 
    private Integer[] ids; //与name属性名相同
    //get|set方法省略
}
    @RequestMapping("/getDstList_noSecurity")
	public List getDstList(Integer[] ids){
		return totalService.getDstList(ids);
	}

七、List类型的绑定

<c:forEach items="${itemList }" var="item" varStatus="s">
<tr>
	<td>
		<input type="hidden" name="userList[${s.index}].name" value="${item.name}"/>
		<input type="text" name="userList[${s.sex}].name" value="${item.sex}"/>
	</td>
</tr>
</c:forEach>
public class UserVo {
     
    private List<User> userList;
    //get|set方法
}
    @RequestMapping("/getDstList_noSecurity")
	public List getDstList(UserVo [] userList){
		return totalService.getDstList(userList);
	}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值