gpt4 book ai didi

java - 在spring MVC中传递非英文字符参数

转载 作者:行者123 更新时间:2023-11-30 11:05:31 24 4
gpt4 key购买 nike

我在 springMVC 中传递参数时遇到问题。

@RequestMapping(value = "/", method = RequestMethod.POST)
public String homePost(Model model, HttpServletRequest request) {
StringBuilder redirect = new StringBuilder();
String para = request.getParameter("keyword");

redirect.append("redirect:/search?" + "cat=0&job="+ para);
logger.info("Page called: SEARCH; Called parameter: KEYWORD: " + redirect.toString());

return redirect.toString();
}

在我的控制台中,我可以看到:

Page called: SEARCH; Called parameter: KEYWORD: redirect:/search?cat=0&job=lol лол lol

但是我的网址是

http://localhost:8088/search?cat=0&job=lol%20%20%20%20%20lol

我将所有编码设置为“UTF-8”,并且我尝试使用西里尔字母从列表中进行搜索,但它一直将西里尔字母转换为空格。我可以使用拉丁字母进行搜索。如果我使用西里尔字母手动写入 URL,我也可以搜索。在控制台中,它还显示

bind => [%lol lol%]

谢谢。

P.S 即使我没有使用StringBuilder,我也有同样的问题。P.S.S 我的 web.xml 有

<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

最佳答案

感谢您的回答。我解决了这个问题,如果有人感兴趣的话。@Alex 的链接非常有用。

@RequestMapping(value = "/", method = RequestMethod.POST)
public String homePost(Model model, HttpServletRequest request) throws UnsupportedEncodingException {
StringBuilder redirect = new StringBuilder();
String para = request.getParameter("keyword");

redirect.append("redirect:/search/" + URLEncoder.encode(para, "UTF-8"));
logger.info("Page called: SEARCH; Called parameter: KEYWORD: " + redirect.toString());

return redirect.toString();
}

获取方法是:

@RequestMapping(value="/search/{keyword}", method = RequestMethod.GET)
public String job(Model model,@PathVariable String keyword) throws UnsupportedEncodingException {

String job = URLDecoder.decode(keyword);
return null;
}

关于java - 在spring MVC中传递非英文字符参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29623761/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com