Set request character encoding of JSF input submitted values to UTF-8

孤街浪徒 提交于 2019-11-26 16:47:54

问题


I have the same problem as Set request character encoding of JSF input submitted values to UTF-8 in GlassFish, the submitted values arrive as Mojibake. However, the answer is targeted at GlassFish and I'm using JBoss AS 7.

I've already specified the JDBC connection URL to use UTF-8:

jdbc:mysql://localhost:3306/mydb?useUnicode=yes&characterEncoding=UTF-8

And in top of my JSF page:

<?xml version='1.0' encoding='UTF-8' ?>

How can I solve the same problem in JBoss AS 7? Or better, in a more generic way so that it works in all servers?


回答1:


The question which you linked to has already excluded the DB encoding from being the cause because the problem already occurs during printing/redisplaying the submitted value before saving in DB. Thus, the problem is in HTTP request encoding.

Your JDBC connection URL with the charset specified,

jdbc:mysql://localhost:3306/mydb?useUnicode=yes&characterEncoding=UTF-8

only tells the MySQL JDBC driver to use UTF-8 to decode values in SQL queries before sending it to DB. This is not only completely beyond JSF's scope, but this is also not the cause of your problem, provided that you're absolutely positive that you've the same problem as in the linked question.

Your XML prolog with the charset specified,

<?xml version='1.0' encoding='UTF-8' ?>

only tells the XML parser to use UTF-8 to decode the XML source before building the XML tree around it. The XML parser actually being used is SAX as internally used by Facelets during JSF view build time. This part has completely nothing to do with HTTP request/response encoding and is thus very unlikely the cause of your problem.

None of them sets the HTTP request encoding, while you need to set the HTTP request encoding. The question which you linked to already shows how to do that for the Glassfish server. In your case, you're however using JBoss AS server. The Glassfish-specific setting is then inapplicable and JBoss doesn't support anything like that. You'd need to bring in a custom servlet filter to do the job. E.g.

@WebFilter("/*")
public class CharacterEncodingFilter implements Filter {

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

    // ...
}



回答2:


In standalone.xml, add atributte url-charset="UTF-8" in the tag http-listener name="default", and add atributte default-encoding="UTF-8" in the tag servlet-container.




回答3:


Adding this to JBOSS_HOME/standalone/configuration/standalone.xml solved it for me:

<system-properties>
    <property name="org.apache.catalina.connector.URI_ENCODING" value="UTF-8"/>
    <property name="org.apache.catalina.connector.USE_BODY_ENCODING_FOR_QUERY_STRING" value="true"/>
</system-properties>

Got it from https://developer.jboss.org/message/643825#643825



来源:https://stackoverflow.com/questions/17628354/set-request-character-encoding-of-jsf-input-submitted-values-to-utf-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!