Question

I'm struggling with how the getter/setter methods are called in a Java Bean. I don't see examples of the tutorials (https://netbeans.org/kb/docs/web/jsf20-intro.html) calling the setter methods and yet the values are obviously set.

As a result, I'm having trouble making sure my setter methods are being called. For instance...

@Named(value = "someJavaBean")
@SessionScoped
public class someJavaBeanSerializable {

    String webServiceResponse;

    public void setWebServiceResponse() {
        this.webServiceResponse = "Just a test";
    }

    public String getWebServiceResponse() {
        this.setWebServiceResponse();
        return this.webServiceResponse;
    }

    public someJavaBean() {
        System.out.println("You are in someJavaBean"); 
    }
}

It appears that I have to manually call setWebSErviceResponse() in order for the setter to run which is typical of other languages. I'm okay doing that, but based on what I've read, I'm not sure that is the correct way to do it.

Any help or clarification would be appreciated.

Thank you,

Was it helpful?

Solution 2

Your setter method needs a parameter:

public void setWebServiceResponse(String webServiceResponse) {
    this.webServiceResponse = webServiceResponse;
}

OTHER TIPS

Java beans define behavior by convention:

  • A class has a default public constructor with no arguments to it (or, essentially, no constructor).
  • Class properties are accessed using the convention of getters and setters - namely getWebServiceResponse() and setWebServiceResponse(String response).
  • The only methods present in the bean are the methods to interact with the fields - that is, a setter and getter for each field. If you have a final field, then this would change (you would only be able to have a getter).

Not having a parameter in your setter violates this convention.

Also, it's bad style to call your setter inside of your getter - the idea behind a setter is that it only sets a field, and a getter only returns whatever value is inside of that field - even if it's null. I'd remove the call to your setter as well.

Lastly, public someJavaBean() is invalid - you may have meant to return void.

Sorry to be dense. In the following code from the tutorial (https://netbeans.org/kb/docs/web/jsf20-intro.html), where is the setter called?

@ManagedBean (name="UserNumberBean")
@SessionScoped
public class UserNumberBean implements Serializable{

    Integer randomInt;
    Integer userNumber;
    String response;

    public String getResponse() {
        if ((userNumber != null) && (userNumber.compareTo(randomInt) == 0)) {

        //invalidate user session
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
        session.invalidate();

        return "Yay! You got it!";
    } else {

        return "<p>Sorry, " + userNumber + " isn't it.</p>"
                + "<p>Guess again...</p>";
    }
    }

    public Integer getUserNumber() {
        return userNumber;
    }

    public void setUserNumber(Integer userNumber) {
        this.userNumber = userNumber;
    }
    /**
     * Creates a new instance of UserNumberBean
     */
    public UserNumberBean() {
        Random randomGR = new Random();
        randomInt = new Integer(randomGR.nextInt(10));
        System.out.println("Duke's number : " + randomInt); 

    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top