0

I am working with JSF 2.2 and Tomcat 8 and I am just starting to play with them.

I have a command button in a jsf page.

<h:commandButton id="newObject" value="New Object" action="#{someObject.someAction}">
<f:param name="object_id" value="#{someObject.object_id}" />
</h:commandButton>

The ManagedBean is similar to this:

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
@ManagedBean
public class SomeObject implements Serializable{
    private static final long serialVersionUID = 1L;
    private int object_id;
    public int getObject_id() {
        return object_id;
    }
    public void setObject_id(int object_id) {
        this.object_id = object_id;
    }
    public String someAction() {
        setObject_id(sqlInsert());
        if(getObject_id() != 0) {
            System.out.println(getObject_id());
            return "new_page";
        }
    }
}

The sqlInsert method is working fine. I use it to insert a new row in some sql table and get the auto generated key, which is an int. If the insert did not happen it would return 0.

I can navigate to the new_page, but the param object_id is 0. I added println to show the object_id and it is the actual key.

What am I doing wrong?

jaec86
  • 45
  • 9
  • Apart from the fact that you're misunderstanding some fundamental concepts about communication in JSF, why are you passing the bean's `objectId` back to itself? At what point in the code you're showing here, are you actually setting the value of `objectId` to something other than 0? – kolossus Apr 07 '15 at 22:51
  • In the jsf page i'm not setting the objectId because it doesn't exist in the db yet. The sqlInsert method is creating the object, that is why i'm setting the objectID there. And is returning the right seed of the db, but in the new page im getting 0 as objectID. – jaec86 Apr 07 '15 at 23:14
  • My guess is that there are a few things wrong, @jaec86. One of them is that you haven't declared a scope on your ManagedBean, and the default is RequestScoped, which means that it is recreated on _every_ HTTP request. Rather than start by posting questions on StackOverflow, I recommend that you start with [Bauke Scholtz's JSF link index](https://jsf.zeef.com/bauke.scholtz). – DavidS Apr 07 '15 at 23:31
  • You should post the part of your code, in the destination page, where you're trying to retrieve the request parameter. *How* and *where* you're trying to retrieve the parameter are important – kolossus Apr 08 '15 at 01:13
  • You presented pseudo code where it is difficult to answer reasonably without making some wild guesses. This statement, "*I can navigate to the `new_page`, but the param `object_id` is 0. I added `println()` to show the `object_id` and it is the actual key.*" contradicts severely with the present managed bean code snippet which is difficult to guess. It attempts to imply that `` supplies zero but in the managed bean you somehow get a non-zero key value. It is likely that `` is not set properly to `object_id` in the managed bean for some uncertain reasons. – Tiny Apr 08 '15 at 05:45

1 Answers1

1

Since you are using the only @ManagedBean annotation on your Managed Bean and not specifying any Scope of you bean explicitly, your Bean will act as if its a @RequestScoped bean.[See link]

So every time you click your New Object button, the Bean is re initialized and you will loose the state(variable values).

Think and decide which scope you want to use [See link]. For your requirements @ViewScoped might do the job for you.

Community
  • 1
  • 1
Kishor Prakash
  • 8,011
  • 12
  • 61
  • 92