1

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

AlfonsSocken
  • 68
  • 2
  • 10
  • You say you must select by class, yet your example is selecting by ID? As far as practise goes, your example is fine. Selecting by class is a perfectly ligitimate practice. – Rory McCrossan Dec 01 '11 at 09:50
  • My fault. Wanted to use class selector. Edited that in question – AlfonsSocken Dec 01 '11 at 10:07
  • @RoryMcCrossan I primarily mentioned that I __have to__ select by class because I wanted to anticipate answers guiding me to use id selection due to performance advantages. see [Improve jQuery](http://www.tvidesign.co.uk/blog/improve-your-jquery-25-excellent-tips.aspx#tip8) – AlfonsSocken Dec 01 '11 at 10:19
  • As to selecting JSF elements by ID: http://stackoverflow.com/a/7928290/157882 – BalusC Dec 01 '11 at 11:04

2 Answers2

1

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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
mrembisz
  • 12,722
  • 7
  • 36
  • 32
  • Anytime I tried something like this I got 'PWC6228: #{...} not allowed in a template text body.' BTW my function should be called on dom ready – AlfonsSocken Dec 01 '11 at 14:54
  • 1
    @AlfonsSocken have a look here then: http://stackoverflow.com/questions/8213620/can-js-in-jsf-reach-the-resource-bundle – mrembisz Dec 01 '11 at 15:23
0

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>
AlfonsSocken
  • 68
  • 2
  • 10