【问题标题】:How to pass parameters to view-id in Pretty-Faces?如何在 Pretty-Faces 中将参数传递给 view-id?
【发布时间】:2012-12-28 09:50:27
【问题描述】:

我想将一些变量值传递给 pretty-config.xml 配置文件中的 view-id 节点。示例:

我想做这样的事情:

 <url-mapping id="allReports">
        <pattern value="/report/#{type}" />
        <view-id value="/pages/report/#{type}.xhtml" />
    </url-mapping>

但我得到了错误:

java.lang.IllegalArgumentException: java.net.URISyntaxException: Illegal character in fragment at index 18: http://localhost/#{type}.xhtml

有人知道怎么做吗?

谢谢。

【问题讨论】:

    标签: jsf-2 prettyfaces


    【解决方案1】:

    正如Prettyfaces main website 所说,您必须执行以下操作:

    <url-mapping id="view-user">
        <pattern value="/user/{username}" />
        <view-id value="/user/view.xhtml" />
    </url-mapping>
    

    这相当于/user/view.xhtml?username=yourParam。如果您键入此 URL /user/Administrator,您会在视图中收到一个请求参数,其名称为 username,值为 Administrator。只需遵循此约定即可。

    如果您想继承父 ID,只需 write a mapping for each type。例如你可以写:

    <url-mapping parentId="view-user" id="admins">
       <pattern value="/admin/#{user}" /> 
       <view-id value="/user/admins/view.xhtml" />
    </url-mapping>
    
    <url-mapping parentId="view-user" id="externals">
        <pattern value="/external/#{user}" /> 
        <view-id value="/user/externals/view.xhtml" />
    </url-mapping>
    

    您也有dynamic view id's,但我认为不可能将它们与静态字符串片段连接起来。要以您希望的方式使用它们,您应该从请求中获取参数并在您的 bean 中处理完整的目标 url。

    【讨论】:

    • 但我不想要查询参数样式。路径参数是。有可能吗?
    【解决方案2】:

    我是这样创建的:

    漂亮的配置.xml

    <url-mapping id="static">
        <pattern value="/s/#{staticViewBean.requestPath}" />
        <view-id value="#{staticViewBean.getViewId}" />
    </url-mapping>
    

    StaticViewBean.java

    @Controller("staticViewBean")
    @Scope("request")
    public class StaticViewBean {
    
        private String requestPath;
    
        public String getViewId() {
            return "/WEB-INF/pages/s/" + requestPath + ".xhtml";
        }
    
        public String getRequestPath() {
            return requestPath;
        }
    
        public void setRequestPath(String requestPath) {
            this.requestPath = requestPath;
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2016-04-01
      • 2011-07-13
      • 1970-01-01
      • 2020-03-02
      • 2020-11-23
      • 2018-02-06
      • 1970-01-01
      • 2022-01-01
      • 2015-03-19
      相关资源
      最近更新 更多