SpringMvc接受特殊符号参数被转义

WEB开发时,在前端通过get / post 方法传递参数的时候  如果实参附带特殊符号,后端接收到的值中特殊符号就会被转义

例如该请求: http://localhost:10001/demo/index.do?name=张三(1)

注:中文()不会出现此种情况

后台就收到的实际 name 值为:   张三(1)

(其实为html中  (  符号的十进制编码

)其实为html中  )  符号的十进制编码

方法1:

  检查web.xml里是否配置了过滤特殊字符的filter,若不需要可以关掉此filter

方法2:

  java中可以使用 org.apache.commons.lang3  包中的  StringEscapeUtils.unescapeHtml4(String str) 方法来进行解码。

方法3:

  在controerl接收的参数前加上@RequestBody注解,具体可百度@RequestBody的用法,此方法局限性比较大

例如 :

@RequestMapping(value = "/index.do")
@ResponseBody
public String addQbzl(HttpSession session, @RequestBody String name) {
}

前端则需要 声明dataType 和 contentType,传递的参数并用JSON.stringify()转为json字符串

1 $.ajax({
2      url: CONTEXTPATH + "/index.do",
3      type: 'POST',
4      dataType: 'JSON',
5      contentType : 'application/json',
6      data: JSON.stringify(Data),
7      success: function (data) {
8   }
9 })
原文地址:https://www.cnblogs.com/guanghe/p/9431659.html