0

In my Liferay portlet app where I use Spring MVC framework, is something wrong with the encoding and I can't figure out what. According to the firebug logs, the value leaves frontend encoded well. But after it's passed to backend's action handler, the encoding is ambiguous. For example: In the frontend is the value: "abcčdďeěf". But to backend arrives "abc─Źd─Će─Ťf".

My form:

<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="portlet" uri="http://java.sun.com/portlet_2_0" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<portlet:defineObjects />

<portlet:actionURL var="formUrl" name="sendForm"/>

<% request.setCharacterEncoding("UTF-8"); %>

<form name="editForm" 
      method="POST"
      action="${formUrl}"
      >
<input type="text" name="query" id="query"/> 
    <input value="Submit" type="submit" />
</form>
<br>

Action handler:

@ActionMapping(value = "sendForm")
    public void sendForm(ActionRequest request, ActionResponse response, Model model) throws UnsupportedEncodingException {            
        String query = request.getParameter("query");
        /*
         * Here is the value of query already ambiguous
         */
        if (query != null && (query.matches(".*\\w.*"))) {
            ArrayList<SearchResultEntity> searchResutls = solrSearchService.getSearchResults("http://localhost:8983/solr/GE/select?q=content%3A" + query + "+OR+title%3A" + query + "+OR+id%3A" + query + "&wt=json&indent=true");
            System.out.println("http://localhost:8983/solr/GE/select?q=content%3A" + query + "+OR+title%3A" + query + "+OR+id%3A" + query + "&wt=json&indent=true");
            request.setAttribute("searchResutls", searchResutls);
        }
    }

Any idea, how to fix this encoding issue?

Martin Dvoracek
  • 1,714
  • 6
  • 27
  • 55
  • 1
    `query.matches(".*\\w.*")` <-- you would be better off using a `Pattern`, especially if this is called often. Also, beware that `\w` in Java only works for ASCII by default – fge Dec 17 '14 at 10:54
  • Thanks for an advice. Maybe even the best approa to check if there are not only whitespaces would be myString.trim().isEmpty() – Martin Dvoracek Dec 17 '14 at 11:05

1 Answers1

0

try

request.setCharacterEncoding("UTF-8"); 

before

 request.setAttribute("searchResutls", searchResutls);
Sagar Koshti
  • 408
  • 2
  • 6
  • 15
  • that won't help...The encoding of the var `query` is ambiguous. I need to have it well encoded in the line `solrSearchService.getSearchResults("http://localhost:8983/solr/GE/ .....` to send a request to another server – Martin Dvoracek Dec 17 '14 at 10:44