I am a novice in Java World. How to avoid confusion over variable declaration in MVC pattern over same variable?
For Example,
In Servlet (Controller):
String firstName = request.getParameter("firstname");
String lastName = request.getParameter("lastname");
In Bean (Model):
private String firstname;
private String lastname;
public Person(String FirstName, String LastName) {
setFirstName(FirstName);
setLastName(LastName);
}
//Getter and Setter Methods
public String getFirstName() {
return firstname;
}
public void setFirstName(String FirstName) {
this.firstname = FirstName;
}
public String getLastName() {
return lastname;
}
public void setLastName(String LastName) {
this.lastname = LastName;
}
In DAO (Data Access Layer):
public void savePerson(String firstName, String lastName) {
// Method statements
}
And in JSP (View):
${person.firstname} ${person.lastname}
My Questions/Confusion?
-
What is the proper way of declaring same variable in different
modules(controller,models,views,dao)? And how should I avoid confusion? -
Is there any conventions I have to follow while declaring variables in different
modules? -
Should variables in Servlets and DAO be same? Should variables in Models/Bean
be different from Servlet and DAO?
In your servlet these would be method variables. That’s pretty fine.
In your Model this works as a Model’s properties. That’s pretty fine, too.
In view you are actually referring to Model’s properties, and not declaring any variable, whatsoever.
In DAO, you are actually, persisting your Model.
So, In Servlet/Controller this will be something more like this,
And in your DAO, it would be more like this,
Hence, declaration only happens in
Model. I hope it clears your confusion.