Spring MVC get and Post pass value garbled problem

Source: Internet
Author: User
Tags goto

1.url Spelling Flyer Value object The list map is passed into the background in JSON format

<%@ 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>insert title here</title>
<link href= ". /css/bootstrap.css "rel= ' stylesheet ' type= ' text/css '/>
<!--jQuery (necessary for Bootstrap ' s JavaScript plugins)--
<script src= ". /js/jquery.min.js "></script>
<!--Custom Theme Files----
<link href= ". /css/style.css "rel=" stylesheet "type=" Text/css "media=" All "/>
<body>
<input type= "button" onclick= "Clink1 ()" value= "via url" value ">
<input type= "button" onclick= "Clink2 ()" value= "single-value Reference" >
<input type= "button" onclick= "Clink3 ()" value= "Object Pass" >
<input type= "button" onclick= "Clink4 ()" value= "List pass value" >
<input type= "button" onclick= "Clink5 ()" value= "Map Pass Value" >
<script type= "Text/javascript" >

function Clink1 () {
var url= "/ajax/testurl?dataparam=test";
$.ajax ({
Type: "Post",
Url:url,
DataType: ' JSON ',
Success:function (data) {
Alert (json.stringify (data));
}
});
}
function Clink2 () {
var url= "/ajax/testdata";
$.ajax ({
Type: "Post",
Data:{dataparam: "Test", str: "Testing"},
Url:url,
DataType: ' JSON ',
Success:function (data) {
Alert (json.stringify (data));
}
});
}
function Clink3 () {
var url= "/ajax/testobject";
var org={id:1,age:1,name: "Zhu", Happy: "Sleep"};
$.ajax ({
Type: "Post",
Data:org,
Url:url,
Data:org,
DataType: ' JSON ',
Success:function (data) {
Alert (json.stringify (data));
}
});
}
function Clink4 () {
var url= "/ajax/testlist";
var userlist = Json.stringify ([
{id:1,age:1,name: "Zhu", Hobby: "Sleeping"},
{id:1,age:22,name: "Zhu2", Hobby: "Sleep 2"}
]);
$.ajax ({
Type: "Post",
Data:userlist,
Url:url,
ContentType: ' Application/json; Charset=utf-8 ',
DataType: ' JSON ',
Success:function (data) {
Alert (json.stringify (data));
}
});
}
function Clink5 () {
var url= "/ajax/testmap";
var userlist = json.stringify ({"Name": "Wanglin", "Age": "18"});
$.ajax ({
Type: "Post",
Data:userlist,
Url:url,
ContentType: ' Application/json; Charset=utf-8 ',
DataType: ' JSON ',
Success:function (data) {
Alert (json.stringify (data));
}
});
}
</script>
</body>
2. Background code

Package Cn.springmvc.controller;

Import java.io.UnsupportedEncodingException;
Import java.util.ArrayList;
Import java.util.List;
Import Java.util.Map;

Import Javax.servlet.http.HttpServletRequest;

Import Org.springframework.http.HttpRequest;
Import Org.springframework.stereotype.Controller;
Import Org.springframework.web.bind.annotation.RequestBody;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RequestMethod;
Import Org.springframework.web.bind.annotation.ResponseBody;

Import Cn.springmvc.model.User;
Import Cn.springmvc.util.ActionResult;
Import Cn.springmvc.util.DataResult;

Import Com.alibaba.fastjson.JSON;
Import Com.alibaba.fastjson.JSONArray;
Import Com.alibaba.fastjson.JSONObject;

@Controller
@RequestMapping ("Ajax")
public class Ajaxdom {
/**
*
* @Title: Test1
* @Description: URL transfer value
* @param @param Dataparam
* @param @return
* @return String
* @throws
*/
@RequestMapping (value= "/testurl", Method=requestmethod.post)
Public @ResponseBody string Test1 (string dataparam) {
ActionResult actionresult=new ActionResult (True,dataparam);
Object Goto JSON
String json = json.tojsonstring (ActionResult);
return JSON;
}
/**
* Get mode
* @Title: Testget
* @Description: TODO
* @param @param Dataparam
* @param @return
* @return String
* @throws
*/
@RequestMapping (value= "/testurl")
Public @ResponseBody string Testget (string dataparam) {
ActionResult actionresult=new ActionResult (True,dataparam);
Object Goto JSON
String json = json.tojsonstring (ActionResult);
return JSON;
}
/**
* Flyer Value
* @Title: Test2
* @Description: TODO
* @param @param Dataparam
* @param @return
* @return String
* @throws
*/
@RequestMapping (value= "/testdata", Method=requestmethod.post)
Public @ResponseBody string Test2 (String dataparam,string str) {
ActionResult actionresult=new ActionResult (TRUE,STR);
Object Goto JSON
String json = json.tojsonstring (ActionResult);
return JSON;
}
/**
* Object Pass Value
* @Title: Test3
* @Description: TODO
* @param @param Dataparam
* @param @return
* @return String
* @throws
*/
@RequestMapping (value= "/testobject", Method=requestmethod.post)
Public @ResponseBody String test3 (user user) {
List<user> userlist=new arraylist<user> ();
Userlist.add (user);
Dataresult dataresult=new Dataresult (userlist);
Object Goto JSON
String json = json.tojsonstring (Dataresult);
return JSON;
}
/**
* List Pass Value
* @Title: Test4
* @Description: TODO
* @param @param Dataparam
* @param @return
* @return String
* @throws
*/
@RequestMapping (value= "/testlist", Method=requestmethod.post)
Public @ResponseBody string Test4 (@RequestBody string userlist) {

list<user> list = Json.parsearray (userlist, User.class);
Dataresult dataresult=new Dataresult (list);
Object Goto JSON
String json = json.tojsonstring (Dataresult);
return JSON;
}
/**
* Map Transfer value
* @Title: TEST5
* @Description: TODO
* @param @param Dataparam
* @param @return
* @return String
* @throws
*/
@RequestMapping (value= "/testmap", Method=requestmethod.post)
Public @ResponseBody string Test5 (@RequestBody string usermap) {
@SuppressWarnings ("Unchecked")
Map<string,string> map= (map<string,string>) json.parse (UserMap);
Dataresult dataresult=new dataresult (map);
Object Goto JSON
String json = json.tojsonstring (Dataresult);
return JSON;
}
}

3. The incoming backend needs to be encoded in the Web. Xml format

<filter>
<filter-name>SpringEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SpringEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

4. The return value garbled problem needs to be configured in spring MVC XML

<!--fix JSON @Respone return value garbled problem--
<bean class= "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

<bean class= "Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" >

<property name= "Messageconverters" >

<list>

<bean class = "Org.springframework.http.converter.StringHttpMessageConverter" >

<constructor-arg>

<bean class= "Org.springframework.beans.factory.config.MethodInvokingFactoryBean" >

<property name= "Targetclass" value= "Java.nio.charset.Charset"/>

<property name= "Targetmethod" value= "forname"/>

<property name= "Arguments" value= "UTF-8"/>

</bean>

</constructor-arg>

</bean>

</list>

</property>

5. Finally, Spring MVC redirect parameter garbled problem

Resolution in the Tomcat directory Conf-->server.xml

Plus uriencoding= "UTF-8" on the line.

<connector connectiontimeout= "20000" port= "Up" protocol= "http/1.1" redirectport= "8443" URIEncoding= "UTF-8"/>

Spring MVC get and Post pass value garbled problem

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.