未来_我来
因为渴望改变,所以必须努力

  12.4 pojo绑定

页面中input的name和controller的pojo形参中的属性名称一致,将页面中数据绑定到pojo。

 

页面定义:

 

controller的pojo形参的定义:

 

打断点测试:

 

 

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

包装对象定义如下:

public class QueryVo {
  private Items items;
}

 

页面定义:

<input type="text" name="items.name" />
<input type="text" name="items.price" />

 

Controller方法定义如下:

public String useraddsubmit(Model model,QueryVo queryVo)throws Exception{
System.out.println(queryVo.getItems());

 

  12.5 自定义参数绑定实现日期类型绑定

 

对于controller形参中pojo对象,如果属性中有日期类型,需要自定义参数绑定。

将请求日期数据串成 日期类型,要转换的日期类型和pojo中日期属性的类型保持一致。

 

 

所以自定义参数绑定将日期串转成java.util.Date类型。

 

需要向处理器适配器中注入自定义的参数绑定组件。

 

    12.5.1 自定义日期类型绑定

 

    12.5.2 配置方式1

springmvc.xml

 

 

 

    12.5.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>

 

  12.6 集合类

    12.6.1 字符串数组

页面定义如下:

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

<input type="checkbox" name="item_id" value="001"/>
<input type="checkbox" name="item_id" value="002"/>
<input type="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);
}

 

 

    12.6.2 List

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

 

List中对象:

成绩对象

 

public class QueryVo {
    private List<Items> itemList;//商品列表
  //get/set方法..
}

 

 

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

 

页面定义如下:

<tr>
  <td>
    <input type="text" name=" itemsList[0].id" value="${item.id}"/>
  </td>
  <td>
    <input type="text" name=" itemsList[0].name" value="${item.name }"/>
  </td>
  <td>
    <input type="text" name=" itemsList[0].price" value="${item.price}"/>
  </td>
</tr>
<tr>
  <td>
    <input type="text" name=" itemsList[1].id" value="${item.id}"/>
  </td>
  <td>
    <input type="text" name=" itemsList[1].name" value="${item.name }"/>
  </td>
  <td>
    <input type="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)throws Exception{
    System.out.println(queryVo.getItemList());
}

 

    12.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(Model model,QueryVo queryVo)throws Exception{
    System.out.println(queryVo.getStudentinfo());
}

 

posted on 2018-01-23 21:51  未来_我来  阅读(212)  评论(0编辑  收藏  举报

2 3
4