how to hide login view name in using pretty faces - jsf-2

I have one application running on tomcat sever.I removed the path in in server.xml so i am able to access the application like
http://localhost:8080/login.xhtml
I am using Pretty faces as well for URL rewrite.
Now i want to know how to send a request to server like
http://localhost:8080
so that it should display my login page.
My pretty-config.xml is
<url-mapping id="login">
<pattern value="/" />
<view-id value="/login.xhtml" />
</url-mapping>
it is not working.please let me know the solution.

Your pretty-config part (which is valid) tells that a path / should be mapped to /login.xhtml and may be referred to as pretty:login in outcomes of JSF components (as well as in action methods).
From here there are two possible options for inconsistencies:
Prettyfaces must be pointed to a valid URL, meaning that FacesServlet mapping in web.xml should be *.xhtml in your case, not faces/*, not *.jsf, etc.
Prettyfaces should be used appropriately in your JSF components. For instance, <h:link outcome="pretty:login" value="Home" /> will render an HTML a element with the value / (which will internally show login.xhtml when you click on it).
Also, it would be helpful to read the excellent PrettyFaces documentation.

Related

PrettyFaces url routing from form submit

I'm using Pretty Faces to do URL rewriting, in order to be able to reuse some xhtml files. I'd like to have URLs like '/honda/index.xhtml' and '/toyota/index.xhtml' both go to the same /make/index.xhtml file, with the make coming in as a parameter. This routing seems to work OK with a config like this:
<url-mapping id="carMake">
<pattern value="/#{make}/index.xhtml"></pattern>
<view-id value="/make/index.xhtml"/>
</url-mapping>
I also have this mapping for a search results type of page:
<url-mapping id="search">
<pattern value="/#{make}/search/index.xhtml" />
<view-id value="/search/index.xhtml" />
</url-mapping>
Both of these work as expected when I manually put the URL in the browser.
I run into a problem when I try to put a form on the first page, that I want to redirect to the second page. I have this jsf xhtml code for a form:
<h:form>
<h:messages />
<h:inputText id="searchTerm"/>
<h:commandButton value="search" action="/honda/search/index.xhtml?faces-redirect=true"/>
</h:form>
(hard coding /honda here to simplify the example)
When I try to submit this search, it bounces back to the same /honda/index.xhtml, with no messages being displayed on the page.
The logs show this:
09-23 11:39:55 DEBUG PrettyNavigationHandler:57 - Navigation requested: fromAction [/honda/search/index.xhtml?faces-redirect=true], outcome [/honda/search/index.xhtml?faces-redirect=true]
09-23 11:39:55 DEBUG PrettyNavigationHandler:60 - Not a PrettyFaces navigation string - passing control to default nav-handler
I've tried without the faces-redirect param, but got the same result.
Why does /honda/search/index.xhtml work when I put it into the browser directly, but not as the result of an action?
If you want to use separate the URL from the View ID, and you don't want to reference the view-id in the app, then you need to use pretty navigation strings:
<h:commandButton value="search"
action="pretty:honda"><f:param name="make" value="honda" /></h:commandLink>
But this really is just making it more complicated than it needs to be. I would recommend doing what #chkal suggested, except his example is a little wrong. It should have been:
<h:commandButton value="search"
action="/search/index.xhtml?faces-redirect=true&make=honda"/>
This should be covered in the docs :) http://ocpsoft.org/docs/prettyfaces/3.3.3/en-US/html/components.html#components.prettylink Check that section (and the ones below it) and see if that helps!
You cannot use a pretty URL as a value for the action attribute this way. You have to use a standard JSF outcome with make being a query parameter.
Try this:
<h:commandButton value="search"
action="/honda/search/index.xhtml?faces-redirect=true&make=honda"/>

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?

Mojarra 2.1.14 flash scope messages and redirect to different path

According to this: http://java.net/jira/browse/JAVASERVERFACES-2136 flash-"scoped" messages should survive a redirect to a page on a different path.. I wanted to use something like this in my application so i downloaded javax.faces-2.1.14-20121003.074348-10 snapshot from here
https://maven.java.net/content/repositories/snapshots/org/glassfish/javax.faces/2.1.14-SNAPSHOT/ to test.
My situation is this: I have a page (call it test.xhtml) in the root directory that in the view-scoped backing bean during the call of the constructor does a check and conditionally sets a message using Omnifaces Message.addFlashGlobalInfo and redirects to index.xthml also in the root directory using Omnifaces Faces.Redirect() (thanks BalusC!). In index.xhtml i have a Primefaces
<p:messages id="msg" showDetail="false" autoUpdate="true" />
I use the same "configuration" described above in other pages as well and it works fine when the redirect is done to the same page called the bean method.
So shouldn't the message survive the different path redirect or did i misunderstood something about this issue?? maybe there is something else wrong here??
Thanks in advance! (i'm looking forward hearing BalusC opinion on this btw :) )
i just used to call an init method that does sets message and redirects but again no message appears!! so i don't think PostConstruct will work either..
Indeed, the <f:event type="preRenderView"> is too late to set a flash message. The flash scope can't be created when JSF is currently sitting in render response phase. You basically need to set the flash message before render response phase. In spite of the name preRenderView, this event is actually fired during (the very beginning of) the render response phase.
The #PostConstruct may be on time, provided that it's not been called during render response. This however won't work very well together with <f:viewParam>.
To fix this, as you're using OmniFaces already, just use <f:event type="postInvokeAction">.
<f:metadata>
<f:viewParam name="some" value="#{bean.some}" />
<f:event type="postInvokeAction" listener="#{bean.init}" />
</f:metadata>
See also:
JSF - Keep Faces Messages after redirect from #PostConstruct
Adding faces message to redirected page using ExternalContext.redirect()

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

Is it possible for Seam url rewriting to add prefix to url?

What I need is to add an prefix (such as 'secure') for all urls which requires login, is it possible for Seam url rewriting to do this:
<page view-id="/view/*" login-required="true">
<rewrite pattern="/{prefix}/{url}" />
</page>
<page view-id="/view/home.xhtml">
<rewrite url="/home"/>
</page>
I don't think this would work, since it is ambigious view/* matches also view/home . In a similar situation I moved all pages to view/secure and forced login on these view-ids.
With an editor which supports global search/replace you can quickly change the references between pages.

Resources