SPRINGMVC return to map and return JSON example

Source: Internet
Author: User

Spring MVC supports the following return methods: Modelandview, Model, Modelmap, Map,view, String, void.

Map

@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;     

The value can be obtained straight through ${key1} in the JSP page, and Map.put () is equivalent to the Request.setattribute method.

The page reads:

<c:forEach items="${map}"var="entry">

        <c:out value= "${entry.key}" />          <c:out value= "${entry.value}" /> </c:forEach>

Return to the JSON example (using Jackson):

@RequestMapping ("/load1")    @ResponseBody 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 ("Xiao Ha");d og.setage ("1 years old");d Og.setcolor ("dark grey");        Objectmapper objectmapper = new Objectmapper ();        String jsonstring=objectmapper.writevalueasstring (dog);        System.out.println (jsonstring);        return jsonstring;    }

Summary:
1. The return value type using String as the request processing method is a more general method, so the returned logical view name does not bind to the request URL, and the model data can be controlled by Modelmap.
2. When using Void,map,model, return the corresponding logical view name real URL is: prefix prefix + view name +suffix suffix.

<bean
class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name= "prefix" value= "/web-inf/jsp/"/>
<property name= "suffix" value= ". jsp"/>
<property name= "Viewclass"
Value= "Org.springframework.web.servlet.view.JstlView"/>
</bean>


3. Using String,modelandview to return the view name can be not bound by the requested URL, Modelandview can set the returned view name.

SPRINGMVC return to map and return JSON example

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.