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 ...
随机推荐
- python基础(36):pymysql模块
1. pymysql模块 在使用pymysql模块前需要学习数据库MySQL:<MySQL基础>. 1.1 pymysql的下载和使用 看完MySQL基础,我们都是通过MySQL自带的命令 ...
- (草稿)如何判断一名UiPath开发人员是否合格?
一名合格的UiPath开发人员究竟需要具备什么核心技能?业务梳理?沟通技巧?VB.net吗?VBA吗?Python?还是SQL?出于多种原因,关于这一点总是众说纷纭,莫衷一是.尽管这些技术都算沾边,但 ...
- 后端必备 Nginx 配置
后端必备 Nginx 配置 概要 防盗链 根据文件类型设置过期时间 静态资源访问 日志配置 日志字段说明 access_log 访问日志 error_log 日志 日志切割 反向代理 禁止指定user ...
- spring+cxf No bean named 'cxf' available
最近项目中需要用到webservice,在spring中集成cxf时一直报错: 严重: StandardWrapper.Throwable org.springframework.beans.fact ...
- 12C-使用跨平台增量备份减少可移动表空间的停机时间 (Doc ID 2005729.1)
12C - Reduce Transportable Tablespace Downtime using Cross Platform Incremental Backup (Doc ID 20057 ...
- JVM基础回顾记录(一):JVM的内存模型
JVM的内存模型&垃圾收集算法 JVM内存模型 JAVA程序执行的基本流程(基于HotSpot): 图1 1.程序计数器 程序计数器是一块较小的内存空间,是当前线程执行字节码的行号指示器,字节 ...
- 第04组 Beta冲刺(4/4)
队名:斗地组 组长博客:地址 作业博客:Beta冲刺(4/4) 各组员情况 林涛(组长) 过去两天完成了哪些任务: 1.分配展示任务 2.收集各个组员的进度 3.写博客 展示GitHub当日代码/文档 ...
- java之封装
java中通过将成员变量声明为private,再提供公共的public方法:setXxx()和getXxx()实现对该属性的操作,以实现以下目的: 隐藏一个类中不需要对外提供的实现: 使用者只能通过事 ...
- linux-在指定路径下查询文件夹是否存在
我们常常在Linux下去查找文件 find / -name 'test.py' # 在根目录下查找名为test.py的文件 但是如果用查找文件的方式去查找文件夹的话,是查不到的 find / -max ...
- Python爬虫的概括以及实战
第一章主要讲解爬虫相关的知识如:http.网页.爬虫法律等,让大家对爬虫有了一个比较完善的了解和一些题外的知识点. 今天这篇文章将是我们第二章的第一篇,我们从今天开始就正式进入实战阶段,后面将会有更 ...