Rewriting URLs using pretty-config - jsf

I am facing problem with rewriting urls using pretty-config.xml and I want help. This is what I want.
I want to render the URL as:
http://www.example.com/{productId}
and the page actual URL is:
http://www.example.com/page/product.jsf
In short, I have one page but I want to render it each time as different url based on product id from the backing bean.

Sounds fairly simple. I would use this:
<url-mapping>
<pattern value="/#{productId}" />
<view-id value="/pages/product.jsf" />
</url-mapping>
Just make sure that you generate the correct links in your pages upon rendering.

Related

How to redirect(Bidirectional way) using pages.xml by EL methods in JSF and Seam project?

I'm facing a problem while rewriting the pattern in pages.xml dynamically.
I have a mainmenu and submenus on the webpage, I have a scenario like when user give's www.website.com/mainmenu/submenu1 , EL method should execute and rewrite that particular page dynamically.Because all the menus and submenu's will come from backend. Also it should work from home page when user select submenu item. I mean, it should work in both the ways(bidirectional).
Critical part is, how to execute and get the /mainmenu/submenu names from backend when user enter's url in address bar. This must use pages.xml only by using seam framework.
Thanks in advance!! Please let me know if my explanation is not clear.
Try reading the seam docs....
<page view-id="/yourPage.xhtml">
<rewrite pattern="/{mainmenu}/{submenu}"/>
<param name="mainMenu" required="true" value="#{yourBean.mainMenu}"/>
<param name="subMenu" required="false" value="#{yourBean.submenu}"/>
<action execute="#{yourBean.processPage}" on-postback="false"/>
</page>
SEAM Documentation

Different urls mapping to the same xhtml file

With jsf 2.x I want to use urls like
../admin1.xhtml
../admin2.xhtml
../admin3.xhtml
that should all call the same xhtml file (generic.xhtml), but with a parameter like this:
../generic.xhtml?page=admin1
../generic.xhtml?page=admin2
../generic.xhtml?page=admin3
instead of creating a lot of useless identical xhtml files to serve the requests. How can i better achieve that with jsfs? Am i bound to write loads of xml or can i make a simple rule in faces-config.xml, or should i use some other tools?
Make use of PrettyFaces url prettyfier. First of all it'll allow you removing your file extensions, which is considered a best practice (you would be able to change your backend framework without altering the urls themselves).
Apart from that, there's the chance to integrate the parameter itself into the url:
<!-- Maps "/admin/#{page}" to the URL "/generic.xhtml?page=value" -->
<url-mapping id="admin-view">
<pattern value="/admin/#{page}" />
<view-id value="/generic.xhtml" />
</url-mapping>
So you'll be able to type:
/admin/admin1
And that will drive you to:
/generic.xhtml?page=admin1

Pretty faces: one mapping configuration for multi .xhtml page

I have one question. How could pretty faces do this:
<code>
<url-mapping id="home">
<pattern value="/viewer" />
<view-id value="/pages/*" />
</url-mapping>
</code>
Well, I wonder if pretty faces could hide paths of all .xhtml in folder using just one configuration as shown as above, instead of to config for each and every file.
This mapping doesn't make any sense. To which view should PrettyFaces forward if the client requests /viewer?
However, you can do something similar with Rewrite, which is the successor of PrettyFaces. With Rewrite you can do something like:
.addRule( Join.path("/viewer/{page}").to("/pages/{page}.xhtml") )
This will basically map your URLs like this:
/viewer/foo -> /pages/foo.xhtml
/viewer/bar -> /pages/bar.xhtml
/viewer/whatever -> /pages/whatever.xhtml
If you want to migrate your app to Rewrite, which is really simple, have a look at the PrettyFaces Migration Guide.

URL rewrite for JSF

I'm using JSF, Jboss. I used urlrewrite filter and i don't know why:
when i type localhost:8080/myweb/user/myname will be forwarded to localhost:8080/myweb/user.xhtml?u=myname. it makes me don't like urlrewritefilter.
After that, i try using prettyfaces. Maybe, it is good for others, not me. i can't find out good tutorials except the documentation. ajax error after adding prettyfaces into my project. And some codes in pretty-config.xml
<url-mapping id="ideas">
<pattern value="/article/#{g}" />
<view-id value="/ideas/article.xhtml" />
</url-mapping> -->
And a form in jsf page will redirect this page with param
public String addUserToGroup() {
...
return "/ideas/article.xhtml?g=" + g + "&faces-redirect=true";
}
can't run.
Can you give me some advices about what library i should use to rewrite URL now. Or how to fix errors of prettyfaces. thanks
Is your configuration commented out? I noticed the '-->' in your code snippit.
<url-mapping id="ideas">
<pattern value="/article/#{g}" />
<view-id value="/ideas/article.xhtml" />
</url-mapping> -->

using pretty faces with web filters

Using Tomcat 7 --- Primefaces 3.4.1 --- javax faces 2.1.17 --- prettyfaces-jsf2 3.3.3
I configured pretty faces on my project correctly but my web filters are not working with new urls which are written by pretty faces.
Here is an example pretty-config.xml
<url-mapping id="home">
<pattern value="/home"/>
<view-id value="/secure/homepage.xhtml"/>
</url-mapping>
<url-mapping id="register">
<pattern value="/register"/>
<view-id value="/public/register.xhtml"/>
</url-mapping>
<url-mapping id="welcome">
<pattern value="/"/>
<view-id value="/public/welcome.xhtml"/>
</url-mapping>
<url-mapping id="profile">
<pattern value="/profile/#{userId}"/>
<view-id value="/profile.xhtml"/>
</url-mapping>
login(welcome) and register pages are in "public" folder, and their web filter is defined with annotation : #WebFilter("/public/*")
for my home page in "secure" folder (exactly there will be more pages in the folder), i defined a web filter also and its annotation : #WebFilter("/secure/*)
pretty urls are working fine, but these filters are only working when i write the original urls.
1) How can i repair my webfilters ?
2) I also want to block user for entering original url. I know that pretty faces is hiding original urls fully but is there a way to do it ?
-- SOLVED -- thanks for BalusC
if you defined your filters with annotations, you can configure dispatcher settings like
#WebFilter(urlPatterns = "/public/*", dispatcherTypes = {DispatcherType.REQUEST, DispatcherType.FORWARD})
PrettyFaces uses like many URL-rewriting solutions RequestDispatcher#forward() to forward the request to the desired target resource.
Servlet filters, when mapped without any <dispatcher>, listens by default on "initial" requests only, not on forwarded, included, nor error'ed requests.
So, when you map another servlet filter in web.xml after the PrettyFaces one, then it would by default not be triggered, unless you explicitly set a <dispatcher> on FORWARD next to the default of REQUEST (you should keep this one for the case PrettyFaces actually doesn't need to perform a forward).
<filter-mapping>
...
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
Or, for the case you're using #WebFilter on your filters, use the dispatcherTypes attribute:
#WebFilter(..., dispatcherTypes = { REQUEST, FORWARD })
Alternatively, if the filter in question doesn't change the request/response target in any way, e.g. setting the charset, compressing using Gzip, listening on exceptions, etc, then you can also just put it before the PrettyFaces one.

Resources