PrimeFaces 3.3 <p:datatable> 过滤失败,并显示 UTF8



我有一个PrimeFaces 3.3/JSF应用程序,我将其部署到JBoss AS 7.1。为了显示我的数据,我使用了带有一些筛选标题的p:dataTable。以下是代码(缩小来源范围后):

<p:outputPanel id="custDataTable">            
        <p:dataTable var="item" value="#{customerController.items}" rowKey="#{item.id}"
                        selection="#{customerController.current}" selectionMode="single" id="customersTable">
            <p:column headerText="Surname" sortBy="#{item.surname}" filterBy="#{item.surname}" id="surname">  
                #{item.surname}
            </p:column>  
            <p:column headerText="Age" sortBy="#{item.age}" filterBy="#{item.age}" id="age" styleClass="age">
                #{item.age}
            </p:column>
            <p:column headerText="&nbsp;">
                <p:spacer width="20" height="0" />
                <p:commandButton update=":custForm" ajax="false" action="#{customerController.prepareEdit}" value="edit">
                    <f:setPropertyActionListener value="#{item}" target="#{customerController.current}" />
                </p:commandButton>
            </p:column>
        </p:dataTable>  
</p:outputPanel>

PrimeFacesp:dataTable对数字Age列的过滤总是有效的,但在Surname栏上会出现一种奇怪的行为。当backingbean的实例变量的姓氏中包含ASCII数据的元素时,过滤就起作用了。但当存在UTF8数据时,过滤仅部分起作用:

[1]我可以在列标题字段中键入我所在地区的UTF8字符,结果确实经过了筛选(这是有效的部分)。

[2]支持bean的当前实例变量始终为null。即绑定:

selection="#{customerController.current}"

似乎不起作用。我在CustomerController::prepareEdit方法中添加了一些日志记录,当按下editp:commandButton时,该值设置为null。因此,我无法编辑基于姓氏列过滤的实例(当存在UTF8数据时)。然而,当我在数字age列上进行筛选时,或者当我根本不进行筛选时可以编辑具有相同UTF8数据的相同实例。

为了解决这个问题,我尝试注册一个字符编码过滤器:

public class CharacterEncodingFilter implements Filter {
    @Override
    public void doFilter(ServletRequest req,
                             ServletResponse resp,
                             FilterChain chain)
             throws IOException, ServletException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        chain.doFilter(req, resp);
    }

并在我的web.xml中注册:

<?xml version="1.0" encoding="UTF-8"?>
<web-app ...
...
  <filter>                                                                                                                                           
      <filter-name>Character Encoding Filter</filter-name>                                                                                           
      <filter-class>mp.util.CharacterEncodingFilter</filter-class>                                                                                   
  </filter>      
</web-app>

但这也没用。

您需要确保请求字符编码设置为UTF-8。您可以使用servlet过滤器来实现这一点,该过滤器映射到覆盖感兴趣请求的URL模式上。例如/*或仅在FacesServlet的servlet名称上。

@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);
    }
    // ...
}

另请参阅:

  • 通过PrimeFaces输入组件检索的Unicode输入已损坏

相关内容

最新更新