问题描述:

What I want is to use a boolean value from applicationContext.properties (Jboss configuration file) in my javascript.

Currently I use Spring to inject a value configured in applicationContext.properties into my backingbean.

Then I put an output text in my jsp like this

<h:outputText id="idValue" styleClass="foo" value="" rendered="#{bean.isRendered}"/>

In my JavaScript I try following

 jQuery(function(){

bRedirect = jQuery(".foo").value != undefined;

...

All this looks so terrible to me, even though it works fine. There must be a smarter way than doing what I do.

Note that I am running JSF1.2 and therefore must use jQuery instead of $ and also select by unique class (foo) and not by id, which may be bad practice as well.

Thank you in advance. alfons

网友答案:

Assuming the function is to be called whenever part is rendered you can embed just your function call in the template:

<script type="text/javascript">
  myFunction("#{springExpressionForBooleanValue}")
</script>

and myFunction definition resides in .js file not processed by JSF. I can't see why you are unable to use $ to address jQuery object in such scenario.

If the function is to be called as a result of some interaction you can make it part of onclick/oblur etc attribute of your JSF component.

You can also embed entire function in the script tag but I think it's better to separate JavaScript logic from your templates.

网友答案:

Weeks after I fixed the problem, I came across t:jsValueSet today.

Which, in my case, would have been the best fitting way to solve above problem. You can use it as follows:

<t:jsValueSet name="test" value="#{myBean.myInfoText}"/>

In your javaScript you now can access the new variable called test:

 <script type="text/javascript">
   alert (test);
 </script>
相关阅读:
Top