Play Spring MVC (eight)----Spring MVC integration JSON

Source: Internet
Author: User
Tags button type

This article is based on the front of the project to integrate JSON, the latest project information see: http://download.csdn.net/detail/u012116457/8436187

First needed jar package: Jackson-core-asl-1.7.2.jar Jackson-mapper-asl-1.7.2.jar

Then the configuration file Json-servlet.xml, which is configured in Web. XML to be called when Tomcat starts:

<?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:p= "http://www.springframework.org/schema/p" xmlns:mvc= " Http://www.springframework.org/schema/mvc "xmlns:context=" Http://www.springframework.org/schema/context "xmlns: Util= "Http://www.springframework.org/schema/util" xsi:schemalocation= "http://www.springframework.org/schema/ Beans Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd Http://www.springframework.org/schem A/context http://www.springframework.org/schema/context/spring-context-3.0.xsd Http://www.springframework . ORG/SCHEMA/MVC http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd Http://www.springframewo      Rk.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd "> <!--configuration support for JSON-- <bean id= "Mappingjacksonhttpmessageconverter" class= "org.springFramework.http.converter.json.MappingJacksonHttpMessageConverter "> <!--in order to process the encoding of the returned JSON data, the default is Iso-88859-1, which                      Set it to UTF-8, solve garbled situations--<property name= "Supportedmediatypes" > <list>        <value>text/html;charset=UTF-8</value> </list> </property> </bean> </beans>
The control classes are as follows:
Package Module.controller;import Javax.annotation.resource;import Javax.servlet.http.httpservletrequest;import Module.entity.user;import Module.service.loginservice;import Org.apache.log4j.logger;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestmethod;import Org.springframework.web.bind.annotation.ResponseBody; @Controller @requestmapping ("/login") public class Logincontroller {private static String loginsuccess= "Module/jsp/index";//@Autowired () @Qualifier ("LoginServiceImpl2 ") @Resource (name=" LOGINSERVICEIMPL2 ") private Loginservice Loginservice; @RequestMapping (value ="/login.do ", method = Requestmethod.post) @ResponseBody//This note indicates that the return value skips the View Processing section and writes directly to the HTTP response body in public user login (user user) { System.out.println ("User:" +user.getname () + "Request Login"); User U = Loginservice.findbyname (User.getname ()), if (u==null) {u=new user (); U.setname ("");} return u;} @RequestMapping ("/loginsuccess.do") Public String Start (HttpServletRequest request) {return loginsuccess;}} 
In spring, if a @requestmapping annotation is used, it is mapped to the actual URL based on the return value of the method, and if the @responsebody annotation is also used, the View Processing section is skipped and the return value is written directly to the HTTP response Body, return it to the client page. As the above code, the data in user format is automatically converted to JSON format to return to the client.

Simply put, if you need to return JSON data, use @responsebody to annotate the method with the return type as the entity class.

Here is the JSP page:

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><%string path = Request.getcontextpath () ; String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML >


Play Spring MVC (eight)----Spring MVC integration JSON

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.