JSF: call backing-bean method without rendering the page (using URL parameters)

后端 未结 1 407
鱼传尺愫
鱼传尺愫 2021-01-07 07:40

Is it possible to use a JSF (PrimeFaces) page in the following fashion ? :

  1. external application (not JSF-based) does an
相关标签:
1条回答
  • 2021-01-07 08:27

    Use <f:viewParam> to set GET parameters as bean properties and use <f:event> to execute a bean action and use NavigationHandler to perform navigation programmatically.

    View:

    <f:metadata>
        <f:viewParam name="foo" value="#{bean.foo}" />
        <f:viewParam name="bar" value="#{bean.bar}" />
        <f:event type="preRenderView" listener="#{bean.action}" />
    </f:metadata>
    

    Bean:

    public void action() {
        // ...
    
        FacesContext context = FacesContext.getCurrentInstance();
        NavigationHandler navigationHandler = context.getApplication().getNavigationHandler();
        navigationHandler.handleNavigation(context, null, "outcome");
    }
    

    Note that, depending on the concrete functional requirement, JSF might not necessarily be the right tool for the job. I'd investigate if you couldn't better use a servlet filter or even a plain servlet for the purpose.

    See also:

    • What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?
    • Hit a bean method and redirect on a GET request
    0 讨论(0)
提交回复
热议问题