Java uses JSON to achieve background and front-end division of Employment (Code instance)

Source: Internet
Author: User

First, the preface

Recently in writing our laboratory website internal Communication platform, background architecture with the Spring + SPRINGMVC + mabatis (Database framework) Integration Framework, as a background, and database interaction, processing business logic has been very adept, but with the front-end cooperation is not so skilled.

To give a small example, a list of users, we take out from the database is a list<user>, but it is in front of the display is a very cool user list, we need to put the data in the form of table style and let the front end staff to add style, Or the front end of the style to leave out the interface by the backstage staff to give the data.

Just contact with the development of the Web site may have similar problems, we are much hope that the front-end and backstage work is clearly AH.

It doesn't have to be so complicated that JSON can help us.

Background just tell the front end, request a URL address can get the user data JSON format data, the front-end in need of the time to request their own good, the rest of the work from the front to do good, clear, clean.


Ii. preparatory work

Jar Pack Preparation

CSDN is very powerful, good people are also a lot of Kazakhstan, I hope that we share a lot, and common progress ha ... The following jar package download address is OK ...

http://download.csdn.net/detail/zhangy0329/607838


Third, code examples

A section of the Interceptor Code for the Controller controller (note explained in detail)

<span style= "White-space:pre" > </span>/** * Test get all Users list * @param request * @return String * @throws IOE Xception */@RequestMapping ("/getallusertest") public String getallusertest (httpservletrequest request, Httpservletre Sponse response) throws ioexception{//Remove all user information from the database, the return value is a collection of a user object that contains username and age two parameters list<user> fin
		
		DAll = Userservice.findall ();
		Create Jsonarray instance Jsonarray Jsonarray = new Jsonarray (); For Each loop to remove each user object for (user User:findall) {//jsonobject is a {} wrapped object (object),//jsonarray is a [] wrapped array (Ar
             Ray)//Here is the object, so use get jsonobject jsonobject Jo = new Jsonobject ();
             Jo.put ("username", user.getusername ());
             Jo.put ("Age", User.getage ());
        Jsonarray.add (Jo);
             try {//Background output test System.out.println (jsonarray.tostring ());
             Sets the character set response.setcharacterencoding ("UTF-8"); Page Output Response.getwriter (). PrintlN ("JSON output form:");
             
         Response.getwriter (). Write (jsonarray.tostring ());
         catch (IOException e) {e.printstacktrace ();
	return null;
	 }

Then the front-end request the corresponding URL to get all the user information.

The effect is as follows:




If you can, the project to a certain extent will be the entire project source sharing, of course, now only test demo, thank you.

Related Article

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.