Java SPRINGMVC Framework Learning (ii) differences between httpserveltrequest and model values

Source: Internet
Author: User

Why do most programs use Model.addattribute () instead of Httpserveletrequest.setattribute () when passing a value to a JSP in a controller?

In fact, the model data, the final spring is also written to the HttpServletRequest attribute, just use model more in line with the MVC design, reduce the inter-layer coupling.

@RequestMapping (value= "/list", method=requestmethod.get) PublicString list (model model) {//the foreground output must be in the form of a ${ul.value. Property name}Model.addattribute ("UserList", userlist); return"User/userlist"; } @RequestMapping (Value= "/userlist", method=requestmethod.get) PublicString list (HttpServletRequest request) {//the foreground output must be in the form of a shape like ${user.id}List<user> userlist =NewArraylist<user>(); Userlist.add (NewUser ("1", "YONGHU1", "121", "[email protected]")); Userlist.add (NewUser ("2", "Yonghu2", "122", "[email protected]")); Userlist.add (NewUser ("3", "Yonghu3", "123", "[email protected]")); Request.setattribute ("UserList", userlist); return"User/userlist"; }

Java SPRINGMVC Framework Learning (ii) differences between httpserveltrequest and model values

Related Article

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.