0

I have an url: example.com/mypage how can I make jsf page based on mypage value? Is it somehow possible to get it from FaceletsContext?

Only way how to do it which I thought is by creating a filter and parsing uri, but it must be very very wrong:

protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
    String uri = request.getRequestURI();
    if (uri == null || uri.length() == 0 || uri.equals("/") || !uri.contains("/") || uri.contains("xhtml")) {
        chain.doFilter(request, response);
        return;
    }
    List<String> parts = new ArrayList<String>(Arrays.asList(uri.split("/")));
    parts.remove("");
    if (parts.size() != 1) {
        chain.doFilter(request, response);
        return;
    }
    forwardTo(request, response, "page.xhtml?value=" + parts.get(0));
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
sandris
  • 1,478
  • 2
  • 18
  • 34
  • Well, you have to use a filter for that. I think you can give a look to [PrettyFaces](http://ocpsoft.org/prettyfaces/) for your url rewriting – Luiggi Mendoza Jan 22 '14 at 20:47
  • What version of JSF are you using? – LaurentG Jan 23 '14 at 07:23
  • Oh, I forgot to mention it - 2.0 – sandris Jan 23 '14 at 07:28
  • You mean, you want extensionless URLs? If so, OmniFaces FacesViews may be helpful. This question is then a dupe: [Customize FacesServlet to get rid of .xhtml extension](http://stackoverflow.com/questions/18508329/customize-facesservlet-url-pattern-to-get-rid-of-xhtml-extension/) – BalusC Jan 23 '14 at 11:34

1 Answers1

0

Since you are using JSF 2, I would suggest you to use an URL-rewriting framework (like PrettyFaces) or tool to map all HTTP requests from example.com/mypage to example.com/page.xhtml?page=mypage.

What you first tried to do with the ServletFilter can be done by such a framework.

As soon as the page is given to you as a query parameter, you can map it to a managed bean value using the JSF2 viewParam functionality:

<f:metadata>
  <f:viewParam name="page" value="#{someBean.page}" />
</f:metadata>
Community
  • 1
  • 1
LaurentG
  • 11,128
  • 9
  • 51
  • 66