SPRINGMVC Processor return Value object

Source: Internet
Author: User

The use of @controller and @responsebody requires the use of an HTTP message converter (Httpmessageconverter), and the opening of a message converter needs to be done by <mvc:annotation-driven/>. Seven Httpmessageconverter objects are created by default when the spring container is initialized <mvc:annotation-driven/>.

1. Introduction of the JAR package

<!--Jackson-core--        <dependency>            <groupId>com.fasterxml.jackson.core</groupId>            <artifactId>jackson-core</artifactId>            <version>2.8.9</version>        </ Dependency>        <!--jackson-databind--        <dependency>            <groupId> com.fasterxml.jackson.core</groupid>            <artifactId>jackson-databind</artifactId>            < Version>2.9.3</version>        </dependency>

  

2. Create a Processor method (return string type)

Returns the string type    @RequestMapping (value = "/second", produces = "Text/html;charset=utf-8")//produces set encoding format    @ responsebody//Setting the response body public    Object Dosecond () {        return "Dosecond";    }

  

(Returns a custom type)

Returns the custom type    @RequestMapping ("/third")//url    @ResponseBody//sets the response body public    Object Dothird () {        UserInfo U1=new UserInfo ();        U1.setusername ("Away from the Human Sorrow");        return u1;    }

  

(Return collection type)

Returns the collection type    @RequestMapping ("/four")//url    @ResponseBody//sets the response body public    Object Dofour () {        list< Userinfo> list=new arraylist<userinfo> ();        Create two objects        UserInfo u1=new UserInfo ();        U1.setusername ("Away from the Human Sorrow");        UserInfo u2=new UserInfo ();        U2.setusername ("The Most beautiful Expectations");        Added to the list collection        List.add (U1);        List.add (U2);        Returns the list return        list;    

  

3. Configuring the Message Converter

<!--View Resolver--   <bean id= "Methodnameresolver" class= " Org.springframework.web.servlet.view.InternalResourceViewResolver ">       <property name=" prefix "value="/"/ >       <property name= "suffix" value= ". jsp"/>   </bean><!--http Converter on and    <MVC: Annotation-driven/>    <!--scanning package all labeled Class--    <context:component-scan base-package= " Cn.happy.day08Object "/>

  

4. Using AJAX to receive data

<% @page language= "java" pageencoding= "Utf-8" contenttype= "text/html; Utf-8 "%>

  

Springmvc Processor return value object

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.