如何将URL映射到资源而不是JSF页面?

时间:2017-05-27 20:08:02

标签: jsf url-routing

我已经习惯了Jax-RS,并且可以通过以下自定义网址向调用者提供数据:

/bicycles/by_color/blue

做:

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

JSF中的等价物是什么?我有一个带有ReST API的应用程序,并希望显示一个模板页面,根据xhtml页面的URL的一部分从数据库中获取特定实体以从中获取数据。

我见过查询参数的示例,例如'?id = 5&amp; pages = true',但没有像'/ 5 / true'这样的网址映射。我该如何做到这一点?

1 个答案:

答案 0 :(得分:1)

对于那些谷歌搜索问题的实际答案,PrettyFaces是这样做的一种方式。它确实有URL美化,&#39;其中包括将URL路径的元素转换为可接收的参数。

我将使用JAX-RS给出一个例子,然后用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

在此处提供了很好的帮助:http://www.ocpsoft.org/prettyfaces/

支持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 (必须在WEB-INF文件夹中创建的特殊文件)

<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>