Maroš Košina
Maroš Košina

Reputation: 146

JSF <h:inputText/> character encoding issue

I got through many "googles" on this topic, but no help came. Thing is, that I set UTF-8 encoding on every single place where I could have (and even could not) but, still data retrieving from inputText is corrupted. I use JSF, Primefaces and Hibernate. When I post to page it is rendered correctly from DB, written as plain text in html doc, even as bean answer.

<h:outputText value="text cez OT: čerstvejší"/><br/>
text cez IDE: čerstvejší<br/><br/>

<h:outputText value="text cez OT_beans: #{temp.dia}"/><br/>
text cez IDE_beans: #{temp.dia}<br/><br/>

<h:outputText value="text cez OT_DBS: #{temp.getDBS()}"/><br/>
text cez IDE_DBS: #{temp.getDBS()}<br/>

Also, when I set the setter of variable to this:

public String getName() {
    return "načítané z aplikácie";
}

it renders correctly. Only when I post from page any of "ľščťžýáíé" characters it corrupts to Ä? and common chars. It runs on Appache Tomcat 7.0.34 managed by NetBeans with CDI, JSF 2.2, Primefaces 3.5, Hibernate 3.2.5. UTF-8 encoding set places:

  1. meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> in every header

  2. Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/> and Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8"/> on Appache server

  3. f:view encoding="UTF-8" contentType="text/html">

  4. I also had webFilter, that did not helped:

    @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding(encoding); response.setCharacterEncoding(encoding); chain.doFilter(request, response); }

Any help ? Additional questions ? THANK YOU !`enter code here

Upvotes: 4

Views: 5182

Answers (2)

Maroš Košina
Maroš Košina

Reputation: 146

Thing is, that usage CDI on Tomcat 7 requires org.jboss.weld.environment.servlet.Listener which uses different encoding (I think). Solution is to configure org.apache.catalina.filters.SetCharacterEncodingFilter and org.jboss.weld.servlet.ConversationFilter before weld listener.

<filter>
    <filter-name>SetCharacterEncoding</filter-name>  
    <filter-class>org.apache.catalina.filters.SetCharacterEncodingFilter</filter-class>  
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>ignore</param-name>  
        <param-value>false</param-value>  
    </init-param>
</filter>
<filter>
    <filter-name>Conversation</filter-name>  
    <filter-class>org.jboss.weld.servlet.ConversationFilter</filter-class>        
</filter>
<filter-mapping>
    <filter-name>SetCharacterEncoding</filter-name>  
    <url-pattern>/*</url-pattern>  
</filter-mapping>
<filter-mapping>
    <filter-name>Conversation</filter-name>  
    <url-pattern>/*</url-pattern>  
</filter-mapping>

Upvotes: 2

MoYapro
MoYapro

Reputation: 119

Perhaps the encoding in your DB is not UTF-8. You can check that when executing show create table on your DB.

Upvotes: 0

Related Questions