3

I have a problem passing my parameters in 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();
}

In my console, I can see:

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

But my URL is

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

I set my all encoding in "UTF-8" and I am trying to search from list using Cyrillic but it keeps converting Cyrillic letters into empty spaces. I can search using Latin letters. If I write in the URL manually using Cyrillic letters, I can also search. In the console, it also shows

bind => [%lol lol%]

Thank you.

P.S Even if I did not use StringBuilder, I have the same problem. P.S.S My web.xml has

<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>
Tim Bold
  • 51
  • 5
  • Does this question help? http://stackoverflow.com/questions/19272494/fastest-way-to-encode-cyrillic-letters-for-url – Lydia Ralph Apr 14 '15 at 14:22
  • Are you using Tomcat? See "How do I change how GET parameters are interpreted?" in http://wiki.apache.org/tomcat/FAQ/CharacterEncoding – Neil McGuigan Apr 14 '15 at 17:14

1 Answers1

2

Thank you for your answers. I solved the problem and if anyone is interested. @Alex 's link was so useful.

@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();
    }

And get method is:

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

        String job = URLDecoder.decode(keyword);
        return null;
}
Tim Bold
  • 51
  • 5