如何将参数传递给Pretty-Faces中的view-id?

时间:2013-01-14 12:27:57

标签: jsf-2 prettyfaces

我想将一些变量值传递给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

有人知道怎么做吗?

谢谢。

2 个答案:

答案 0 :(得分:3)

正如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。如果您输入此网址/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。

答案 1 :(得分:0)

我是这样创造的:

漂亮-config.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;
    }

}