0

I'm very used to Jax-RS and the ability to make data available to the caller through custom URLs like:

/bicycles/by_color/blue

by doing:

@Path("{searchCategory}/{searchParam}")
List<Item> get(@PathParam....

What is the equivalent in JSF? I have an application with a ReST API and would like to display a template page, grabbing a specific entity from the database according to part of the URL for the xhtml page to get data from that will be displayed.

I've seen examples with query parameters like '?id=5&pages=true' but nothing with URL mapping like '/5/true.' How do I accomplish this?

KG6ZVP
  • 3,610
  • 4
  • 26
  • 45
  • Possible duplicate of [How do I process GET query string URL parameters in backing bean on page load?](https://stackoverflow.com/questions/10724428/how-do-i-process-get-query-string-url-parameters-in-backing-bean-on-page-load) – Kukeltje May 27 '17 at 21:16
  • And combine the duplicate with prettyfaces – Kukeltje May 27 '17 at 21:16
  • Not a duplicate. This is specifically asking how to map URLs, NOT PARAMETERS. – KG6ZVP May 27 '17 at 21:58
  • There was an additional comment!. But you can also try http://showcase.omnifaces.org/facesviews/ExtensionlessURLs – Kukeltje May 28 '17 at 07:14
  • That's similar, but still not really what I'm asking about. I want to make parts of the URL into receivable parameters. – KG6ZVP May 28 '17 at 07:43
  • ok, good luck... if PrettyFaces and/or the omnifaces extensionless urls cannot help with what you need, your question is unclear to mee. Since _"I want to make parts of the URL into receivable parameters."_ is 100% what they both do. So either you did not fully read their documentation or I 100% misunderstood your question. Hope someone else can. Cheers – Kukeltje May 28 '17 at 07:53
  • Oh, PrettyFaces is a library for this? Would you make an answer for this so the solution is clear? – KG6ZVP May 28 '17 at 07:56
  • @KG6ZVP, PrettyFaces is a library that has as the main purpose a way to "beautify" JSF URLs. By default JSF doesn't allow these kind of URLs unless you do some dark magic on server side. However, I think you should try the new JSF 2.3 (released some months ago, may be a bit hard to find good content on the web). One of the most cool features of JSF 2.3 is the built-in URL beautifier. Yes, no more need of aditional libraries (at least it's what they said, I still have not tested it). Anyways, good luck with your research! – Bonifacio May 28 '17 at 22:08

1 Answers1

1

For those Googling for an actual answer to the question, PrettyFaces is one way of doing this. It does URL 'beautification,' which includes making elements of the URL's path into receivable parameters.

I'm going to give an example using JAX-RS, then the equivalent with PrettyFaces:

Jax-RS

@Path("user")
public class UserEndpoint{
    @PersistenceContext
    EntityManager em;

    @GET
    @Path("{userid}")
    public UserEntity getUser(@PathParam("userid")long userId){
        return em.find(UserEntity.class, userId);
    }
}

JSF with PrettyFaces

Good help for it on here: http://www.ocpsoft.org/prettyfaces/

Backing Bean

@Named
public class UserViewController{
    @PersistenceContext
    EntityManager em;

    public UserEntity getUser(){
        ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
        Map<String, String> params = ec.getRequestParameterMap();
        long userId = Long.valueOf(params.get("userid"));
        return em.find(UserEntity.class, userId);
    }
}

pretty-config.xml (special file that must be created inside the WEB-INF folder)

<pretty-config xmlns="http://ocpsoft.org/schema/rewrite-config-prettyfaces" 
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            xsi:schemaLocation="http://ocpsoft.org/schema/rewrite-config-prettyfaces
                                http://ocpsoft.org/xml/ns/prettyfaces/rewrite-config-prettyfaces.xsd">

<url-mapping id="view-user">
        <pattern value="/user/#{userid}" />
        <view-id value="/user/view.xhtml" />
    </url-mapping>
</pretty-config>
KG6ZVP
  • 3,610
  • 4
  • 26
  • 45