tags:

views:

126

answers:

1

Is there a way to call java method in .xhtml?

I just want to be able to call java.net.URLEncoder.encode() method from xhtml file.

Is it possible to do this?

In jsp it was very easy to do <% String encodedURL = java.net.URLEncoder.encode(url, type); %>

+1  A: 

Use <f:param>.

<h:outputLink value="page.jsf">
    <f:param name="foo" value="#{bean.foo}" />
    <f:param name="bar" value="#{bean.bar}" />
</h:outputLink>

This will end up in page.jsf?foo=encodedFooValue&bar=encodedBarValue.

Note: scriptlets indeed eases writing raw Java code in a view template, but that does still not make it a good practice! Use taglibs/EL whenever possible, else the particular logic simply belongs in a real Java class such as a backing bean constructor or method.

BalusC