我在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();
}
在我的控制台中,我可以看到:
页面呼叫:搜索;调用参数:KEYWORD: 重定向:/ search?cat = 0& job =lolлолlo
但我的网址是
http://localhost:8088/search?cat=0&job=lol%20%20%20%20%20lol
我将所有编码设置为“UTF-8”,并且我尝试使用Cyrillic从列表中搜索,但它会将西里尔字母转换为空格。我可以用拉丁字母搜索。如果我使用西里尔字母手动写入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>
答案 0 :(得分:1)
感谢您的回答。我解决了这个问题,如果有人有兴趣的话。 @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;
}