0

我正在使用 JSF2,我需要能够通过 commandLink 将参数从一个 JSF 页面传递到另一个页面。

我在页面上funding.xhtml(ViewScoped)并定义了以下链接:

<p:commandLink styleClass="toolbar" 
               action="/application/customerApplicationManagement.jsf">
    <p:graphicImage url="/resources/gfx/search.png" />
    <h:outputText value="#{msg.menu_searchApplications}" styleClass="toolbarLink" />
</p:commandLink>

我需要将一个字符串值传递给 customerApplicationManagement 页面,指示我来自哪个页面,以便在选择应用程序后,我可以返回该页面。我已经尝试了几个关于如何传递这个值的建议,包括f:param, f:viewParam。我什至尝试将其直接添加到 url ( ?fromPage=funding) 等,但它们似乎只有在将值传递回当前页面时才有效,而不是我正在导航到的新页面。

有人可以告诉我如何最好地实现这一点。

4

2 回答 2

2

Use <f:param> and <f:viewParam>:

Source page:

<p:commandLink styleClass="toolbar" 
           action="/application/customerApplicationManagement.jsf">
    <p:graphicImage url="/resources/gfx/search.png" />
    <h:outputText value="#{msg.menu_searchApplications}" styleClass="toolbarLink" />
    <f:param name="fromPage" value="funding.xhtml" />
</p:commandLink>

Destination page (bound):

<f:metadata>
    <f:viewParam name="fromPage" value="#{destinationBacking.fromPage}" />
</f:metadata />

<h:link value="Go back!" outcome="#{destinationBacking.fromPage}" />

Destination page (unbound):

<f:metadata>
    <f:viewParam name="fromPage" />
</f:metadata />

<h:link value="Go back!" outcome="fromPage" />

Backing bean (only if you want to bind the param):

@ManagedBean
@ViewScoped
public class DestinationBacking{
    String fromPage;

    public String getFromPage(){
        return fromPage;
    }

    public void setFromPage(String frompage){
        fromPage = frompage;
    }
}

Your view path will be binded to fromPage property from the destination backing bean and after you can use it to return to the original page.

Also I want to say that this way is a bit 'hackeable' by the end user, I mean, you're passing the original path through pure url. See also other ways to achieve that, as flash scope, which is very useful specially if you're working with @ViewScoped beans.

于 2013-03-15T17:50:00.240 回答
0

我不知道您尝试实现目标的方法的细节,因此我们无法判断它们出了什么问题,但是如果我们认为您的代码“原样”,您将没有任何可以传递您想要的字符串的东西.

不要重复我们自己,这里有很多专门用于使用这种或那种方法的答案,所以在我看来,当然,我会给你最好的参考。

  1. 如何将参数传递给数据表内的命令链接
  2. ViewParam 与 @ManagedProperty
  3. 可以<f:metadata><f:viewParam>用于什么。

关于 JSF 中后退按钮的使用,您还可以查看我自己关于How to get back to the same page in JSF的回答。

顺便说一句,使用 POST 进行页面到页面导航被认为是一种不好的做法。如果您只需要导航到另一个页面,您最好使用plain<h:link><h:button>代替。

于 2013-03-15T17:27:46.907 回答