user3808021
user3808021

Reputation:

Render f:viewAction in some cases

I have a home page:

<f:metadata>
    <f:viewAction action="#{booksBean.selectBook()}"/>
</f:metadata>

<h:head>
    <title>home</title>
</h:head>
<h:body>
 ...
<h:link >
    <h:graphicImage name="images/books/s1.jpg" />
    <f:param name="isbn" value="25413652" />
</h:link>
...
</h:body>

When user clicks on link, the isbn value has been sent to booksBean.selectBook() correctly.

But problem is when user navigates from login page to home page,

Here is userBean.login() :

public String login() {
    if (loginSuccessfully) {
        return "home?faces-redirect=true"; // problem
    } else {
          //show error message
    }
}

Problem is in mentioned section, when user moves to home.xhtml the booksBean.selectBook() is called automatically, and since it is null yet, i got NullPointerException.

How can i go to home page from login page without invoking the booksBean.selectBook() ?

Upvotes: 1

Views: 636

Answers (2)

kolossus
kolossus

Reputation: 20691

You can selectively invoke a f:viewAction by specifying the if condition. In your case, it appears you want the action executed on postback only, in which case, you can have:

  <f:viewAction action="#{booksBean.selectBook()}" if="#{facesContext.postBack}"/>

Related:

Upvotes: 3

Sajad
Sajad

Reputation: 2363

Here is the solution by not using of f:viewaction

<h:commandButton value="optional value" action="#{booksBean.selectBook()}">
    <f:param name="isbn" value="25413652"></f:param>
    <f:ajax execute="@form" render="@form"></f:ajax>
</h:commandButton>

Upvotes: 0

Related Questions