1

Is it possible in Mojarra-2.1. If I do dispatching to another view from the action method by clicking action buttons:

<h:commandButton value="dispatch" action="#{myBean.action()}" />

The bean:

@ManagedBean
@RequestScoped
public class MyBean{

    public String action(){
        //do some
        return "view";
    }
}

my browser then recieve the content of the view view. The view view, in turn contains the following:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">

      <f:metadata>
            <f:event listener="#{viewBean.doDoubleDispatch()}" type="preRenderView" />
      </f:metadata>
</html>

where

@ManagedBean
@RequestScoped
public class ViewBean{
    public String doDoubleDispatch(){
        //do
        return "finalView";
}

After invoking the doDoubleDispatch method I didn't see the content of finalView. But If click an actionButton which invokes the method it will work perfectly fine.

So, such things are not possible in Mojarra-2.1, aren't they?

user3663882
  • 6,957
  • 10
  • 51
  • 92

1 Answers1

2

The <f:event listener> does not support navigation case outcomes.

Only the JSF 2.2 <f:viewAction action> does. Perhaps you're confusing with it.

You need to make it a void method and navigate/redirect manually.

public void doDoubleDispatch() {
    // ...

    FacesContext context = FacesContext.getCurrentInstance();
    NavigationHandler navigationHandler = context.getApplication().getNavigationHandler();
    navigationHandler.handleNavigation(context, null, "finalView");
}

That said, the <f:event> doesn't necessarily need to end up in <f:metadata>. That's only for self-documentary purposes done when used together with <f:viewParam>. It was during the JSF 2.0/2.1 era actually abused to be able to invoke a bean method after all view parameters are been set. JSF 2.2 therefore introduced the <f:viewAction> to fill this gap, which in turn indeed requires being placed in <f:metadata>.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Just to make sure, is something like `navigationHandler.handleNavigation(context, null, "finalView?faces-redirect=true");` going to be possible? I mean to perform redirecting instead of dispatching... – user3663882 Jun 16 '15 at 10:53
  • Yes. Alternative is `ExternalContext#redirect()` if you don't care about configurable (implicit) navigation cases. – BalusC Jun 16 '15 at 11:07
  • I just have noticed the the url in the browser's line get changed when I perform such dispatching. Is it possible to stay address the same? – user3663882 Jun 16 '15 at 13:50
  • That will only happen when you're redirecting, not when you're dispatching. Apparently you've chosen to send a redirect. Perhaps food for read as it surprises me that you're wondering about this even though you explicitly used the proper terminology in the 1st comment: http://stackoverflow.com/questions/15521451/how-to-navigate-in-jsf-how-to-make-url-reflect-current-page-and-not-previous-o and http://stackoverflow.com/questions/11277366/what-is-the-difference-between-redirect-and-navigation-forward-and-when-to-use-w/ – BalusC Jun 16 '15 at 13:51