First, Introduction
This article uses a small demoof Springmvc and Ajax toimplement the JSON The object returned to the page with no technical content, purely because the SPRINGMVC framework was introduced in the recent project .
Ii. examples of Getting Started
①. Build the project and import the corresponding spring jar package and parse the JSON package Fastjson.
②. Configuring spring's core class in the Web. xml File Dispatcherservlet
③. Configuring spring's core configuration file Spring-servlet.xml
④. Writing entity Classes person
public class Person {
private String name;
Private Integer age;
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public Integer Getage () {
return age;
}
public void Setage (Integer age) {
This.age = age;
}
Public String toString () {
Return "[Name:" + name + ", Age:" + Age + "]";
}
}
⑤. Writing the Controller class Personcontroll
@Controller
public class Personcontroll {
@RequestMapping ("Toajax.do")
Public String Toajax () {
Return "Ajax";
}
@RequestMapping (value = "Ajax.do", method = Requestmethod.get)
public void Ajax (@ModelAttribute person Person,printwriter printwriter) {
SYSTEM.OUT.PRINTLN (person);
String jsonstring = json.tojsonstring (person, Serializerfeature.prettyformat);
Printwriter.write (jsonstring);
Printwriter.flush ();
Printwriter.close ();
}
}
⑥. Writing an access page ajax.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" Utf-8 "%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>ajax for Springmvc</title>
<script type= "Text/javascript" src= "Js/jquery-1.6.2.min.js" ></script>
<script type= "Text/javascript" >
$ (function () {
$ ("#click"). Click (function () {
$.ajax ({
Type: "GET",
URL: "Ajax.do",
Data: "Name=zhangsan&age=20",
DataType: "Text",
Success:function (msg) {
Alert (msg);
}
});
});
});
</script>
<body>
<input id= "click" type= "button" value= "Click to show Person"/>
</body>
⑦. Accessing url:http://localhost:8080/springmvc/toajax.do
From: http://blog.csdn.net/zdp072/article/details/18187033
This article uses SPRINGMVC and Ajax to implement the return of a JSON object to a page