Is there something better to generate absolute links? - jsf-2

Is a better way to generate absolute links in JSF 2.0 ? Right now I'm using <h:outputLink/> in that ugly way with #{facesContext.externalContext.requestContextPath} like below. I don't want to use JSTL and <c:url />
<h:outputLink value="#{facesContext.externalContext.requestContextPath}/pages/home.jsf">Home</h:outputLink>

You can shorten #{facesContext.externalContext.requestContextPath} to #{request.contextPath}. You can even get rid of it to use HTML <base> tag instead.
In this particular case, better is to use <h:link> instead. It can take a context-relative navigation case path in outcome attribute:
<h:link value="Home" outcome="pages/home" />
JSF will take care about adding the right context path and FacesServlet mapping while generating the <a> element:
Home
See also:
Communication in JSF 2.0 - Implicit navigation
How get the base URL?

Related

JSF2 - How to create a link containing parameters and anchor?

I want to produce a link looking like this:
http://domain.com/page.jsf?test=12#top
I tried using <h:outputLink> :
<h:outputLink value="/page.jsf#top">
<f:param name="test" value="12" />
My link
</h:outputLink>
But it failed and produced the link http://domain.com/page.jsf#top?test=12, which is wrong.
Is there a way to do this using JSF, or do I have to create this kind of links "manually"?
Since JSF 2.0 there is an <h:link> component that takes a JSF navigation case outcome through its outcome attribute, thus making it a perfect candidate for navigation within a JSF-based application. The component you used, <h:outputLink> is best used for navigation to the external world. You can of course use it to handle JSF navigation but it will feel plain clumsy.
If you decide to switch to <h:link> you can make use of its fragment attribute to attach your anchor (always look at the documentation - linked above):
The identifier of the page fragment which should be brought into focus when the target page is rendered. The value of this attribute is appended to the end of target URL following a hash (#) mark. This notation is part of the standard URL syntax.
All in all, your link should come as:
<h:link value="My link" outcome="/page" fragment="top">
<f:param name="test" value="12" />
</h:link>
It will produce the HTML that you desire.
Further point of reference:
When should I use h:outputLink instead of h:commandLink?

get the url until the context path

I use Icefaces and JSF and I have this problem:
I have the following url:
http://myMappedServer/myApp/followingThings
I would like to get in my xHtml page the value http://myMappedServer/myApp
How can I achieve this without using the managed bean?
Use EL: #{request.contextPath}.
It's quite useful for creating navigation links, to set in your main template a Facelets variable :)
<ui:param name="root" value="#{request.contextPath}/" />
UPDATE: It's not recommended to use the full path available in the app server because it's not guaranteed to be the same URL the user is using to access your app, so, beware of that.
If you really want, though, you can do that, using some methods available in HttpServletRequest to create an String like this:
#{request.scheme}://#{request.serverName}:#{request.serverPort}#{request.contextPath}

Creating a JSF URL without the windowid parameter

In my JSF web-app, I want to create a URL for the user to bookmark or copy. That part is easy enough, like this:
<h:link value="Permanent Link"
outcome="/showStuff">
<f:param name="recID" value="#{bean.recordID}" />
</h:link>
Although that link has the desired parameter (recID) in it, it also has the windowid parameter generated by the JSF Servlet. Is there any convenient way to generate the URL without the windowid parameter? Or does this make any difference?
(This is with Mojarra)
You can remove the WindowId using a URLRewriteFilter framework such as OCPsoft Rewrite URLRewriteFilter
Doing something like this should be fairly straightforward using a single configuration rule. You can obviously fiddle if this rule is either too strict or too general.
.defineRule()
.when(Direction.isOutbound().and(
URL.matches("{prefix}{windowId}{suffix}")
.where("windowId").matches("windowId=[^&]+")))
.perform(Substitute.with("{prefix}{suffix}"))
Check out the rewrite site. It's pretty easy to set up. http://ocpsoft.org/rewrite/

Changing Browser URL from backing bean in JSF 2

This is related to Changing Browser URL from backing bean
#balusc
How do I do the same thing in JSF 2? I have a search page with 6 different components (mostly PrimeFaces 3.0) for setting the search parameters. How can I get all the parameters to appear in the URL so that the user can share the results page using the URL?
I looked at PrettyFaces, and that seems to be capable of doing this in a better way, but I would rather make it work with just JSF2 if that is a simpler solution...
Thanks!
You should either manually specify all the parameters via nested f:param tags like this:
<h:link outcome="nextPage">
<f:param name="param1" value="val1" />
<f:param name="param2" value="val2" />
</h:link>
or just specify includeViewParams parameter:
<h:link outcome="nextPage" includeViewParams="true">
The same goes for h:button

why sec:authorize doesn't work?

I have a JSF 2 page based on Facelets and use Spring Security 3 behind the application. When I put some tags like this within my page:
<sec:authorize access="hasRole('SS')" >
<h:outputText value="X" /></sec:authorize>
the X will display at runtime anyway. The auto completion feature of eclipse work correctly to show the "sec:" tags and their properties at programming time. what's the problem?
Have you got:
<%# taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
at the top of the file?
Also, you need use-expressions="true" in the http tag in securityBeans.xml. Doing this means that any old style access="ROLE_BLAH" tags in securityBeans or wherever also need to change to use expressions.

Resources