开发者

How to obtain the value of h:inputHidden element whose value is calculated using jquery javascript

My requirement is when the user clicks the submit button, a new value calculated should be stored in the inputHidden field value. I have written function in jQuery to calculate the new value for the inputHidden field, when submit button is clicked. The new value is assigned to the inputHidden field value. But the problem is that while retrieving the value in the backing bean using hidden.getValue(), it returns null value.

    jQuery code:
    function hidden(){
    var valueCalculated = '3';
    $('#hidden').val(valueCalculated);
    alert($('#hidden').val());  //displays 3 when submit button is clicked.
    }

   JSF code:
   <h:inputHidden binding="#{bean.hidden}"/>

In the backing bean, I have getters and setters for hidden of type HTMLInputHidden, and I re开发者_开发知识库trieve the hidden value by using getValue(). This should return the valueCalculated but it returns null. What is the way to obtain the calculated value in the backing bean.


The HtmlInputHidden#getValue() will only return the submitted value when you're inside the invoke action phase. It's namely been set during the update model values phase. So if you're trying to get it during bean's construction or during other JSF phase before invoke action phase, you will get null.

To fix this, rewrite the code logic so that it is been accessed at the right moment; in the command button/link action method. Otherwise you have to get it manually from the request parameter map instead.

E.g.

<h:form id="form">
    <h:inputHidden id="hidden" value="#{bean.hidden}" />
    <h:commandButton value="submit" action="#{bean.submit}" onclick="$('#form\\:hidden').val('foo')" />
</h:form>

with

public void submit() {
    // Here, in the bean's action method, it should already be set.
    System.out.println(hidden); // "foo"
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜