SSM之springMVC(3/4)

时间:2019-03-19
本文章向大家介绍SSM之springMVC(3/4),主要包括SSM之springMVC(3/4)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

上一节 SSM之springMVC(2/4)

目录

包装类型pojo参数绑定

需求

实现方法

页面参数和controller方法形参定义

集合类型绑定

数组绑定

需求

实现

List绑定

需求

实现

Map绑定

服务器校验

数据回显

什么是数据回显

Pojo数据回显方法


包装类型pojo参数绑定

需求

商品查询controller方法中实现商品查询条件传入。

实现方法

第一种方法:在形参中添加HttpServletRequest request参数,通过request接收查询条件参数。

第二种方法:在形参中让包装类型的pojo接收查询条件参数。

       分析:

       页面传参数的特点:复杂,多样性。条件包括:用户账号、商品编号、订单信息…

       如果将用户账号、商品编号、订单信息等放在简单pojo(属性是简单类型)中,pojo类属性比较多,比较乱。

       建议使用包装类型的pojo,pojo中属性是pojo。

页面参数和controller方法形参定义

页面参数:

<input name="itemsCustom.name"/>

注意:itemsCustom和包装pojo中的属性一致即可。

Controller方法形参:

public String findItemsList(HttpServletRequest request,ItemsQueryVo itemsQueryVo) throws Exception

集合类型绑定

数组绑定

需求

商品批量删除,用户在页面选择多个商品,批量删除。

实现

<script type="text/javascript">

    function findItemsList(){ document.itemsForm.action="${pageContext.request.contextPath }/items/findItemsList.action";

        document.itemsForm.submit();

    }

    function deleteItems(){

    document.itemsForm.action="${pageContext.request.contextPath }/items/deleteItems.action";

        document.itemsForm.submit();

    }

</script>

<input type="button" value="查询" onclick="findItemsList()"/>

    <input type="button" value="批量删除" onclick="deleteItems()"/>

<td><input type="checkbox" name="items_id" value="${item.id}"/></td>

//批量删除

@RequestMapping("deleteItems")

public String deleteItems(Integer[] items_id)throws Exception{

    if(items_id.length>0){

        for(Integer id:items_id){

            itemsService.deleteByPrimaryKey(id);

        }

    }

    return "redirect:findItemsList.action";

}

List绑定

需求

通常在需要批量提交数据时,将提交的数据绑定到list<pojo>中。比如:成绩录入,批量提交,

本例子需求:批量商品修改,子啊页面输入多个商品信息,将多个商品信息提交到controller方法中。

实现

<script type="text/javascript">

    function updateAllItems(){ document.itemsForm.action="${pageContext.request.contextPath }/items/updateItemsAll.action";

        document.itemsForm.submit();

    }

</script>

<input type="button" value="确定修改" onclick="updateAllItems()"/>

<c:forEach items="${itemsList }" var="item" varStatus="status">

<tr>

    <td><input type="checkbox" name="itemsCustomsList[${status.index }].id" value="${item.id}"/></td>

    <td><input name="itemsCustomsList[${status.index }].name" value="${item.name }"></td>

    <td><input name="itemsCustomsList[${status.index }].price" value="${item.price }"></td>

    <td><input name="itemsCustomsList[${status.index }].createtime" value="${item.createtime }"></td>

    <td><input name="itemsCustomsList[${status.index }].detail" value="${item.detail }"></td>

</tr>

</c:forEach>

在ItemsQueryVo.java中添加private List<ItemsCustom> itemsCustomsList;

ItemsServiceImpl.java

@Override

public void updateItemsAll(ItemsQueryVo itemsQueryVo) throws Exception {

    if(itemsQueryVo!=null){

        List<ItemsCustom> list=itemsQueryVo.getItemsCustomsList();

        for(ItemsCustom i:list){

            itemsMapper.updateByPrimaryKeyWithBLOBs(i);

        }

    }

}

ItemsControllerjava

// 批量修改

@RequestMapping("/updateAllItemsList")

public String updateAllItemsList(HttpServletRequest request) throws Exception {

    List<ItemsCustom> itemsCustomsList = itemsService.findItemsList(null);

    request.setAttribute("itemsList", itemsCustomsList);

    return "itemsUpdateAll";

}

@RequestMapping("/updateItemsAll")

public String updateItemsAll(ItemsQueryVo itemsQueryVo) throws Exception {

    itemsService.updateItemsAll(itemsQueryVo);

    return "redirect:findItemsList.action";

}

Map绑定

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

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

Public class QueryVo{

       Private Map<String, Object> itemInfo=new HashMap< String, Object >();

       //get/set方法

}

 

页面定义如下:

<tr>

<td>学生信息:</td>

<td>

姓名:<input type=”text” name=”itemInfo[‘name’]”/>

年龄:<input type=”text” name=”itemInfo[‘price’]”/>

… …

</td>

</tr>

 

Controller方法定义如下:

Public String useraddsubmit(Model model,QueryVo queryVo) throws Exception{

System.out.println(queryVo.getStudentinfo());

}

服务器校验

https://blog.csdn.net/qq_40802448/article/details/88655866

数据回显

什么是数据回显

提交后,如果出现错误,将刚才提交的数据回显到刚才的提交页面。

Pojo数据回显方法

1、使用@ModelAttribute指定pojo回显到页面在request中的key

public String editItemsSubmit(Model model,HttpServletRequest request, Integer id,

            @ModelAttribute("itemsList") @Validated(value={ValidGroup1.class}) ItemsCustom itemsCustom,BindingResult bindingResult) throws Exception { ... }

2、@ModelAttribute还可以将方法的返回值传到页面

在商品查询列表页面,通过商品类型查询商品信息。

在controller中定义商品类型查询方法,最终将商品类型传到页面。

// 商品分类

// itemtypes 表示最终将方法返回值放在request中的可以,根据这个值在jsp页面取数据;

@ModelAttribute("itemtypes")

public Map<String, String> getItemTypes() {

    Map<String, String> itemTypes = new HashMap<String, String>();

    itemTypes.put("101", "数码");

    itemTypes.put("102", "母婴");

    return itemTypes;

}

商品类型:<select name="itemtype">

            <c:forEach items="${itemtypes }" var="itemtype">

                <option value="${itemtype.key }">${itemtype.value }</option>

            </c:forEach>

        </select>

3、使用最简单方法使用model,可以不用@ModelAttribute

model.addAttribute("itemsList", objectErrors);即可

 

 

下一节 SSM之springMVC(3/4)