0

我使用 jsf 和使用 netbeans 7.3.1 的 eclispeLink 开发了一个 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."); 
    }

}

我的模板.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>

问题是什么?我该如何解决?

4

1 回答 1

0

一切看起来都很好!您应该检查以下内容:

a) 检查您的 Glassfish Resources 内部是否有以下内容:

<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;
于 2014-10-30T16:15:02.917 回答