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