0

the situation is as follows, i have a page to add/update user in the prerender method depending on a parameter either creating a new object or getting existing one

@Component("user")
@Scope("request")
public class UserBean {


    private User userObj;
    private boolean editUser;


    public String addUser() throws Exception {

        if (editUser) {
            userService.updateUser(userObj);
        } else {
            userService.addUser(userObj);
        }
        return "users?faces-redirect=true";
    }

    public void preRender(ComponentSystemEvent event) throws Exception {

            System.out.println("############ PRERENDER #############");
            if (editUser) {
                userObj = userService.getUser(userID);
                pageTitle = "Updating " + userObj.getName();
                buttonTitle = "Save Changes";
            } else {
                userObj = new User();
                pageTitle = "Adding new user";
                buttonTitle = "Add User";

            }

        }

and in the jsf page i call the prerender as:

<f:event id="event1" listener="#{user.preRender}" type="javax.faces.event.PreRenderComponentEvent" />

but when i press the add button which is as follows:

<h:commandButton value="#{user.buttonTitle}" action="#{user.addUser}" style="width: 105px; "/> 

i am getting above exception, please advise.

Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498
  • I guess your `commandButton` is creating a new request to the server which causes the UserBean creating a new instance. Therefore the `userObj` is null. – flash Oct 18 '11 at 11:55
  • @flash any ideas to solve it ? – Mahmoud Saleh Oct 18 '11 at 12:03
  • @Mika Sure, you should consider changing the scope from request to session. That would be the easiest solution. It that is not feasible for you, there are other solutions as well. – flash Oct 18 '11 at 12:11
  • session is not a good solution yes. – Mahmoud Saleh Oct 18 '11 at 12:26
  • @Mika You should set the scope temporary to session to confirm that this is the problem. – flash Oct 18 '11 at 12:33
  • Those annotations are not from JSF. What bean management framework are you using? Spring? You should mention and tag that as such. Spring is not part of Java EE. In JSF you can just put the bean in the view scope as `@ManagedBean @ViewScoped` (indeed, the session scope is bad). You should basically do the same in Spring (sorry, I have no idea how to do it with Spring, I'm no Spring guy). – BalusC Oct 18 '11 at 13:27
  • @flash the session works yes, but it's not good solution as we agreed. – Mahmoud Saleh Oct 18 '11 at 13:43
  • @Balusc this page is for adding a user, do you think the it should be viewScoped not requestScoped ? – Mahmoud Saleh Oct 18 '11 at 13:45

1 Answers1

1

The problem is that your h:commandButton is creating a new request to the server which causes the UserBean to creating a new instance of itself in scope Request.

There are several solutions I can think of.

1) It seems that you know if your page is in edit mode or not. Then you can get rid of your preRender method and instead get the userObj from database when calling the getter of userObj. Then you can pass to your add method if your page is in edit mode or not. Therefore you have to modify your commandButton: (note: you have to change the value to your current edit mode)

  <h:commandButton value="#{user.buttonTitle}" action="#{user.addUser}" style="width: 105px; ">
      <f:setPropertyActionListener value="true" target="#{user.editUser}" />
  </h:commandButton>

and your userBean to:

@Component("user")
@Scope("request")
public class UserBean {

private User userObj;
private boolean editUser;

public String addUser() throws Exception {
    userObj = getUserObj();
    if (editUser) {
        userService.updateUser(userObj);
    } else {
        userService.addUser(userObj);
    }
    return "users?faces-redirect=true";
}

public void setEditUser(boolean editUser) {
    this.editUser = editUser;
}

public User getUserObj() {
    if (editUser) {
        if(userObj == null) {
            userObj = userService.getUser(userID);
        }
        return userObj;
    } 
    else {
        return userObj = new User();
    }
}

public void setUserObj(User userObj) {
    this.userObj = userObj;
}

This should give you a basic idea how it works. The trick is to use the f:setPropertyActionListener.

2) You could use the view scope to solve that issue. The problem is spring doesn't offer this out of the box. The good news is that you can build the view scope on your own. That a look at that blog post.

flash
  • 6,730
  • 7
  • 46
  • 70
  • well, the second solution works very fine, but i noticed the following warning on terminal: `WARNING: Setting non-serializable attribute value into ViewMap: (key: user, value class: com.myapp.beans.UserBean)` so any ideas ? btw do you have any good samples/resources like above for using JSF 2 with spring 3 – Mahmoud Saleh Oct 19 '11 at 08:55
  • @Mika Try to let UserBean implement the Serializable Interface. – flash Oct 19 '11 at 09:18
  • but my UserBean autowire other beans like dao and service (singelteon) which i don't want to serialize too, got me ? – Mahmoud Saleh Oct 19 '11 at 09:54
  • @Mika You don't have to worry too much about the warning. For a detailed explaination please see http://stackoverflow.com/questions/3372687/jsf-backing-bean-should-be-serializable – flash Oct 19 '11 at 12:28