我尝试使用Swing创建一个MVC应用程序。我对实施以及应该如何应对感到困惑。我的意思是:
我有Gui,它是所有逻辑发送到一个名为controller的类的视图,我有一个模型,我有模型属性。(我读过MVC就是这样)
我在视图中创建了一些随机代码,输入了我想要的代码数量,并将其与ActionListener
一起传输到类命名控制器。使用方法中控制器类上的按钮生成的随机代码。
生成随机代码,然后我想将它们保存在数据库中。
我很困惑如何在数据库上保存生成的代码。
我应该在类命名控制器中创建一个方法,以便我可以从那里保存它们?或另一个与保存更新的不同类查找..........方法?如果是,那么为什么我用Model属性创建Model类?我怎样才能使用Model类。
如果我必须使用它,或者我只需要拥有这个类就可以了解如何使用Model类。 Model类的用途是什么,只有它与属性一起存在并保存在其他地方?
通常使用什么方法,以便我可以使用MVC模式?我很困惑吗?任何帮助
我忘了告诉我使用Hibernate。
谢谢
PS。我也读过这个http://java.sun.com/products/jfc/tsc/articles/architecture/,但我不明白。
public class code(){// this is the Model
private int i;
public void setter(int i){
this.i=i;
}
public int getter(){
return i;
}
public String generateStringInt() {
return new BigInteger(190, random).toString(32);
}
// what ever i want to create with the int i variable i will do it on this class?
///then i will pass it on the controller to sent it on the view
//OR save if i want to save it.?Is this way of thinking right?
//Or is there any other way to do it ?
/// if i have a Button and press it from the View it will generate this method?or
// i have to do it else?
public String generateStringInt() {
return new BigInteger(190, random).toString(32);
}
}
//then if i want to save i can just do it like
//session.save(object) or is there any other way?
现在好些吗? 谢谢
答案 0 :(得分:5)
让我为你打破这个......
Model
- 业务逻辑和数据
View
- 显示模型的输出
Controller
- 行动已完成。
Swing
基于MVC
。它也被称为PLAF
(可插入的外观和感觉)
使用此MVC架构的优势在于,您可以保持相同的模型并不断更改视图。
<强>例如强>
拥有一个运行计算器程序的模型。
现在使用此模型,然后使用Swing或JSP来反映输出,分别用于desktop
另一个用于web
。
对于Swing应用程序,MVC的顺序就是这样...... 。
Action is done on the Controller
Controller tells the Model about it
Model make necessary changes, according to the Action
Controller informs the change in state of Model to the View
View will update itself.
对于Web应用程序,MVC的顺序是这样的......
Action is done on the Controller
Controller tells the Model about it
Model make necessary changes, according to the Action
Now Controller informs View and Also make the Changes reflect in View
答案 1 :(得分:1)
嗨,我有同样的问题,这里是一个非常简单易用的教程,并了解所有关于
的内容http://www.leepoint.net/notes-java/GUI/structure/ui-model-communication.html
然后这个
http://www.leepoint.net/notes-java/GUI/structure/30presentation-model.html这是我认为最好的方法。