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

4
  • 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. Commented Dec 1, 2011 at 9:50
  • My fault. Wanted to use class selector. Edited that in question Commented Dec 1, 2011 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 Commented Dec 1, 2011 at 10:19
  • As to selecting JSF elements by ID: stackoverflow.com/a/7928290/157882 Commented Dec 1, 2011 at 11:04

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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 have a look here then: stackoverflow.com/questions/8213620/…
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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.