Java MVC 模式中的变量声明约定/规则?
我是Java世界的新手。如何避免 MVC 模式中的变量声明对同一变量造成混淆?
例如,
在 Servlet(控制器)中:
String firstName = request.getParameter("firstname");
String lastName = request.getParameter("lastname");
在 Bean(模型)中:
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;
}
在 DAO(数据访问层)中:
public void savePerson(String firstName, String lastName) {
// Method statements
}
在 JSP(视图)中:
${person.firstname} ${person.lastname}
我的问题/困惑?
在不同的环境中声明相同变量的正确方法是什么
模块(控制器、模型、视图、dao)?我应该如何避免混淆?在不同的环境中声明变量时,我必须遵循什么约定吗? 模块?
Servlet 和 DAO 中的变量应该相同吗?应该在模型/Bean 中使用变量 与 Servlet 和 DAO 不同吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的 servlet 中,这些将是方法变量。那很好。
在您的模型中,这充当模型的属性。那也挺好的。
在视图中,您实际上是指模型的属性,而不是声明任何变量。
在 DAO 中,你实际上是在坚持你的模型。
因此,在 Servlet/Controller 中,这将更像是这样,
而在您的 DAO 中,这将更像是这样,
因此,声明仅发生在
Model
中。我希望它能消除您的困惑。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.我认为您使用的变量名称很好。您可以根据模块的类型命名您的类。因此,您可以使用 MyApplicationController、EmployeeModel、EmployeeDAO 等名称来命名您的类。
I think the variable names you have used are fine. You can name your classes based on the type of the module. So you could name your classes using names like MyApplicationController, EmployeeModel, EmployeeDAO, etc.