SPRINGMVC Combat (iv)-processing model data

Source: Internet
Author: User
Tags bind http request

Spring MVC provides several ways to output model data: Modelandview: Processing method when the return value type is Modelandview, the method body can add model data through the object Map and Model: When the processing method returns, when the entry is Org.springframework.ui.Model, Org.springframework.ui.ModelMap, or JAVA.UTI.MAP, the map Data is automatically added to the model. @SessionAttributes: A property in the model is staged in HttpSession so that the property can be shared between multiple requests @ModelAttribute: When the annotation is annotated by a method in the parameter, the object in the parameter is placed in the data model Modelandview Overview

The return value of the controller processing method, if Modelandview, contains both the view information and the model data information.

To Add model data: Moelandview AddObject (String attributename, Object attributevalue) Modelandview addallobject (map< string,?> MODELMAP)

set view: void Setview (view view) void Setviewname (String viewName) Example

register_form.jsp

<! DOCTYPE html>

Modelcontroller.java

Package Com.ricky.codelab.webapp.ch3;

Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.servlet.ModelAndView;
Import Com.ricky.codelab.webapp.ch2.model.User;

@Controller
@RequestMapping ("model") public
class Modelcontroller {

    @RequestMapping ("register")
    Public Modelandview Register (user user) {

        modelandview mv = new Modelandview ("Home");
        Mv.addobject ("user", user);

        return mv;
    }
}

home.jsp

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
    pageencoding=" UTF-8 "%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
Map and Model Overview

Spring MVC internally uses a Org.springframework.ui.Model interface to store model data.

Spring MVC creates an-implicit model object as a storage container for model data before invoking the method. If the method's entry is a MAP or model type, Spring MVC implicitly passes the reference of the model to those arguments. In the body of the method, the developer can access all the data in the model through this entry object, or they can add new attribute data to the model. Example

1. Map

Package Com.ricky.codelab.webapp.ch3;

Import Java.util.Map;
Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Com.ricky.codelab.webapp.ch2.model.User;

@Controller
@RequestMapping ("model") public
class Modelcontroller {

    @RequestMapping ("register")
    Public String Register (user user, map<string, object> Map) {

        map.put ("user", user);

        Return "Home";
    }
}

2. Model

Package Com.ricky.codelab.webapp.ch3;

Import Org.springframework.stereotype.Controller;
Import Org.springframework.ui.Model;
Import org.springframework.web.bind.annotation.RequestMapping;

Import Com.ricky.codelab.webapp.ch2.model.User;

@Controller
@RequestMapping ("model") public
class Modelcontroller {

    @RequestMapping ("register")
    Public String Register (user user, model model) {

        Model.addattribute ("user", user);

        Return "Home";
    }
}
Internal Implementation Details

How does SPRINGMVC set the Modelandview to the request domain?
We can take a look at the process it executes through debug:



In the Exposemodelasrequestattributes method of Org.springframework.web.servlet.view.AbstractView, we see the following code:

/** * Expose the Model objects in the given map as request attributes.
     * Names'll is taken from the model Map.
     * This method is suitable to all resources reachable by {@link Javax.servlet.RequestDispatcher}. * @param model MAP of Model objects to expose * @param request current HTTP request */protected void expose Modelasrequestattributes (map<string, object> model, HttpServletRequest request) throws Exception {for (MAP).
            Entry<string, object> Entry:model.entrySet ()) {String modelname = Entry.getkey ();
            Object Modelvalue = Entry.getvalue ();
                if (modelvalue! = null) {Request.setattribute (modelname, Modelvalue); if (logger.isdebugenabled ()) {Logger.debug ("Added model Object" + modelname + "' of type [" + Model
                Value.getclass (). GetName () + "] to request in view with name ' + getbeanname () +" ' ");
 }           } else {Request.removeattribute (modelname);
                            if (logger.isdebugenabled ()) {logger.debug ("removed model object" + ModelName +
                "' from request in view with Name '" + getbeanname () + "'"); }
            }
        }
    }

It iterates through each of the model's Key-value key-value pairs and calls Request.setattribute (ModelName, Modelvalue), and the method sets them to the request domain.

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.