JSON is used for both front and back data interactions.
Framework SSM (Spring+springmvc+mybatis)
@RequestBody annotations implement JSON data to receive HTTP requests, convert JSON data to Java objects, annotations before loading method parameters
The @ResponseBody annotation is used to convert the object returned by the controller's method through the Httpmessageconverter interface to the specified format data such as: Json,xml, etc., by response response to the client
1. Guide Pack (Jackson's bag, search online)
2. Use < Mvc:annotation-driven ></Mvc:annotation-driven when configuring the Mapper and adapter, and anumber of formats are loaded by default. such as JSON, no manual configuration required
[Java]View PlainCopy
- <mvc:annotation-driven></mvc:annotation-driven>
3.controller Writing
[Java]View PlainCopy
- @RequestMapping ("/edititemsubmit_requestjson")
- public @ResponseBody Items Edititemsubmit_requestjson (@RequestBody items items) throws Exception {
- return items;
- }
Compilation of JS in 4.jsp
[JavaScript]View PlainCopy
- function Request_json () {
- $.ajax ({
- Type:"POST",
- URL:"${pagecontext.request.contextpath}/item/edititemsubmit_requestjson.action",
- ContentType:"Application/json;charset=utf-8",
- Data:' {' name ': ' Test product ', ' price ': 99.9} ',
- Success:function (data) {
- alert (data);
- }
- });
- }
The @RequestBody converts the incoming JSON data to a bit items object, and @responsebody returns the items object to the JSP page in JSON format.
Note: Ensure that the JSON key is consistent with the fields of the Items PO class
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
JSON format is used for both front and back data interaction in the SSM framework