• Post Reply Bookmark Topic Watch Topic
  • New Topic

java bean's constructor

 
mike zhang
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In MVC model of JSP the java bean class functions as the model. I don't know if I impression is wrong or not, it seems it requires the java bean class have an empty constructor. Is it Okay to have a non-empty constructor for a java bean class ? i.e is the following bean OK ?
***************************************
public class Order {
private float price;
private String id;
private Customer cust;
public Order(Customer c) { //---(1)
c.setName(); //---(2)
c.setAddress(); //---(3)
}
...
}
*************************
And, is it ok to replace lines 1,2,3 by
public Order() {
Customer cust = new Customer();
cust.setName();
cust.setAddress();
}
Besides whether it works, which way is the favored
java bean model's constructor ?
Thanks,
Mike
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a Java Class to function as a JSP Bean, it must have a Default contructor (One which doesn't take arguments. It should also have a setter for each property you want to set with jsp:setProperty and a getter for each property you want to get with jsp:getProperty.
So, your first example will NOT function as a JSP Bean as it doesn't have a default constructor. While the second on will work, customer will not have any value set for name or address. This probably isn't what you are looking for. Nothing states that the properties you set can't be in a wrapped class such as customer. You can have
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!