您的位置:首页 > 编程语言 > Java开发

springmvc中的参数绑定

2017-01-09 21:12 225 查看

在Struts2中我们都知道,它的传值是通过action类的属性进行传值的,并且在Action的方法中是不允许有形参的,但在springmvc中的参数传递是通过形参来传递的。

1.1    参数绑定

    处理器适配器在执行Handler之前需要把http请求的key/value数据绑定到Handler方法形参数上。

1.1.1 默认支持的参数类型

处理器形参中添加如下类型的参数处理适配器会默认识别并进行赋值。

1.1.1.1HttpServletRequest

通过request对象获取请求信息

1.1.1.2HttpServletResponse

通过response处理响应信息

1.1.1.3HttpSession

通过session对象得到session中存放的对象

1.1.1.4Model/ModelMap

ModelMap是Model接口的实现类,通过Model或ModelMap向页面传递数据,如下:

//调用service查询商品信息
Items item =
itemService.findItemById(id);
model.addAttribute("item",item);
 

页面通过${item.XXXX}获取item对象的属性值。

使用Model和ModelMap的效果一样,如果直接使用Model,springmvc会实例化ModelMap。

1.1.2 参数绑定介绍

         注解适配器对RequestMapping标记的方法进行适配,对方法中的形参会进行参数绑定,早期springmvc采用PropertyEditor(属性编辑器)进行参数绑定将request请求的参数绑定到方法形参上,3.X之后springmvc就开始使用Converter进行参数绑定。

1.1.3 简单类型

当请求的参数名称和处理器形参名称一致时会将请求参数与形参进行绑定。

1.1.3.1 整型

public String editItem(Modelmodel,Integer id) throws Exception{



1.1.3.2 字符串

1.1.3.3 单精度/双精度 

1.1.3.4 布尔型

处理器方法:

public String editItem(Modelmodel,Integer id,Boolean status) throwsException

请求url:
http://localhost:8080/springmvc_mybatis/item/editItem.action?id=2&status=false
说明:对于布尔类型的参数,请求的参数值为true或false。

1.1.3.5@RequestParam

使用@RequestParam常用于处理简单类型的绑定。

value:参数名字,即入参的请求参数名字,如value=“item_id”表示请求的参数区中的名字为item_id的参数的值将传入;
required:是否必须,默认是true,表示请求中一定要有相应的参数,否则将报;
HTTP Status 400 - Required Integer parameter'XXXX' is not present

defaultValue:默认值,表示如果请求中没有同名参数时的默认值

定义如下:

public String editItem(@RequestParam(value="item_id",required=true)String id) {    }
形参名称为id,但是这里使用value=" item_id"限定请求的参数名为item_id,所以页面传递参数的名必须为item_id。

注意:如果请求参数中没有item_id将跑出异常:

HTTP Status 500 - Required Integerparameter 'item_id' is not present

这里通过required=true限定item_id参数为必需传递,如果不传递则报400错误,可以使用defaultvalue设置默认值,即使required=true也可以不传item_id参数值

1.1.4 pojo

1.1.4.1简单pojo

将pojo对象中的属性名于传递进来的属性名对应,如果传进来的参数名称和对象中的属性名称一致则将参数值设置在pojo对象中

页面定义如下;

<inputtype="text" name="name"/>
<inputtype="text" name="price"/>
Contrller方法定义如下:

@RequestMapping("/editItemSubmit")
    public String editItemSubmit(Items items)throws Exception{
    System.out.println(items);
请求的参数名称和pojo的属性名称一致,会自动将请求参数赋值给pojo的属性。

1.1.4.2包装pojo

如果采用类似struts中对象.属性的方式命名,需要将pojo对象作为一个包装对象的属性,action中以该包装对象作为形参。

包装对象定义如下:

Public class QueryVo {
private Items
items;
}
页面定义:

<inputtype="text" name="items.name" />
<inputtype="text" name="items.price" />
Controller方法定义如下:

public String useraddsubmit(Modelmodel,QueryVoqueryVo)throwsException{
System.out.println(queryVo.getItems()); 

1.1.5 自定义参数绑定

1.1.5.1需求

根据业务需求自定义日期格式进行参数绑定。

1.1.5.2Converter

1.1.5.2.1      自定义Converter
public
class
CustomDateConverter implements Converter<String,Date> {
   
@Override
    public Date convert(String source) {
       try {
           SimpleDateFormat simpleDateFormat =
newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
           return simpleDateFormat.parse(source);
       } catch (Exception e) {
           e.printStackTrace();
       }
       return
null
;
    } 
}
1.1.5.2.2      配置方式1

<mvc:annotation-driven
conversion-service="conversionService">
</mvc:annotation-driven>
<!--conversionService -->
   
<bean id="conversionService"
       
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
      
<!-- 转换器 -->
      
<property name="converters">
          
<list>
             
<bean class="cn.itcast.ssm.controller.converter.CustomDateConverter"/>
          
</list>
      
</property>
    </bean>
1.1.5.2.3      配置方式2
<!--注解适配器 -->
   
<bean
      
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property
name="webBindingInitializer"
ref="customBinder"></property>

   
</bean>
   
   
<!-- 自定义webBinder -->
   
<bean id="customBinder"
      
class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
      
<property name="conversionService"
ref="conversionService"
/>
   
</bean>
   
<!--conversionService -->
   
<bean id="conversionService"
      
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
      
<!-- 转换器 -->
      
<property name="converters">
          
<list>
             
<bean class="cn.itcast.ssm.controller.converter.CustomDateConverter"/>
          
</list>
      
</property>
    </bean>

1.1.6 集合类

1.1.6.1 字符串数组

页面定义如下:

页面选中多个checkbox向controller方法传递

<inputtype="checkbox" name="item_id"value="001"/>
<inputtype="checkbox" name="item_id" value="002"/>
<inputtype="checkbox" name="item_id" value="002"/> 
传递到controller方法中的格式是:001,002,003

Controller方法中可以用String[]接收,定义如下:

public String deleteitem(String[] item_id)throws Exception{
        System.out.println(item_id);
} 

1.1.6.2 List

List中存放对象,并将定义的List放在包装类中,action使用包装对象接收。

List中对象:

成绩对象

Public class QueryVo {
Private
List<Items> itemList;//商品列表
 
  //get/set方法..

包装类中定义List对象,并添加get/set方法如下:

页面定义如下: 

<tr>
<td>
<inputtype="text" name=" itemsList[0].id" value="${item.id}"/>
</td>
<td>
<inputtype="text" name=" itemsList[0].name" value="${item.name }"/>
</td>
<td>
<inputtype="text" name=" itemsList[0].price" value="${item.price}"/>
</td>
</tr>
<tr>
<td>
<inputtype="text" name=" itemsList[1].id" value="${item.id}"/>
</td>
<td>
<inputtype="text" name=" itemsList[1].name" value="${item.name }"/>
</td>
<td>
<inputtype="text" name=" itemsList[1].price" value="${item.price}"/>
</td>
</tr> 
上边的静态代码改为动态jsp代码如下:

<c:forEach
items="${itemsList }"
var="item"varStatus="s">
<tr>
   
<td><input
type="text"name="itemsList[${s.index}].name"
value="${item.name }"/></td>
   
<td><input
type="text"name="itemsList[${s.index}].price"
value="${item.price }"/></td>
    .....
    .....
</tr>
</c:forEach>
Contrller方法定义如下: 

public String useraddsubmit(Model model,QueryVo queryVo)throwsException{
}

1.1.6.3 Map

在包装类中定义Map对象,并添加get/set方法,action使用包装对象接收。

包装类中定义Map对象如下:

Public class QueryVo {
private Map<String, Object> itemInfo= new HashMap<String, Object>();
  //get/set方法..
}
页面定义如下:

<tr>
<td>学生信息:</td>
<td>
姓名:<inputtype="text"name="itemInfo['name']"/>
年龄:<inputtype="text"name="itemInfo['price']"/>
.. .. ..
</td>
</tr>
Contrller方法定义如下:

public String useraddsubmit(Modelmodel,QueryVo queryVo)throws Exception{
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: