1

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() ?

  • By not using a viewAction, perhaps? Why do you think you need a viewAction here? – meriton Apr 19 '15 at 16:23
  • @meriton I need it in home `h:link` , how can i send a `GET` parameter in through a link to a bean method without a `viewAction` ? –  Apr 19 '15 at 16:40

2 Answers2

3

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:

Community
  • 1
  • 1
kolossus
  • 20,559
  • 3
  • 52
  • 104
  • 1
    This exactly fixed a `NPE` which I got before when a view with a "required" (no attribute set, breaks 2nd AJAX request) parameter was not set. E.g. `/faces/admin/admin_user_edit.xhtml?userId=x` versus `/faces/admin/admin_user_edit.xhtml`, the 2nd one was triggering that NPE as the `f:viewAction` was triggered but the required property in backing bean was not set (because no parameter specific). Hope this helps some more people. But now my edit form is empty. :-( – Roland Sep 23 '18 at 21:00
0

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>
Sajad
  • 2,273
  • 11
  • 49
  • 92