My goal is to pass a string with parameters (Hello {0}
) from the backing bean to JavaScript. My current approach is to put the string in a h:inputHidden
and let JS read the value onLoad.
Sadly f:param
inside of h:inputHidden
is not working as in a h:outputFormat
.
What approach is generally advisible?
- I could combine the string with the parameters in the backing bean, but imo that should not be that beans concern.
- I could combine them in JS, but again that seems like reinventing the wheel (using
replace
functions ...) - I dont see a (good) way to use a
h:outputFormat
but make it invisible to the user. - I see no way to make
h:inputHidden
acceptf:param
s.
EDIT: The actual implementation used is Apache MyFaces 1.1.4, so I updated the title to JSF 1.1.
Due to BalusCs answer I remembered my first (and discarded) approach:
Direct inclusion in JS source code.
<script> var text = '<h:outputFormat value="Hello {0}"><f:param value="World" /></h:outputFormat>'; </script>
Sadly in JSF 1.1 I have to use <f:verbatim>
which makes the code really ugly (even eclipses syntax checker doesnt understand it - although its working):
<f:verbatim>
<script>
var text = '</f:verbatim>
<h:outputFormat value="Hello {0}"><f:param value="World" /></h:outputFormat>
<f:verbatim>';
</script>
</f:verbatim>
And an additional drawback is that I cannot put that in the <head>
since it has to be inside <f:view>
.