Creating Bean (Model)

Thursday, February 25, 2010 Posted by Unknown
In this post we will learn how to create a package in netbeans and how to create a class in that pakage.
Right click on source packages and select package



Enter package name(model in this example) and click next
now create a bean class with in this package. for this select that package and right click and select create and select class. and now enter name of that class.In this example that class name is UserBean. and click finish



then netbeans creates a class with default structure.

package model;

/**
*
* @author Jagadeesh
*/
public class UserBean {

}

now insert setter methods and getter methods in that been.Simple way to create setter methods and getter methods.
1. declare variables
2. select all variables and right click and click on insert code. select setter methods and getter methods and ok



then it will create setter and getter methods.
in this example i declared four variables for form values and five variables for display error messages.


package model;
package model;

/**
* @author Jagadeesh
*/
public class UserBean {

private String userName ;
private String dateOfBirth ;
private String email ;
private String phoneNo ;

//for validation error messages

private String userNameError;
private String dateOfBirthError;
private String emailError;
private String phoneNoError;
private boolean isValid;

//this is for action means edit or submit or delete or update
private String action;
//construtor.it is invoked when we run jsp page.
//so first time no values will display in jsp page
public UserBean()
{
userName ="";
dateOfBirth ="";
email="";
phoneNo="";

userNameError="";
dateOfBirthError="";
emailError="";
phoneNoError="";

action = "submit";
}

public String getDateOfBirth() {
return dateOfBirth;
}

public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPhoneNo() {
return phoneNo;
}

public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

//setter and getter methods for errors

public String getUserNameError() {
return userNameError;
}

public void setUserNameError(String userNameError) {
this.userNameError = userNameError;
}
public String getDateOfBirthError() {
return dateOfBirthError;
}

public void setDateOfBirthError(String dateOfBirthError) {
this.dateOfBirthError = dateOfBirthError;
}

public String getEmailError() {
return emailError;
}

public void setEmailError(String emailError) {
this.emailError = emailError;
}

public String getPhoneNoError() {
return phoneNoError;
}

public void setPhoneNoError(String phoneNoError) {
this.phoneNoError = phoneNoError;
}

public boolean getIsValid() {
return isValid;
}
public void setIsValid(boolean isValid) {
this.isValid = isValid;
}

public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}

}


in next post we will create a class for validate form
Labels:

Post a Comment