接下来看看参数绑定:

默认Conrtroller可以绑定四种参数:HttpServletRequest,HttpServletResponse,HttpSession,Model 

案例:利用request取出传递的参数



//获取参数
    @RequestMapping(value = "/itemEdit.action")
    public ModelAndView toEdit(HttpServletRequest request, HttpServletResponse response, HttpSession session,Model model) {
        //Servlet方式
        String id = request.getParameter("id");
        
        Items items = itemService.selectItemsById(Integer.parseInt(id));
        
        ModelAndView mav = new ModelAndView();
        mav.addObject("item", items);
        mav.setViewName("editItem");
        return mav;
    }



 

然而,其实可以不需要这样取出参数:可以直接绑定:



// 获取参数
    @RequestMapping(value = "/itemEdit.action")
    public ModelAndView toEdit(Integer id, HttpServletRequest request, HttpServletResponse response,
            HttpSession session, Model model) {

        Items items = itemService.selectItemsById(id);
        ModelAndView mav = new ModelAndView();
        mav.addObject("item", items);
        mav.setViewName("editItem");
        return mav;
    }



 

这种简单参数绑定其实还可以用@RequestParam来做:

如果传递的参数是id,而我们非要使用ID来接收,就需要这个注解:

(required默认为true,意味着如果传递参数为空,将会报错)



// 获取参数
    @RequestMapping(value = "/itemEdit.action")
    public ModelAndView toEdit(@RequestParam(value = "id", required = false, defaultValue = "1") Integer ID,
            HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model) {

        Items items = itemService.selectItemsById(ID);
        ModelAndView mav = new ModelAndView();
        mav.addObject("item", items);
        mav.setViewName("editItem");
        return mav;
    }



 

然而如果传递的参数是一个对象(POJO类)?

场景:用户写完博客,设置好分类等信息后,将要发布,设置后传递的参数就是POJO类(博客POJO)

使用限制:传递表单各项的name属性的名字必须和POJO类的属性名一致

 

很简单,直接绑定即可:注意使用限制:



@RequestMapping(value = "/updateitem.action")
    public ModelAndView updateitem(Items items) {
        
        itemService.updateItemsById(items);
        
        ModelAndView mav = new ModelAndView();
        mav.setViewName("success");
        return mav;
    }



 

另外,就写博客这个场景,会出现中文乱码问题,这里顺便解决了:

web.xml中配置filter过滤器:



<!-- 处理POST提交乱码问题 -->
    <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>



 

接下来,绑定包装POJO类:

 简单的包装类包装商品:




spring 如何给list添加数据 spring mvc list参数_ViewUI

spring 如何给list添加数据 spring mvc list参数_java_02

package org.dreamtech.springmvc.pojo;

public class QueryVo {
    
    //商品
    private Items items;

    public Items getItems() {
        return items;
    }

    public void setItems(Items items) {
        this.items = items;
    }
    
}


View Code


 

绑定包装类:

 其实和绑定POJO类相同,不过注意提交表单各项的name属性必须是item.属性名



@RequestMapping(value = "/updateitem.action")
    public ModelAndView updateitem(QueryVo vo) {
        
        itemService.updateItemsById(vo.getItems());
        
        ModelAndView mav = new ModelAndView();
        mav.setViewName("success");
        return mav;
    }



 

 

场景:传递商品时候,可以修改生产日期,而日期的格式未必规范,于是SpringMVC适配器无法辨认

比如我们这里商品POJO类有一个时间属性类型是Date,而表单传过来的一定是String,于是我们想到能否转换?

自定义转换:

首先需要在springmvc配置文件中配置:



<mvc:annotation-driven conversion-service="conversionServiceFactoryBean" />

    <!-- 配置Conveter转换器 转换工厂 -->
    <bean id="conversionServiceFactoryBean"
        class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <!-- 配置 多个转换器 -->
        <property name="converters">
            <list>
                <bean class="org.dreamtech.springmvc.conversion.DateConveter" />
            </list>
        </property>
    </bean>



 

 

DateConveter类:输入String类型数据,返回Date类型数据

 



package org.dreamtech.springmvc.conversion;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.core.convert.converter.Converter;

public class DateConveter implements Converter<String, Date>{

    public Date convert(String source) {

        try {
            if(null != source){
                //转换2018-8-5 11:14:15
                DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                return df.parse(source);
            }
        } catch (Exception e) {

        }
        return null;
    }

}



 

 

参数绑定数组:

 场景:商品页面选定多个商品点击删除按钮,后端接收数组(商品ID)

 



@RequestMapping(value = "/deletes.action")
    public ModelAndView deletes(Integer[] ids) {
        
        itemService.delete(ids);
        
        ModelAndView mav = new ModelAndView();
        mav.setViewName("success");
        return mav;
    }



这时候只需要前端的表单checkbox的name是ids,各项的value分别是item.id即可

 

参数绑定List:

和数组应用场景一致:

 前端代码需要写成示例这样:



<c:forEach items="${itemList}" var="item" varStatus="s">
<tr>
    <td><input type="checkbox" name="ids" value="${item.id }"></td>
    <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>
    <td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td>

</tr>
</c:forEach>



 

绑定List需要写成上边提到的包装类方式:不能直接绑定在参数上



public ModelAndView updateitem(QueryVo vo) {
        //service...
        
        ModelAndView mav = new ModelAndView();
        mav.setViewName("success");
        return mav;
    }



 

只需要QueryVo类有一个List且名为itemList



private List<Items> itemsList;

    public List<Items> getItemsList() {
        return itemsList;
    }

    public void setItemsList(List<Items> itemsList) {
        this.itemsList = itemsList;
    }