View parameter when navigating to another page

前端 未结 2 1749
独厮守ぢ
独厮守ぢ 2020-12-22 09:49

I am using JSF2, and I need to be able to pass a parameter from one JSF page to another via a commandLink.

I am on page funding.xhtml (ViewScoped) and h

相关标签:
2条回答
  • 2020-12-22 10:15

    I don't know the specifics of the methods you tried to achieve your goal and hence we cant tell what was wrong with them, but if we consider your code 'as is' you don't have anything that will pass the string you want.

    Not to repeat ourselves, there are plenty of answers here dedicated to using this or that method, so I will give you the best references, in my opinion, of course.

    1. How can I pass a parameter to a commandlink inside a datatable;
    2. ViewParam vs @ManagedProperty;
    3. What can <f:metadata> and <f:viewParam> be used for.

    Regarding the usage of back buttons in JSF you could also take a look at my own answer on How to get back to the same page in JSF.

    By the way, using POST for page-to-page navigation is considered to be a bad practice. If all you need is to navigate to another page you'd better use plain <h:link> or <h:button> instead.

    0 讨论(0)
  • 2020-12-22 10:29

    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.

    0 讨论(0)
提交回复
热议问题