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. How to: Use the Entity Framework Code First in XAF 如何:在 XAF 中使用EF CodeFirst

    This topic demonstrates how to create a simple XAF application with a business model in a DbContext ...

  2. Ligg.EasyWinApp-100-Ligg.EasyWinForm:一款Winform应用编程框架和UI库介绍

        本项目是一个Winform应用编程框架和UI库.通过这个该框架,不需任何代码,通过XML配置文件,搭建任意复杂的Windows应用界面,以类似Execel公式的方式实现基本的过程控制(赋值.条 ...

  3. java后台树形结构展示---懒加载

    一.数据库设计 二.实体类:entity import com.joyoung.cloud.security.common.validatedGroup.Add;import com.joyoung. ...

  4. QT在linux下获取网络类型

    开发中遇到这样一个需求,需要判断当前网络的类型(wifi或者4G或者网线),在这里给大家一块分享下: 1.这里有一个linux指令:nmcli(大家自行百度即可) 2.nmcli device sta ...

  5. 产品经理如何使用 CODING 进行项目规划

    CODING 为您的企业提供从概念到软件开发再到产品发布的全流程全周期软件研发管理,为您的研发团队提供全程助力,帮助研发团队捋清需求.不断迭代.快速反馈并能实时追踪项目进度直到完成.同时 CODING ...

  6. Hibernate 数据层基类实现

    提取经常操作表如新增.修改.删除.查询.分页查询.统计等业务功能,形成基类,用泛型传参,有利于每个实体对象数据层继承. package com.base.dao; import java.io.Ser ...

  7. Navicat 软件的使用以及pymysql

    Navicat 软件的使用以及pymysql 一.navicate的安装及使用 下载 直接百度搜索navicate ,如下图 连接数据库 新建数据库以及新建表 选中然后鼠标右键 建模 利用navica ...

  8. 史上最全Oracle数据泵常用命令

    本文转自https://blog.csdn.net/Enmotech/article/details/102848825 墨墨导读:expdp和impdp是oracle数据库之间移动数据的工具,本文简 ...

  9. 18c & 19c Physical Standby Switchover Best Practices using SQL*Plus (Doc ID 2485237.1)

    18c & 19c Physical Standby Switchover Best Practices using SQL*Plus (Doc ID 2485237.1) APPLIES T ...

  10. java 报错 System.out.printIn 错误: 找不到符号

    java 运行 System.out.printIn() 报错 ,java System.out.println 这是错误示范 !!! System.out.printIn("仔细看看pri ...