繁体   English   中英

从 javascript 运行 jsf 托管 bean 方法

[英]Running jsf managed bean method from javascript

我是 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>

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

首先使用 h:inputText 来定义 class 以在 jQuery('classForSearch')中进行搜索。 要隐藏输入,请添加简单的 CSS class('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

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

暂无
暂无

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

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