【问题标题】:Running jsf managed bean method from javascript从 javascript 运行 jsf 托管 bean 方法
【发布时间】:2011-05-24 14:47:22
【问题描述】:

我是 JS 的新手,遇到了从 javascript 使用托管 bean 的问题。

我正在尝试通过 h:inputHidden 执行此操作,但仍然没有正确的行为。

<h:inputHidden id="hidden" value="#{bean.myVariable}" />

还有我的剧本

<script type="text/javascript">
        function func(){
            var varFromBean = document.getElementById('myForm:myVariable').value;
            ....
        }

</script>

我做错事了吗? 还有另一种方法是通过运行托管bean方法来定义JS变量吗?

提前致谢!

编辑

我需要它来进行丰富的日历定制。我需要允许用户从特定时期选择日期。

<rich:calendar value="#{bean.selectedDate}"
               isDayEnabled="disableDays" dayStyleClass="disabledDaysStyle"
               firstWeekDay="1"/>

完整的 JavaScript 是:

<script type="text/javascript">
        function disableDays(day){
            var curDt = new Date();
            if (curDt == undefined){
                curDt = day.date.getDate;
            }
            var period = document.getElementById('form:period').value;
            if ((curDt.getTime() + period) &gt; day.date.getTime()) return true;
            if (curDt.getTime() &lt; (day.date.getTime()))  return true;
            else return false;
        }
        function disabledDaysStyle(day){
            if (!disableDays(day)) return 'rich-calendar-boundary-dates';
        }
</script>

【问题讨论】:

    标签: javascript jsf richfaces


    【解决方案1】:

    要从 JS 中隐藏的 JSF 输入中获取托管 bean 值,您可以通过以下方式使用 jQuery:

    首先使用 h:inputText 代替在 jQuery ('classForSearch') 中定义搜索类。要隐藏输入,请添加简单的 CSS 类('inpt-hidden'):

    <style>
        .inpt-hidden { display: none; }
    </style>
    
    <h:inputText value="#{bean.myVariable}" styleClass="inpt-hidden classForSearch" />
    

    之后,您将能够使用 jQuery 访问它:

    <script type="text/javascript">
            function func(){
                var varFromBean = jQuery('.classForSearch').val();
                ....
            }
    
    </script>
    

    热设置 jQuery 你可以找到:http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery#Setup

    【讨论】:

    • 我想补充一点,你也可以给 h:inputText 一个 ID 并在 ID 上使用 jQuery 方法。
    • 非常感谢,这可能会有所帮助。但我想知道除了使用 h:inputText 是否还有其他方法?这似乎很不自然..?
    【解决方案2】:

    在客户端尝试获取元素值时,托管 bean 方法将不会运行。它只是返回页面加载后已经存在的值。要运行方法,您可以使用 a4j:jsFunction(如果您不使用 jsf 1.2,则使用它的模拟)和您需要的参数。

    【讨论】:

      猜你喜欢
      • 2013-01-24
      • 2014-07-02
      • 2023-03-05
      • 2012-03-26
      • 1970-01-01
      • 2016-07-05
      • 2016-07-09
      • 2013-04-16
      • 2012-10-13
      相关资源
      最近更新 更多