Running jsf managed bean method from javascript
I'm pretty new in JS and faced an issue of using managed bean from javascript.
I'm trying to do this by means of h:inputHidden but still don't have a correct behavior.
<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?
开发者_如何学GoThanks 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:
<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) > day.date.getTime()) return true;
if (curDt.getTime() < (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:
First use h:inputText instead to definle class for search in jQuery ('classForSearch'). To hide input it add simple 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:
<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
Managed bean method will not run on attempt to get element value on client side. 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.
精彩评论