繁体   English   中英

JSF 在绑定时抛出参数类型不匹配

[英]JSF throws argument type mismatch when binding

我正在尝试执行以下操作:当用户在rich:calendar组件中输入一个值时, h:inputText应将其required属性设置为true 我正在按照这篇文章中的说明进行操作: 通过 JavaScript 引用 JSF 控件的属性

很抱歉创建另一篇文章,但我不知道如何将代码发布到评论区并使其可读。 该页面抛出此错误:

javax.el.ELException: /pages/overtime/overtime-n.xhtml @121,65 binding="#{oc.overtimeDate}": java.lang.IllegalArgumentException: argument type mismatch

问题是绑定在c:forEach循环内,我正在尝试使用循环变量进行绑定。 overtime.overtimeItems被定义为一个ArrayList<OvertimeComponent> ,每个 OvertimeComponent 对象都具有各种属性( overtimeDateovertimeDateIdidovertimeHours等)。

<c:forEach items="#{overtime.overtimeItems}" var="oc">
  <rich:calendar value="#{oc.overtimeDate}" 
    requiredMessage="Date 1 is required."
    id="#{oc.overtimeDateId}" 
    binding="#{oc.overtimeDate}" 
    required="#{oc.id == 1 ? true : false}">
  </rich:calendar>
  <h:inputText value="#{oc.overtimeHours}" 
    id="#{oc.overtimeHoursId}"
    requiredMessage="Hours is required." 
    required="#{not empty oc.overtimeDate.value}" >
  </h:inputText>
  .....
</c:forEach>

如果rich:calendar对象有值,我如何使h:inputText必需? 有一种方法可以使用日历的binding属性来执行此操作,但我不确定如何在c:forEach 我不能在这个项目中使用 AJAX。 谢谢。

binding属性应该指向UIComponent ,而不是像Date这样的值对象。 如果您没有使用c:forEach ,您上一个问题中的答案会起作用。

<rich:calendar value="#{oc.overtimeDate}" 
    requiredMessage="Date 1 is required."
    id="#{oc.overtimeDateId}" 
    binding="#{calendarComponent}" 
    required="#{oc.id == 1 ? true : false}">
</rich:calendar>
<h:inputText value="#{oc.overtimeHours}" 
    id="#{oc.overtimeHoursId}"
    requiredMessage="Hours is required." 
    required="#{not empty calendarComponent.value}" >
</h:inputText>

上面的示例将组件绑定到“页面范围”,而不是特定的 bean,因为您通常对 bean 内部的组件不感兴趣。 您可以随意命名#{calendarComponent} 您可以在同一页面的任何位置以相同的名称访问它。

但是,在您的特定情况下,您使用的是c:forEach而不是ui:repeat ,因此组件实际上在组件树中ui:repeatui:repeat仅在 HTML 渲染器中重复)。 这意味着您不能使用建议的方法。 然后所有组件将共享相同的绑定,这是错误的。 最好将UIComponent (更准确地说, UIInput )属性添加到oc后面的对象并绑定到它,以便每个组件都有自己唯一的绑定。 例如

public class OvertimeComponent {
    private UIInput calendarComponent;
    // ...

<rich:calendar value="#{oc.overtimeDate}" 
    requiredMessage="Date 1 is required."
    id="#{oc.overtimeDateId}" 
    binding="#{oc.calendarComponent}" 
    required="#{oc.id == 1}">
</rich:calendar>
<h:inputText value="#{oc.overtimeHours}" 
    id="#{oc.overtimeHoursId}"
    requiredMessage="Hours is required." 
    required="#{not empty oc.calendarComponent.value}" >
</h:inputText>

(请注意,我简化了rich:calendar required属性的 EL,因为它已经返回了一个boolean

为什么日历上的值和组件绑定都指向同一个 el 表达式? 您不能在类型为 date 的 Richfaces 日历上进行组件绑定。

另外,您是否尝试过使用 ui:repeat 而不是 c:foreach ?

https://rogerkeays.com/jsf-c-foreach-vs-ui-repeat

众所周知,c:foreach 会在与 jsf 和 jsp 生命周期不匹配相关的 facelets 上下文中出现问题。 查看上面的帖子以获取更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM