SPRINGMVC Study notes (+)-SPRINGMVC annotation Development collection type parameter binding

Source: Internet
Author: User

SPRINGMVC Study notes (+)-SPRINGMVC annotation Development collection type parameter binding

Tags: springmvc

    • SPRINGMVC Learning Note 13-SPRINGMVC Annotation Development collection type parameter binding
      • Array binding
        • Demand
        • Presentation Layer Implementation
      • List bindings
        • Demand
        • Presentation Layer Implementation
      • Map bindings

This article mainly introduces the collection type parameter bindings for annotation development, including array bindings, list bindings, and map bindings.

Array binding requirements

Product batch deletion, the user selects multiple items on the page. Bulk Delete.

Presentation Layer Implementation

Key: The page selection (multiple selection) of the product ID, to the controller method of the form, the method to use the array to receive a page request for multiple product IDs.

    • Controller method Definition:
// 批量删除 商品信息@RequestMapping("/deleteItems")publicdeleteItemsthrows Exception
    • Page Definition:
<c:foreach Items="${itemslist}" var="Item"><tr>    <TD><input type="checkbox" name= "items_id" value="${ Item.id} "/></td>    <TD>${item.name}</td>    <TD>${item.price}</td>    <TD><fmt:formatdate value="${item.createtime}" pattern="Yyyy-mm-dd HH: Mm:ss "/></td>    <TD>${item.detail}</td>    <TD><a href="${pagecontext.request.contextpath}/items/edititems.action?id=${ Item.id} ">Changes</a></td></tr></C:foreach>
List binding Requirements

Usually when you need to submit data in bulk. Bind the submitted data to list<pojo> , for example: Score entry (enter multiple course results. Batch submission),

This sample needs: bulk product changes. Enter multiple product information on the page. Submits multiple product information to the Controller method.

Presentation Layer Implementation
    • Controller method Definition:
      • 1, enter the bulk Product change page (page style reference Product list implementation)
      • 2, Batch change product submission

Use the list to receive the bulk data submitted by the page, receive through the wrapper Pojo, define the attributes in the wrapper Pojo list<pojo>

public  class  itemsqueryvo  { //product information  private  items items; //for System extensibility.    Extend the original generated PO  private  itemscustom itemscustom;    //bulk product information  private  List<itemscustom> itemslist;  
// 批量改动商品提交// 通过ItemsQueryVo接收批量提交的商品信息,将商品信息存储到itemsQueryVo中itemsList属性中。@RequestMapping("/editItemsAllSubmit")publiceditItemsAllSubmitthrows Exception {    return"success";}
    • Page Definition:
<c:foreach Items="${itemslist}" var="Item" varstatus ="status">    <tr>        <TD><input name="Itemslist[${status.index}].name" value="${item.name} "/></td>        <TD><input name="Itemslist[${status.index}].price" value="$ {item.price} "/></td>        <TD><input name="Itemslist[${status.index}].createtime" value="< Fmt:formatdate value= "${item.createtime}" pattern="Yyyy-mm-dd HH:mm:ss"/> "/></td>        <TD><input name="Itemslist[${status.index}].detail" value="${ Item.detail} "/></td>    </tr></C:foreach>

The format of name:

相应包装pojo中的list类型属性名[下标(从0開始)].包装pojo中List类型的属性中pojo的属性名

Examples:

"name="itemsList[${status.index }].price"

Can and packaging type of the binding induction control, in fact, in the packaging class Pojo based on a number of subscript. It is only the wrapper class parameter binding, which is consistent with the attribute name of the Pojo class in the wrapper Pojo, and the list parameter is bound. To match the property name of the list type in the wrapper Pojo.

Map bindings

The map type attribute is also defined in the wrapper Pojo.

Defines the map object in the wrapper class and joins the Get/set method, which is received by the action using the wrapper object.

    • The wrapper class defines the map object such as the following:
Public class QueryVo {privatenew HashMap<String, Object>();  //get/set方法..}
    • The page definition is as follows:
<tr><td>学生信息:</td><td>姓名:<inputtype="text"name="itemInfo[‘name‘]"/>年龄:<inputtype="text"name="itemInfo[‘price‘]"/>.. .. ..</td></tr>
    • Contrller method definitions such as the following:
publicuseraddsubmit(Model model,QueryVo queryVo)throws Exception{System.out.println(queryVo.getStudentinfo());}

Author @brianway Many other articles: personal site | CSDN | Oschina

SPRINGMVC Study notes (+)-SPRINGMVC annotation Development collection type parameter binding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.