Spring MVC supports the following return methods:modelandview, model, modelmap, map, view, string, void.


@requestmapping ("/hello")
  public modelandview helloworld () {
    string message="hello world, spring 3.x!";
    return new modelandview ("hello", "message", message);

The modelandview constructor can specify the name of the page returned.You can also jump to the specified page through the setviewname () method


@requestmapping ("/demo2/show")
  public map<string, string>getmap () {
    map<string, string>map=new hashmap<string, string>();
    map.put ("key1", "value-1");
    map.put ("key2", "value-2");
    return map;

You can get the value directly through ${key1} in the jsp page. map.put () is equivalent to the request.setattribute method.


You can return to pdf excel, etc., for the time being I did not understand in detail.


Specify the name of the view page returned,It can be accessed by setting the return address path and the page name suffix.

Note:If the method declares the @responsebody annotation, the return value will be output directly to the page.

@requestmapping (value="/showdog")
  public string hello1 () {
    return "hello";
@requestmapping (value="/print")
  public string print () {
    string message="hello world, spring mvc!";
    return message;

Example returning json (using jackson):

@requestmapping ("/load1")
  public string load1 (@requestparam string name,@requestparam string password) throws ioexception {
    system.out.println (name + ":" + password);
    //return name + ":" + password;
    mydog dog=new mydog ();
    dog.setname ("");dog.setage ("1 year old");dog.setcolor ("dark gray");
    objectmapper objectmapper=new objectmapper ();
    string jsonstring=objectmapper.writevalueasstring (dog);
    system.out.println (jsonstring);
    return jsonstring;


If the return value is empty,The response view page corresponds to the access address

@requestmapping ("/index")
  public void index () {

The corresponding logical view is named "index"


1. Using string as the return value type of the request processing method is a more general method.The logical view name returned in this way will not be bound to the request url, which has great flexibility.Model data can be controlled through modelmap.

2. When using void, map, model, the real URL of the corresponding logical view name is returned:prefix prefix + view name + suffix suffix.

3. Use string, modelandview to return the view name without being requested by the url binding, modelandview can set the returned view name.

  • Previous Detailed springmvc json data interaction controller method return value is simple type
  • Next jQuery Ajax asynchronous loading display wait effect code sharing