SPRINGMVC Classic Series-13 using SPRINGMVC to process AJAX requests---"Linuszhu"

Source: Internet
Author: User

Note: This article is a personal original, I hope to have reproduced the need for friends to mark the source of the article, assuming that all friends think it is good to write, give a praise ha, your encouragement is the greatest power of my creation, Linuszhu Thank you very much, of course, if there are flaws in the article, please contact [email protected] , please friends treatise, thank you.

This section mainly explains how Springmvc handles Ajax requests, first of all to explain the Jackson class Library, to help us in the Java convert objects to and from json,XML data. He was able to convert the objects returned by the controller directly into JSON data for client use and the client to transfer JSON data to the server for conversion , it is very convenient.

The detailed environment construction process is as follows:

1. Download the two Jackson jar Pack:

Jackson-core-asl-1.7.2jar, Jackson-mapper-asl-1.7.2jar

2. Add the following sections in the SPRINGMVC configuration file, for example:

< Mvc:annotation-driven /> <!-- support spring3.0 new MVC Annotations- -

<!-- start the annotation function for Spring MVC , complete the mapping of the request and annotation POJO -

<Bean class=" Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter ">

<property name="cacheseconds" value= c8> "0" />

< property name="messageconverters" >

<list >

<Bean class="Org.springframework.http.converter.json." Mappingjacksonhttpmessageconverter "></bean>

</list >

</Property>

</bean >

3. The client code ajaxtest.jsp such as the following:

<%@page language="java" contentType="text/html; Charset=utf-8 "

pageencoding = "UTF-8" %>

<! DOCTYPE HTML  Public "-//w3c//dtd HTML 4.01 transitional//en" >

< HTML >

< Head >

< title > Ajax </ title >

< META http-equiv = "Content-type" CONTENT = "Text/html;charset=utf-8" >

< Script >

function createajaxobj () {

var req;

if (window. XMLHttpRequest) {

req = New XMLHttpRequest ();

} else {

req = New ActiveXObject ("msxml2.xmlhttp"); //ie

}

return req;

}

function Sendajaxreq () {

var req = Createajaxobj ();

Req.open ("Get", "Myajax.do?method=test1&uname= Zhang San");

Req.setrequestheader ("Accept", "Application/json");

Req.onreadystatechange = function () {

eval ("var result=" + req.responsetext);

document.getElementById ("Div1"). InnerHTML = Result[0].uname + "==="

+ result[1].uname + "===" + result[2].uname;

};

req.send (null);

}

</ Script >

</ Head >

< Body >

< a href="javascript:void (0);" onclick = "Sendajaxreq ();" > Ajax Test </ a >

< Div id="Div1"></div>

</ Body >

</ HTML >

4. Server-side code such as the following:

Package Com.spring.ajax;

Import java.util.ArrayList;

Import java.util.List;

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;

Import Com.spring.bean.User;

@Controller

@RequestMapping ("Myajax.do")

public class Myajaxcontroller {

@RequestMapping (params = "Method=test1")

Public @ResponseBody

List<user> test1 (String uname) throws Exception {

String uname2 = new String (Uname.getbytes ("iso8859-1"), "GBK");

System.out.println (uname2);

System.out.println ("Myajaxcontroller.test1 ()");

list<user> list = new arraylist<user> ();

User U1 = new user ();

U1.setid (111);

U1.setuname (" test 1");

User U2 = new user ();

U2.setid (222);

U2.setuname (" test 2");

User U3 = new user ();

U3.setid (333);

U3.setuname (" Test 3");

List.add (U1);

List.add (U2);

List.add (U3);

return list;

}

}

5. Project execution test.

Enter Address: http://localhost:8081/SpringMVC02/ajaxTest.jsp , the interface such as the following:

Click on the link to test, will invoke the background program, the results are returned to the page, such as the following:

SPRINGMVC Classic Series-13 using SPRINGMVC to process AJAX requests---"Linuszhu"

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.