SPRINGMVC Note Series (13)--Modelandview object of the Model data processing article

Source: Internet
Author: User

Description: The content of this article is reproduced to: https://my.oschina.net/happyBKs/blog/420310
If there is infringement of the place, please contact me, I will be immediately deleted.

Previous articles covered various annotations for processing requests, as well as content such as Pojo object reception and servlet native API parameter type support, which mainly addressed how to receive and map request data.

Next we'll focus on how to process and return model data. Before our controller class's processing method returned is a string, to generate our final physical URL path. There are several types of model data that can be exported.

Spring MVC provides several ways to output the model data:

Modelandview: The method body can add model data through the object when the processing method returns a value of type Modelandview

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 with a method in the parameter, the object of the entry is placed in the data model

Starting from this article, we enter a small link, is the Model data processing, this chapter first introduced the Modelandview.

Modelandview

We write a method of the Controller class as follows, we construct a Modelandview object, and the corresponding return segment of the final physical URL as the construction parameter (actually is the original return string, now as the Modelandview constructor method parameters). SPRINGMVC will process the request and turn to the destination URL as before. So why do we use Modelandview objects? This is because we can add some model data to the Modelandview object, and SPRINGMVC will put the models data in the Modelandview into the request domain object.

Source parsing conclusion: in fact, regardless of whether the processing method returns a string, a modelandview, or any type above, SPRINGMVC internally translates to a modelandview.

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

• Add model data:

–moelandview AddObject (String attributename, Object AttributeValue)

–modelandview Addallobject (Map

package com.happyBKs.springmvc.handlers;

Import Java.util.Date;
Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;

Import Org.springframework.web.servlet.ModelAndView;

Import com.happyBKs.springmvc.beans.Location; @RequestMapping ("/model") @Controller public class Modeldatahandler {/* * Target method return value allows Modelandview type * to contain the view
     Figure and model information * SPRINGMVC will put the model data in the Modelandview into the request domain object. * * It is important to note that, in fact, the processing function, regardless of the return String,modelandview,map type, SPRINGMVC internally will be automatically converted to Modelandview * */@RequestMapping
        ("/modelandview") public Modelandview Handle1 () {String restr= "modelandviewtest";
        Modelandview mv=new Modelandview (RESTR);
        Mv.addobject ("Time", New Date ());
        Location Loc=new location ();
        Loc.setcity ("urban");
        Loc.setprovince ("Shanghai");
        Mv.addobject ("local", loc);
    return MV; }

}

Target page:/views/modelandview.jsp (Model/modelandview)

<%@ page language= "java" contenttype= "text/html; Charset=iso-8859-1 "
    pageencoding=" iso-8859-1 "%>
<%@ page iselignored=" false "%>  
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

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.