【问题标题】:jsf: get id from rest url or rewrite urljsf:从rest url获取id或重写url
【发布时间】:2017-02-02 16:47:12
【问题描述】:

目前我将 url 重写如下

.addRule(Join.path("/users").to("/views/user/index.jsf"))

但现在我需要重写包含数字(用户 ID)的 url。例如,如果我的url为/users/1/users/200等,我需要匹配它们才能查看/views/user/profile.jsf

同样,我也需要将url/users/1/edit/users/200/edit改写为/views/user/edit.jsf

我怎样才能做到这一点?

【问题讨论】:

    标签: jsf prettyfaces


    【解决方案1】:

    这就是现在对我有用的方法,但不确定这是否是正确的做事方式。

    首先我将网址改写为.addRule(Join.path("/users/{id}").to("/views/user/show.jsf"))

    我把网址设为

    <h:column>
                    <f:facet name="header">Show</f:facet>
                    <h:outputLink value="/users/#{user.id}">Show</h:outputLink>
                </h:column>
    

    这会将 url 生成为 /users/1/users/200 等。

    现在我可以从 jsf 页面 show.xhtml&lt;h:outputText value="#{param['id']}" /&gt; 访问用户 id 参数

    我也可以从bean中访问参数

    public void show(){
        Map<String, String> params = FacesContext.getCurrentInstance().
                getExternalContext().getRequestParameterMap();
        String user_id = params.get("id");
    }
    

    或者我也可以这样做

    show.xhtml

    <ui:param name="user" value="#{userController.show(param['id'])}" />
    
    Name: #{user.firstName}
    

    userController 中作为

    public User show(Integer id){
        user = userService.findById(id);
        return user;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-15
      • 2017-01-16
      • 2021-07-28
      • 2012-05-10
      • 2012-10-03
      • 1970-01-01
      相关资源
      最近更新 更多