SpringMVC重定向路径中带中文参数

springboot重定向到后端接口测试

package com.mozq.http.http_01.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; // http://localhost:7001/acc /**
* springboot测试是否可以直接重定向进入后端接口。
*/
@Controller
@RequestMapping("/acc")
public class Demo_02 {
/**
* 不对中文参数进行编码,重定向后无法正确获取参数。
* 不仅中文字符,还有一些特殊字符都应该进行编码后拼接到url中,具体的要看规范。
* 英文字母和数字是肯定可以的。
*
* 运行结果:
* http://localhost:7001/acc/accept?name=??&address=??
* ??:??
*/
@RequestMapping("/cn")
public String cn(){
return "redirect:http://localhost:7001/acc/accept?name=刘备&address=成都";
} /**
* 对参数进行编码,重定向到后端接口,不需要我们进行解码,就可以拿到正确参数。可能因为springboot内嵌的tomcat的uri编码默认采用utf-8,和我们编码的方式相同,所以参数被正确获取。
* 运行结果:
* http://localhost:7001/acc/accept?name=%E5%88%98%E5%A4%87&address=%E6%88%90%E9%83%BD
* 刘备:成都
*/
@RequestMapping("/enc")
public String enc() throws UnsupportedEncodingException {
String Url = "http://localhost:7001/acc/accept"
+ "?name=" + URLEncoder.encode("刘备", "UTF-8")
+ "&address=" + URLEncoder.encode("成都", "UTF-8");
return "redirect:" + Url;
} /**
* 运行结果:
* http://localhost:7001/acc/accept?name%3D%E5%88%98%E5%A4%87%26address%3D%E6%88%90%E9%83%BD
* Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'name' is not present]
*/
@RequestMapping("/encWrong")
public String encWrong(){
String params = "name=刘备&address=成都";
String encParams = "";
try {
encParams = URLEncoder.encode(params, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return "redirect:http://localhost:7001/acc/accept?" + encParams;
} @RequestMapping("/accept")
@ResponseBody
public String accept(@RequestParam("name") String name, @RequestParam("address") String address){
return name + ":" + address;
} }

springboot重定向到前端接口测试

package com.mozq.http.http_01.demo;

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map; // http://localhost:7001/cn
@Controller
public class Demo_01 {
/**
* 不对中文参数进行编码,重定向后无法正确获取参数。
* 不仅中文字符,还有一些特殊字符都应该进行编码后拼接到url中,具体的要看规范。
* 英文字母和数字是肯定可以的。
*/
@RequestMapping("/cn")
public String cn(){
return "redirect:http://localhost:7001/demo.html?name=刘备&address=成都";
} /**
* 对参数进行编码,重定向后前端可以拿到参数,需要手动解码。
*/
@RequestMapping("/enc")
public String enc() throws UnsupportedEncodingException {
String Url = "http://localhost:7001/demo.html?"
+ "name=" + URLEncoder.encode("刘备", "UTF-8")
+ "&address=" + URLEncoder.encode("成都", "UTF-8");
return "redirect:" + Url;
} @RequestMapping("/encWrong")
public String encWrong(){
String params = "name=刘备&address=成都";
String encParams = "";
try {
encParams = URLEncoder.encode(params, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return "redirect:http://localhost:7001/demo.html?" + encParams;
} }
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>demo</h1>
<script>
console.log(window.location.href);
console.log(getQueryVariable("name"));
console.log(getQueryVariable("address"));
//http://localhost:7001/demo.html?name=??&address=??
//http://localhost:7001/demo.html?name%3D%E5%88%98%E5%A4%87%26address%3D%E6%88%90%E9%83%BD /*
http://localhost:7001/demo.html?name=%E5%88%98%E5%A4%87&address=%E6%88%90%E9%83%BD
11 %E5%88%98%E5%A4%87
12 %E6%88%90%E9%83%BD
decodeURIComponent("%E5%88%98%E5%A4%87");
"刘备"
decodeURI("%E5%88%98%E5%A4%87");
"刘备" 前端需要自己用js解码:
global对象的decodeURI()和decodeURIComponent()使用的编码是UTF-8和百分号编码。
(我们编码时使用的是UTF-8,所以可以正常解码)
decodeURI()和decodeURIComponent()的区别是,decodeURI()不会处理uri中的特殊字符,此处我们应该选择decodeURIComponent()解码。
*/
function getQueryVariable(variable){
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if(pair[0] == variable){return pair[1];}
}
return(false);
}
</script>
</body>
</html>

bug

/*
bug: 后端代码在url路径中直接拼接中文,然后进行重定向,重定向后无法获取中文参数。
*/
String deliveryAddress = "天字一号30";
String orderMealUrl = "http://"+PropertiesHelper.getRelayDomanName()+"/restaurant/?openId=MO_openId&storeId=MO_storeId&orderEquipment=MO_orderEquipment&deliveryAddress=MO_deliveryAddress"
.replace("MO_openId", openId)
.replace("MO_storeId", String.valueOf(storeId))
.replace("MO_orderEquipment", String.valueOf(orderEquipment))
.replace("MO_deliveryAddress", deliveryAddress)
;
/*
重定向到点餐页面:orderMealUrl=http://aqv9m6.natappfree.cc/restaurant/?openId=oor4oxEII8_ddih2_RxdtYbxf9Cg&storeId=1&orderEquipment=6&deliveryAddress=天字一号30
*/ /* 方案:对路径中的中文进行URL编码处理 */
String deliveryAddress = "天字一号30";
String encDeliveryAddress = "";
try {
encDeliveryAddress = URLEncoder.encode(deliveryAddress, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String orderMealUrl = "http://"+PropertiesHelper.getRelayDomanName()+"/restaurant/?openId=MO_openId&storeId=MO_storeId&orderEquipment=MO_orderEquipment&deliveryAddress=MO_deliveryAddress"
.replace("MO_openId", openId)
.replace("MO_storeId", String.valueOf(storeId))
.replace("MO_orderEquipment", String.valueOf(orderEquipment))
.replace("MO_deliveryAddress", encDeliveryAddress)

SpringMVC重定向路径中带中文参数的更多相关文章

  1. GBK 编码时 url 中带中文参数的问题

    项目中遇到的 GBK 编码问题,记录如下. 将代码精简为: <!DOCTYPE HTML> <html> <meta charset="gb2312" ...

  2. JS在路径中传中文参数

    需要用 encodeURI('中文');处理一下.

  3. struts2中form提交到action中的中文参数乱码问题解决办法(包括取中文路径)

    我的前台页是这样的: <body>      <form action="test.action" method="post">     ...

  4. get请求url中带有中文参数出现乱码情况

    在项目中经常会遇到中文传参数,在后台接收到乱码问题.那么在遇到这种情况下我们应该怎么进行处理让我们传到后台接收到的参数不是乱码是我们想要接收的到的,下面就是我的一些认识和理解. get请求url中带有 ...

  5. js的url中传递中文参数乱码,如何获取url中参数问题

    一:Js的Url中传递中文参数乱码问题,重点:encodeURI编码,decodeURI解码: 1.传参页面Javascript代码: <script type=”text/javascript ...

  6. Js的Url中传递中文参数乱码的解决

    一:Js的Url中传递中文参数乱码问题,重点:encodeURI编码,decodeURI解码: 1.传参页面Javascript代码: 2. 接收参数页面:test02.html 二:如何获取Url& ...

  7. java中的中文参数存到数据库乱码问题

    关于java中的中文参数乱码问题,遇见过很多,若开发工具的字符集环境和数据库的字符集环境都一样,存到数据库中还是乱码的话,可以通过以下方法解决: 用数据库客户端检查每个字段的字符集和字符集校对和这个表 ...

  8. 解决Java getResource 路径中含有中文的情况

    问题描述 当Java调用getResource方法,但是因为路径中含有中文时,得不到正确的路径 问题分析 编码转换问题 当我们使用ClassLoader的getResource方法获取路径时,获取到的 ...

  9. IE浏览器url中带中文报错的问题;以及各种兼容以及浏览器问题总结

    1.解决IE浏览器url带中文报错 /* encodeURI()解决IE浏览器请求url中带中文报错的问题 */ URL = encodeURI("<%=basePath%>ve ...

随机推荐

  1. [转]UiPath Installing the Firefox Extension

    本文转自:https://docs.uipath.com/studio/lang-en/v2019/docs/installing-the-firefox-extension From UiPath ...

  2. C语言编程的一些小总结

    1. static:可用于定义静态局部变量 在局部变量前,加上关键字static,该变量就被定义成为一个静态局部变量. 举一个静态局部变量的例子: void fn() { static int n=1 ...

  3. orm终极大爆炸

    orm终极 甩一个代码给你 # 创建字段 class Field: def __init__(self, name, column_type, primary_key, default): self. ...

  4. "(error during evaluation)" computed

    在vue-cli搭建的去哪网app项目中使用了  computed  计算属性 computed计算属性在chrome插件中的 vue devtools 插件中报错 应该显示出来 computed 属 ...

  5. Python元组是什么

    引出 在使用Python过程中,列表.集合和字典是比较常用的数据结构. 列表简单说就是数组,不对,它就是数组 集合就是去重的元素结构,和JAVA中的set一样 字典就是一个key-value的键值对, ...

  6. Node.js上传文件出现Unexpected field

    上传文件时,input框的name值要与node接口中single(' ')中的参数一致,否则会报"意外字段的错" 前端用的layui 后端node接口

  7. 20.never give up

  8. [考试反思]1111csp-s模拟测试110:三思

    题目名是为了照应3天的倒计时(我才不会说是因为我考场又摸鱼了) 在OJ上得到了295的好成绩,但是本地评测没有O2掉了10分. 总体来说还可以.T1全场切,T2半场切,T3纯暴力不卡常都有95... ...

  9. java中窗口的打开与关闭

    作为小白的我,今天学习了java中打开与关闭窗口的方法. 1.在java中创建一个窗口 import java.awt.*;import java.awt.event.*;public class L ...

  10. 利用Python进行数据分析-Pandas(第三部分)

    访问数据是使用本书所介绍的这些工具的第一步.这里会着重介绍pandas的数据输入与输出,虽然别的库中也有不少以此为目的的工具. 输入输出通常可以划分为几个大类:读取文本文件和其他更高效的磁盘存储格式, ...