jsf facelet波斯字符编码错误

时间:2013-09-08 12:34:43

标签: jsf encoding filter

我使用jsf和eclispeLink开发了一个带有netbeans 7.3.1的java应用程序,但是我的角色没有正确保存。我查看请求变量,发现我的字符未在请求对象中正确显示。

我的Jsf样本:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core">

    <ui:composition template="/template.xhtml">
        <ui:define name="title">
            <h:outputText value="#{bundle.CreateTblAccessTitle}"></h:outputText>
        </ui:define>
        <ui:define name="body">
            <h:panelGroup id="messagePanel" layout="block">
                <h:messages errorStyle="color: red" infoStyle="color: green" layout="table"/>
            </h:panelGroup>
            <h:form>
                <h:panelGrid columns="2">
                    <h:outputLabel value="#{bundle.CreateTblAccessLabel_id}" for="id" />
                    <h:inputText id="id" value="#{tblAccessController.selected.id}" title="#{bundle.CreateTblAccessTitle_id}" required="true" requiredMessage="#{bundle.CreateTblAccessRequiredMessage_id}"/>
                    <h:outputLabel value="#{bundle.CreateTblAccessLabel_userName}" for="userName" />
                    <h:inputText id="userName" value="#{tblAccessController.selected.userName}" title="#{bundle.CreateTblAccessTitle_userName}" />
                    <h:outputLabel value="#{bundle.CreateTblAccessLabel_userTypeId}" for="userTypeId" />
                    <h:selectOneMenu id="userTypeId" value="#{tblAccessController.selected.userTypeId}" title="#{bundle.CreateTblAccessTitle_userTypeId}" >
                        <f:selectItems value="#{tblUserTypeController.itemsAvailableSelectOne}"/>
                    </h:selectOneMenu>
                    <h:outputLabel value="#{bundle.CreateTblAccessLabel_formId}" for="formId" />
                    <h:selectOneMenu id="formId" value="#{tblAccessController.selected.formId}" title="#{bundle.CreateTblAccessTitle_formId}" required="true" requiredMessage="#{bundle.CreateTblAccessRequiredMessage_formId}">
                        <f:selectItems value="#{tblFormController.itemsAvailableSelectOne}"/>
                    </h:selectOneMenu>
                    <h:outputLabel value="#{bundle.CreateTblAccessLabel_activitesId}" for="activitesId" />
                    <h:selectOneMenu id="activitesId" value="#{tblAccessController.selected.activitesId}" title="#{bundle.CreateTblAccessTitle_activitesId}" required="true" requiredMessage="#{bundle.CreateTblAccessRequiredMessage_activitesId}">
                        <f:selectItems value="#{tblAccessActivitiesController.itemsAvailableSelectOne}"/>
                    </h:selectOneMenu>
                </h:panelGrid>
                <br />
                <h:commandLink action="#{tblAccessController.create}" value="#{bundle.CreateTblAccessSaveLink}" />
                <br />
                <br />
                <h:commandLink action="#{tblAccessController.prepareList}" value="#{bundle.CreateTblAccessShowAllLink}" immediate="true"/>
                <br />
                <br />
                <h:link outcome="/index" value="#{bundle.CreateTblAccessIndexLink}"/>
            </h:form>
        </ui:define>
    </ui:composition>

</html>

MY过滤器更改编码:

public class EncodingFilter implements Filter{

    private String encoding;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        encoding = filterConfig.getInitParameter("requestEncoding");
        if( encoding==null ) encoding="UTF-8";
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        httpRequest.setCharacterEncoding(encoding);
        httpResponse.setCharacterEncoding(encoding);
        response.setContentType("text/html;charset=utf-8");
        chain.doFilter(httpRequest, httpResponse);
    }

    @Override
    public void destroy() {
        throw new UnsupportedOperationException("Not supported yet."); 
    }

}

我的Template.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <h:head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title><ui:insert name="title">Default Title</ui:insert></title>
        <h:outputStylesheet name="css/jsfcrud.css"/>
    </h:head>

    <h:body>
        <h1>
            <ui:insert name="title">Default Title</ui:insert>
        </h1>
        <p>
            <ui:insert name="body">Default Body</ui:insert>
        </p>
    </h:body>
</html>

我的web.xml

<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>business.EncodingFilter</filter-class>
        <init-param>
                <param-name>requestEncoding</param-name>
                <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <jsp-config>
        <jsp-property-group>
            <url-pattern>*.xhtml</url-pattern>
            <page-encoding>UTF-8</page-encoding>
        </jsp-property-group>
    </jsp-config>
</web-app>

有什么问题?我怎么能解决它?

1 个答案:

答案 0 :(得分:0)

每件事看起来都很好!您应该检查以下内容:

a)检查您的Glassfish资源是否在内部跟踪:

<property name="useUnicode" value="true"/>
<property name="characterEncoding" value="UTF8"/>

b)您的数据库和表格是否为UTF-8 CHARCTER SET

对于mysql使用以下代码:

ALTER DATABASE dbname DEFAULT CHARACTER SET utf8;

USE dbname;

ALTER TABLE tblname CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;