UTF-8 in PrimeFaces 3.x

2019-04-07 19:35发布

I have a very simple application,there is an inputtext in my index page and a button to go to page2.xhtml.

    <h:body>
        <h:form>
            <h:inputText value="#{mainBean.testValue}"/>
            <p:commandButton update="myoutput" value="ajax call" ajax="false"/>
            <p:separator />
            <h:commandButton action="#{mainBean.gotoPageTwo}" value="goto Page2"/>
            <br/>
            <h:outputText value="#{mainBean.testValue}" id="myoutput"/>
        </h:form>
    </h:body>
</html>

I tested this application with PrimeFaces 2.2.1 and there was no problem. but after submit each of the above button, my UTF-8 characters will destroy. I tested filter but it don't work. Is it a bug in PrimeFaces 3.x? Can any body solve this problem?

1条回答
老娘就宠你
2楼-- · 2019-04-07 19:45

The web.xml example of the answer you found at PrimeFaces forum is incomplete. The <filter-mapping> is missing. Without that, the filter won't even run at all. Add it accordingly

<filter-mapping>
    <filter-name>Character Encoding Filter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

When you're already on Servlet 3.0 (Tomcat 7, Glassfish 3, etc), an alternative is to use just the @WebFilter annotation on the class. Don't forget to remove the filter entry from web.xml.

@WebFilter("/*")

For a background explanation of the cause of this character encoding problem during PrimeFaces 2.x-3.x upgrade, see also Unicode input retrieved via PrimeFaces input components become corrupted

查看更多
登录 后发表回答