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

时间:2024-11-20 16:38:14

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)