来自onChange的JSF动作

时间:2012-08-02 17:50:35

标签: jsf input onchange

我正在尝试使用JSF设置一个数据表,该数据表具有行总计的行,它将对行中任何框的行总数onChange求和。该行看起来像:

<tr id="input_row_1">
<td id="mondayInput1">
    <h:inputText id="monday1" size="4" value="#{timesheet.monday1}" required="false" />
    <!-- <input name="monday1" class="smallInput" type="number" size="2" maxlength="4" min="0"/> -->
</td>
<td id="tuesdayInput1">
    <h:inputText id="tuesday1" size="4" value="#{timesheet.tuesday1}" required="false" />
    <!--<input name="tuesday1" class="smallInput" type="number" size="2" maxlength="4" min="0"/> -->
</td>
<td id="wednesdayInput1">
    <h:inputText id="wednesday1" size="4" value="#{timesheet.wednesday1}" required="false" />
    <!--<input name="wednesday1" class="smallInput" type="number" size="2" maxlength="4" min="0"/> -->
</td>
<td id="thursdayInput1">
    <h:inputText id="thursday1" size="4" value="#{timesheet.thursday1}" required="false" />
    <!--<input name="thursday1" class="smallInput" type="number" size="2" maxlength="4" min="0"/> -->
</td>
<td id="fridayInput1">
    <h:inputText id="friday1" size="4" value="#{timesheet.friday1}" required="false" />
    <!--<input name="friday1" class="smallInput" type="number" size="2" maxlength="4" min="0"/> -->
</td>
<td id="saturdayInput1">
    <h:inputText id="saturday1" size="4" value="#{timesheet.saturday1}" required="false" />
    <!--<input name="saturday1" class="smallInput" type="number" size="2" maxlength="4" min="0"/> -->
</td>
<td id="sundayInput1">
    <h:inputText id="sunday1" size="4" value="#{timesheet.sunday1}" required="false" />
    <!--<input name="sunday1" class="smallInput" type="number" size="2" maxlength="4" min="0"/> -->
</td>
<td>
    <h:inputText id="total1" size="4" value="#{timesheet.total1}" required="false" />
    <!-- <input name="total1" id="total1" type="text" size="5" readonly="readonly" /> -->
</td>

所以我首先尝试将onChange="#{timesheet.setTotal1}"添加到日期条目中,但onChange事件调用javascript,而不是java / JSF代码。有人可以帮助让总数更新吗?

编辑: 我接受了BalusC的回答,但还需要在event标记中包含<f:ajax>属性才能使其正常工作,就像这样

<h:inputText id="friday1" size="4" value="#{timesheet.friday1}" maxlength="4" required="false" >
    <f:ajax event="change" render="total1" listener="#{timesheet.updateTotal1}" />
</h:inputText>

1 个答案:

答案 0 :(得分:7)

实际上,onchange属性仅被视为纯文本HTML属性,它应代表JavaScript onchange处理函数。这与“普通香草”HTML没有什么不同。该属性中的任何EL表达式都将被视为值表达式。另请参阅tag documentation

根据您的问题历史,您正在使用JSF 2.x(请在未来的问题中明确提及确切的JSF impl /版本,因为与JSF 1.x相比,JSF 2.x中的许多内容都有所不同)。因此,只需使用新的JSF 2.0 <f:ajax>标记即可。

<h:inputText ...>
    <f:ajax listener="#{timesheet.changeTotal1}" render="idOfTotalComponent" />
</h:inputText>

...

<h:outputText id="idOfTotalComponent" value="#{timesheet.total1}" />

public void changeTotal1(AjaxBehaviorEvent event) {
    total1 = ...;
}