Spring MVC Data Echo

Source: Internet
Author: User

SPRINGMVC Data Echooriginal August 02, 2015 11:39:00
    • Label:
    • Springmvc
1 Data echo 1.1 What data echo

After submission, if an error occurs, the data just submitted is echoed to the submission page just now.

That is, the form submission failure does not need to return to the form page to re-fill, the original submitted data needs to be displayed on the page again.

1.2 Pojo Data Echo method

1, SPRINGMVC default to Pojo data echo.

Pojo data is passed into the controller method, Springmvc automatically puts Pojo data into the request domain,key equals pojo type (lowercase first letter)

That is, the identifier in the Modify information Edititems () method is Model.addattribute ("itemscustom", itemscustom);

       edititems.jsp The page receives an identity of   <input type= "hidden" name= "id" value= " ${ Itemscustom.id} />

the method of committing the modification public String edititemssubmit (modelmodel,httpservletrequest request,integer ID,@Validated ( Value={validgrouop1. Class}) itemscustom itemscustom,bindingresult Bindingresult)throws Exception

The three parties can automatically echo the same

Use the @ModelAttribute to specify Pojo Echo to the page key in Request

1. Bind request parameters to Pojo and expose the model data to the view page

This method enables data echo effects.

Theitem in the @ModelAttribute ("item") corresponds to the alias of the Itemscustom of the Itemscustom Itemscustom, which is used to hold the "itemscustom" of the page. ${Item.name}" Item consistently implements data echo

Product Modification Submission

@RequestMapping ("/edititemsubmit")

Public String Edititemsubmit (model model, @ModelAttribute ("item") Itemscustom Itemscustom)

Page:

<tr>

<TD> Product name </td>

<TD><input type="text"name="name" value="${ Item.name} " /></td>

</tr>

<tr>

<TD> Commodity Price </td>

<TD><input type="text"name="Price" value=" ${Item.price}"/></td>

</tr>

You can also use Model.addattribute ("item", Itemscustom) to complete data echoing without @modelattribute.

2. @ModelAttribute can also upload the return value of the method to the page

On the Product Query list page, check the product information by product type.

The Product Type Query method is defined in the controller, and the product type is eventually uploaded to the page.

// commodity classification

//itemtypes indicates that the return value of the method is eventually placed in the request key

@ModelAttribute ("Itemtypes")

Public Map<string, String>getitemtypes () {

map<string,string> itemtypes = new hashmap<string,string> ();

Itemtypes.put ("101", " digital ");

Itemtypes.put ("102", " Mother and Baby ");

return itemtypes;

}

Itemtypes data can be obtained on the page.

Product Categories:

<Select name= "ItemType" >

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

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

</C:foreach>

</Select>

3, use the simplest method to use model, can not @modelattribute

@RequestMapping ("/edititemssubmit")

Public Stringedititemssubmit (Model model

, HttpServletRequest request,

Integerid,

@ModelAttribute ("items")@Validated (VALUE={VALIDGROUOP1. Class}) Itemscustom Itemscustom,

Bindingresultbindingresult)throwsexception{

// Get validation error message

if (Bindingresult.haserrors ())

{

// output error message

List<objecterror>allerrors=bindingresult.getallerrors ();

for (Objecterror error:allerrors)

{

System. out. println (Error.getdefaultmessage ());

}

// error message delivery to page

Model.addattribute ("Allerrors", allerrors);

// use model to make data echo

Model.addattribute ("items", itemscustom);

return "Items/edititems";

}

// call service to update the product information, the page needs to upload the product information to this method

Itemsservice.updateitems (id,itemscustom);

//redirect no plus path

//return "Redirect:queryItems.action";

// page forwarding

return "Forward:queryItems.action";

}

1.3 Simple Type data echo

Use the model in the simplest way.

Model.addattribute ("id", id);

For your own reference only, if offended please leave a message contact

reprinted from: 47205657

Spring MVC Data Echo

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.