简体   繁体   English

从 javascript 运行 jsf 托管 bean 方法

[英]Running jsf managed bean method from javascript

I'm pretty new in JS and faced an issue of using managed bean from javascript.我是 JS 的新手,遇到了使用 javascript 中的托管 bean 的问题。

I'm trying to do this by means of h:inputHidden but still don't have a correct behavior.我试图通过 h:inputHidden 来做到这一点,但仍然没有正确的行为。

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

and my script和我的剧本

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

</script>

Am i do smth in wrong way?我做错事了吗? And there are another ways to define JS variable by running managed bean method?还有另一种通过运行托管bean方法来定义JS变量的方法吗?

Thanks in advance!提前致谢!

EDIT编辑

I need it for rich:calendar customizing.我需要它来丰富:日历定制。 I need to allow user to pick date from particular period.我需要允许用户从特定时期选择日期。

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

and full JavaScript for this is:和完整的 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>

To get managed bean value from hidden JSF input in JS you can using jQuery in following way:要从 JS 中隐藏的 JSF 输入获取托管 bean 值,您可以按以下方式使用 jQuery:

First use h:inputText instead to definle class for search in jQuery ('classForSearch').首先使用 h:inputText 来定义 class 以在 jQuery('classForSearch')中进行搜索。 To hide input it add simple CSS class ('inpt-hidden'):要隐藏输入,请添加简单的 CSS class('inpt-hidden'):

<style>
    .inpt-hidden { display: none; }
</style>

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

After that you will be able to access it using jQuery:之后,您将能够使用 jQuery 访问它:

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

</script>

Hot to setup jQuery you can find at: http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery#Setup热设置 jQuery 您可以在以下位置找到: http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery#Setup

Managed bean method will not run on attempt to get element value on client side.托管 bean 方法不会在客户端尝试获取元素值时运行。 It just return value that already present after page load.它只是返回页面加载后已经存在的值。 To run method you could use a4j:jsFunction (or its analog if you do not use jsf 1.2) with parameters you need.要运行方法,您可以使用 a4j:jsFunction(如果您不使用 jsf 1.2,则使用其模拟)和您需要的参数。

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

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