SpringMVC重定向路径中带中文参数
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重定向路径中带中文参数的更多相关文章
- GBK 编码时 url 中带中文参数的问题
项目中遇到的 GBK 编码问题,记录如下. 将代码精简为: <!DOCTYPE HTML> <html> <meta charset="gb2312" ...
- JS在路径中传中文参数
需要用 encodeURI('中文');处理一下.
- struts2中form提交到action中的中文参数乱码问题解决办法(包括取中文路径)
我的前台页是这样的: <body> <form action="test.action" method="post"> ...
- get请求url中带有中文参数出现乱码情况
在项目中经常会遇到中文传参数,在后台接收到乱码问题.那么在遇到这种情况下我们应该怎么进行处理让我们传到后台接收到的参数不是乱码是我们想要接收的到的,下面就是我的一些认识和理解. get请求url中带有 ...
- js的url中传递中文参数乱码,如何获取url中参数问题
一:Js的Url中传递中文参数乱码问题,重点:encodeURI编码,decodeURI解码: 1.传参页面Javascript代码: <script type=”text/javascript ...
- Js的Url中传递中文参数乱码的解决
一:Js的Url中传递中文参数乱码问题,重点:encodeURI编码,decodeURI解码: 1.传参页面Javascript代码: 2. 接收参数页面:test02.html 二:如何获取Url& ...
- java中的中文参数存到数据库乱码问题
关于java中的中文参数乱码问题,遇见过很多,若开发工具的字符集环境和数据库的字符集环境都一样,存到数据库中还是乱码的话,可以通过以下方法解决: 用数据库客户端检查每个字段的字符集和字符集校对和这个表 ...
- 解决Java getResource 路径中含有中文的情况
问题描述 当Java调用getResource方法,但是因为路径中含有中文时,得不到正确的路径 问题分析 编码转换问题 当我们使用ClassLoader的getResource方法获取路径时,获取到的 ...
- IE浏览器url中带中文报错的问题;以及各种兼容以及浏览器问题总结
1.解决IE浏览器url带中文报错 /* encodeURI()解决IE浏览器请求url中带中文报错的问题 */ URL = encodeURI("<%=basePath%>ve ...
随机推荐
- C# -- 模拟扑克牌发牌
C# -- 模拟扑克牌发牌 1. User 类: 玩家 public class User { private List<PaperCard> listCard = new List&l ...
- Springboot vue.js html 跨域 前后分离 Activiti6 工作流 集成代码生成器 shiro 权限
官网:www.fhadmin.org 特别注意: Springboot 工作流 前后分离 + 跨域 版本 (权限控制到菜单和按钮) 后台框架:springboot2.1.2+ activiti6.0 ...
- ubuntu 安装在硬盘与配置
安装 下载Ubuntu ISO文件,使用rufus制作启动U盘,重启选择这个U盘启动. 用rufus做启动盘时,提示缺少文件,点下载,找到log,进入找到下载地址,手动下载,并放到软件所在路径下的文件 ...
- JDK新特性关于流操作部分
// array 工具类 可以用来快捷的将数组转化为list List<String> strings = Arrays.asList("zhongguo", &quo ...
- Oracle 11g与12c的审计详解
最近遇到一些脚本诱发的审计相关BUG,感觉有必要重新梳理一下11g与12c的审计模式,于是根据官网修正了一下以前的一篇笔记这里发出来. 一.审计功能的开启: SQL> show paramete ...
- nginx配置文件 http 强跳转 https
路径 /usr/local/nginx/conf/conf.d/test.jackcui.com.conf server { listen 80; server_name test.jackcui.c ...
- GUI程序分析实例
GUI程序开发概述 GUI程序开发原理 GetMessage(&msg)将消息队列中的消息取出来,在循环中进行处理. GUI程序开发的本质
- 【GUI】基于V7开发板的裸机和各种RTOS版本的emWin程序模板,支持硬件JPEG,已发布(2019-05-26)
说明: 1.MDK请使用5.26及其以上版本,IAR请使用8.30及其以上版本. 2.修正了ST提供的部分驱动设计不合理的地方. 3.原创实现硬件JPEG添加到emWin中,实现简单,全程使用SDRA ...
- IT兄弟连 HTML5教程 CSS3揭秘 CSS选择器2
4 结构性伪类选择器 在学习结构性伪类选择器之前,先了解两个概念:伪类选择器和伪元素选择器.伪类选择器是CSS中已经定义好的选择器,不能随便命名.常用的伪类选择器是使用在a元素上的几种,如a:lin ...
- MongoDB自学------(4)MongoDB主从搭建
MongoDB复制原理 mongodb的复制至少需要两个节点.其中一个是主节点,负责处理客户端请求,其余的都是从节点,负责复制主节点上的数据. mongodb各个节点常见的搭配方式为:一主一从.一主多 ...