最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jsf 2 - How to obtain the value of h:inputHidden element whose value is calculated using jquery javascript - Stack Overflow

programmeradmin1浏览0评论

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 retrieve 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.

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 retrieve 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.

Share Improve this question asked May 6, 2011 at 1:54 user679526user679526 8454 gold badges16 silver badges40 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

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 mand 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:mandButton 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)

  1. 暂无评论