glassfish: redirect all url to index - jsf

I'm working on the java ee application using glassfish 3 server and jsf.
I want all of requests to urls starting from my context root to be redirected to my index.xhtml.
For example, if a user types:
my-host/my-app-context-root/lgsfdjglksjdflgjldskfjg-anything
I want this request to be redirected to:
my-host/my-app-context-root/index.xhtml
So I want to know how can I implement this rule. If it's possible, I would like to do it somehow using Java/jsf or web.xml or some other files which belongs to my application only. I want to avoid to do any "general" server configurations, such as setting properties "redirect_n" as it is suggested there.
Thanks in advance and sorry for my English.

You could create a servlet filter with mapping "/*". Then use HttpServletRequest#getRequestURI() to check whether this is a request for js/css files or any other request and you can accordingly redirect to index.xhtml.

One solution would be to use a servlet with / in web.xml. This servlet would Act as the Default servlet for your application and could output the content of index.xhtml.

Related

Set URL to dynamic URL in JSF [duplicate]

For example, I have class Article with methods getTitle () and getContent ().
I also have ArticlesService with method getAllArticles (). How to create a list of links with meaningful names (formed with #{article.title})? Like:
http://mysiteaddress.com/article/first-article-title
http://mysiteaddress.com/article/how-to-make-links-in-jsf
..or something similar.
I can create links with all necessary functionality with <h:commandLink>, but I don't know how to make nice 'href' for it: it always has href '#'.
I can create nice links with <h:outputLink> but I don't know how to add necessary functionality to it.
In jsp I created my own front-controller, which parsed urls from requests and then performed redirection to correspondent jsp-page.
How to achieve the same functionality in JSF?
If this is intended as an improvement of an existing application, then you basically need a Filter which detects "dirty" and "friendly" URLs. When it detects a "dirty" URL, then it should redirect the request to a "friendly" URL by HttpServletResponse#sendRedirect(). When it detects a "friendly" URL, then it should forward the request to the "dirty" URL by RequestDispatcher#forward(). An example can be found in this related question: How to use a servlet filter in Java to change an incoming servlet request url?
Further, you also need a custom ViewHandler to produce the desired "friendly" URL for JSF <h:form>, <h:link>, etc. An example can be found here: Dynamic Directory in Java EE Web Application.
If this is a new application or an application which is open to changes, you could consider any of the existing pretty URL libraries instead of reinventing the wheel:
PrettyFaces, which is a complete URL rewrite solution. It requires an additional XML configuration file pretty-config.xml. This library is useful if you want to completely change URLs and/or want to configure redirects from old to new URLs.
FacesViews of OmniFaces library, which makes existing URLs just extensionless by a single web.xml context param. It also supports "MultiViews" whereby path parameters can declaratively be injected in managed beans. E.g. /foo/bar/baz can point to /foo.xhtml and the values bar and baz can be injected by #Param(pathIndex).
There's also the experimental PrettyUrlPhaseListener of Mojarra Scales library, but it's an old library and PrettyFaces is largely based on it, so it's not worth the effort.
Did you tried PrettyFaces?
Simple, effective, bookmark, JSF
PrettyFaces is an OpenSource extension for JSF1.2 and JSF2.0, which enables creation of bookmark-able, pretty URLs. PrettyFaces solves this problem elegantly, including features such as: page-load actions, seamless integration with faces navigation, dynamic view-id assignment, managed parameter parsing, and configuration-free compatibility with other JSF frameworks.
SEO and Consistency:
Any business knows how important Search Engine Optimization can be for sales. PrettyFaces allows SEO-friendly URLs, and improved customer experience. Give your site a uniform, well understood feeling, from the address bar to the buy button.

Adding omnifaces project gives incorrect url at redirect

Using omnifaces 1.6 in a jsf project with primefaces 3.5 on glassfish 3.1.2
FacesContext.getCurrentInstance().getExternalContext().redirect("localhost/my-project/views/index.jsf");
Using this code will redirect to a page with url localhost/my-project/
This issue is when omnifaces is added without omnifaces the function works correctly with no problem
I don't know what you expect but when you redirect to localhost/my-project/ then a redirect to a page with url localhost/my-project/ seems like the correct behaviour.
Maybe you missed the port (e.g. redirect to localhost:8080/my-project/) but in general it is not intended to redirect to a URL containing localhost but to a specific page (e.g. redirect to index.jsf or index.xhtml). You don't even need the webapp context (i.e. my-project), you can just redirect to your "main page" like this:
FacesContext.getCurrentInstance().getExternalContext().redirect("home.xhtml");

How to set default home while developing jsf applications

I am developing a jsf application, and my application is already on the production environment. But there is something i want to know: if my domain name is: "mydomain.com" and in my application the home page is: "home.jsf", i would like my application to display the home page directly when i visit: www.mydomain.com
But what is happening now is to get my home page to display, i must write: www.mydomain.com/home.jsf . This seems to me very tedious.
Does anyone know a way to do this?
Just mention the welcome file in your web.xml as follows:
<welcome-file-list>
<welcome-file>home.jsf</welcome-file>
</welcome-file-list>
As far as JSF is concern, adding a simple welcome file wont produce good result. In addition to home.xhtml file, create an empty home.jsf file.. the server will not run the empty file (home.jsf) , wherelse it will proceed with home.xhtml. Then declare the welcome file in web.xml. for more have a look at this answer https://stackoverflow.com/a/4618142/839393

How to create addressable pages using JSF

Using the currrent version of Java EE, how do you create addressable web pages using Java Server Faces (JSF). That is, creating JSF pages that have a clean URL, so the page for the person entity with ID 1234 might be http://www.example.com/person/1234? It's clear enough to me how to service a clean URL using the Java API for RESTful web services (JAX-RS), but not how to do so for a JSF page, or how to combine the two.
A previous question I found suggests that doing so is not actually possible. Is that really so?
Use a URL rewriting solution like PrettyFaces. It uses basically a simple Filter under the covers which forwards the request from pretty to ugly URL and redirects the request from ugly to pretty URL based on some XML mapping file.
Related questions:
Bookmarkable URL in JSF application - Trying to use Spring Webflow and JSF . Any suggestions?
How to rewrite the URL
How do I configure JSF url mappings without file extensions?

how to create a link to a JSF page from outside the application

I am working with a JSF application that posts on every mouse click, so if you get 5 pages deep, your url stays the same.
now i need to link to one of these pages from outside of jsf. what can i do?
(i am new to jsf)
Navigation rules use forward by default, that's why the URL does not change.
Use http://site.com/ctx/page.jsf to access a given page. *.jsf is the mapping of your faces servlet to in web.xml (it can have different value in your configuration, but normally it is *.jsf or /faces/*
i dont know if ive got it right but as much as i understood it you need to use GET instead of POST this time, right?
in my current project we still use JSF 1.2 and that is achieved by PrettyFaces (http://ocpsoft.com/prettyfaces/) by us. do you mean that?

Resources