Return value of the processor method in Ssm-springmvc-21:springmvc object

Source: Internet
Author: User
Tags object object

------------I do not have him, but the hand is ripe, humble and foolish, and eager to be hungry-------------

Today to record is the processing method, the return value is the kind of object, I gave it a bit of the class:

1. The return value is an object value (for example, 1), 2. A string type that returns a value of object (for example, "I am not Chinese"), 3. Returns the object type with the value object (for example, a custom UserInfo type Object ). 4. Return a collection type with a value of object (for example, a list collection of userinfo)

Requires annotations in a core:<mvc:annotation-driven></mvc:annotation-driven> configuration file to drive this node

and jar Bag, Jackson's

        <dependency>            <groupId>com.fasterxml.jackson.core</groupId>            <artifactId> jackson-core</artifactid>            <version>2.8.  1</version>        </dependency>        <dependency>            <groupId> com.fasterxml.jackson.core</groupid>            <artifactId>jackson-databind</artifactId>            < version>2.5. 1</version>        </dependency>

So: Start a case, (the code used in a case is not repeated in two, three, four cases, and so on, and will simply mention it inside)

One, the return value is an object value

1. First prepare a processor, which adds case one to the processing method

Package Cn.dawn.day12return;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestbody;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.responsebody;import Java.util.arraylist;import java.util.List;/** * Created by Dawn on 2018/3/28.*/@Controller Public classReturnobjectcontroller {/*return value object numeric type*/@RequestMapping ("/returnobjint") @ResponseBody PublicObject Returnobjint () {return 1; }}

2. An XML configuration file is required: (unlike in the past, note drivers need to be configured)

<?xml version="1.0"encoding="UTF-8"? ><beans xmlns="Http://www.springframework.org/schema/beans"Xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"Xmlns:mvc="Http://www.springframework.org/schema/mvc"Xmlns:context="Http://www.springframework.org/schema/context"xsi:schemalocation="Http://www.springframework.org/schema/beanshttp//www.springframework.org/schema/beans/spring-beans.xsdhttp//Www.springframework.org/schema/mvchttp//www.springframework.org/schema/mvc/spring-mvc.xsdhttp//Www.springframework.org/schema/contexthttp//www.springframework.org/schema/context/spring-context.xsd "><!--package Scanner--<context:component-scanBase-package="Cn.dawn.day12return"></context:component-scan> <!--View Resolver-<beanclass="Org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"Value="/"></property> <property name="suffix"Value=". JSP"></property> </bean> <!--note-driven <mvc:annotation-driven></mvc:annotation-driven> </beans>

3.web.xml Dispatch the central scheduler to the XML above, you will not give the source.

4. Go to Web URL to access it, OK

Two, a string type that returns a value of object

1. Treatment Method:

    /* returns the value of the string type of object */ "/returnobjstr" "Text/html;charset=utf-8"        )    @ResponseBody    public  Object returnobjstr () {        return  " I am not Chinese?" ";    }

This out specifies the encoding of the returned page, Utf-8, if not returned will be garbled

2. Own configuration xml,web.xml with the above, because the processing method, in the above a processor class

3. Access via URL

Three, object type with return value of

1. Define a UserInfo entity class;

Package Cn.dawn.day12return;/** * Created by Dawn on 2018/3/26.*/ Public classUserInfo {PrivateString username; PrivateString password;  PublicUserInfo () {} PublicUserInfo (string Username, string password) { This. Username =username;  This. Password =password; }     PublicString GetUserName () {returnusername; }     Public voidSetusername (String username) { This. Username =username; }     PublicString GetPassword () {returnpassword; }     Public voidSetPassword (String password) { This. Password =password; }}

2. Defining the Processor method

    /* The return value of the object objects user type */  "/returnobjuser")    @ResponseBody    public      Object Returnobjuser () {        UserInfo u1=new UserInfo (" Mengliu " ,"123");         return U1;    }

3. Custom configuration XML file, Web.

4. Access via web URL

Four, a collection type that returns a value of object

1.UserInfo with the above is good:

2. Define the processing method:

    /*returns the collection of Value object object user*/@RequestMapping (Value="/returnobjuserlist") @ResponseBody PublicObject returnobjuserlist () {List<UserInfo> lists=NewArraylist<userinfo>(); UserInfo U1=NewUserInfo ("Mengliu","123"); UserInfo U2=NewUserInfo ("Meng Qi","123");        Lists.add (U1);        Lists.add (U2); returnlists; }

3. Custom configuration XML file, Web.

4. Define a JSP page where I write Ajax and parse the returned List<userinfo> object

<%@ page language="Java"pageencoding="Utf-8"Iselignored="false"%>"Text/javascript"Src="${pagecontext.request.contextpath}/js/jquery-1.8.3.min.js"></script> <script type="Text/javascript">$ (function () {$ ("input"). On ("Click", function () {$.ajax ({URL:"${pagecontext.request.contextpath}/returnobjuserlist", success:function (date) {alert (typeofdate);                        $.each (Date,function (i, item) {alert (item.username);            })                    }                });        });    }); </script>"image/1.jpg">--%><input type="Button"Value="I am in a windowless black house, living six people, a person all night writing code, Comfort my Lonely heart"/></body>

5. Start Tomcat, access the JSP, click Use

Return value of the processor method in Ssm-springmvc-21:springmvc 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.