【问题标题】:Issue passing date type with f:param使用 f:param 发出传递日期类型
【发布时间】:2010-08-20 16:43:31
【问题描述】:

我正在尝试按日期和用户 ID 过滤 listPage。从数据表中,选定的用户 ID 和日期值通过 f:param 标记传递给 listPage。列表页面显示“值必须是日期”的错误,并且过滤器不起作用。如果它只是由没有日期的用户 ID 过滤,它就可以工作。我可能做错了什么?或者我怎样才能让它工作?我也尝试过使用 JBoss 接缝日期转换器,它也不起作用。

这是我的代码:

firstPage.xhtml

<h:column>
    <f:facet name="header">Date</f:facet>
            <h:outputText value="#{_auditEventFact[3]}">
            </h:outputText>
</h:column>



    <rich:column styleClass="action">
        <f:facet name="header">Action</f:facet>  
    <s:link value="View" view="/AuditEventFactList.xhtml" action="# auditEventFactList.lookUpUserAuditRecords()}">
    <f:param name="userid" value="#{_auditEventFact[1]}"/>
    <f:param name="ntimestamp" value="#{_auditEventFact[3]}"/>
  </s:link>                                             
    </rich:column>

pages.xml

<param name="from"/>
   <param name="userid" value="#{auditEventFactList.auditEventFact.userid}"/>
   <param name="ntimestamp" value="#{auditEventFactList.auditEventFact.ntimestamp}" converterId="javax.faces.DateTime"/>

ListAction.java

private static final String EJBQL = "select auditEventFact from AuditEventFact auditEventFact";
private static final String[] RESTRICTIONS = {
        "lower(auditEventFact.userid) like concat(lower(#{auditEventFactList.auditEventFact.userid}),'%')",
        "auditEventFact.ntimestamp = #{auditEventFactList.auditEventFact.ntimestamp}",
        "lower(auditEventFact.auditid) like concat(lower(#{auditEventFactList.auditEventFact.auditid}),'%')", };

private AuditEventFact auditEventFact = new AuditEventFact();   
public AuditEventFactList() {
    setEjbql(EJBQL);
    setRestrictionExpressionStrings(Arrays.asList(RESTRICTIONS));
    setMaxResults(25);
}

EntityBean.java

   @Temporal(TemporalType.TIMESTAMP)
    @Column(name="NTIMESTAMP#", length=11)
    public Date getNtimestamp() {
        return this.ntimestamp1;
    }

    public void setNtimestamp(Date ntimestamp1) {
        this.ntimestamp1 = ntimestamp1;
    }

【问题讨论】:

    标签: java jsf seam


    【解决方案1】:

    我不做 Seam,所以我无法给出针对 Seam 的答案。但基本上,f:param 设置了一个 HTTP 请求参数,该参数只能包含字符串,因为它们按照 HTTP 规范以 ASCII 风格传输。它并没有像您预期的那样作为完全值得的 java.util.Date 对象传递。相反,String.valueOf(date) 的结果被传递。其格式在Date#toString() 中指定。

    用简单的 JSF 术语来说,有两种解决方案:

    1. &lt;h:commandLink&gt;f:setPropertyActionListener 结合使用,它能够“传递”完全有价值的非标准Java 对象作为“参数”。

      <h:commandLink value="View" action="#{auditEventFactList.lookUpUserAuditRecords}">
          <f:setPropertyActionListener target="#{entityBean.ntimestamp}" value="#{_auditEventFact[3]}"/>
      </h:commandLink>
      
    2. ntimestamp 中使用String 而不是Date 或在其周围包裹另一个String 设置器,将String 转换为Date 并委托给原始Date 设置器。 java.text.SimpleDateFormat 可能对此有所帮助。

    看看这是否可以与 Seam 结合使用。

    【讨论】:

      【解决方案2】:

      您可以在参数上使用&lt;s:convertDateTime /&gt; 标记吗?我自己从未尝试过,但值得一看。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-26
        • 2017-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多