
[英]spring mvc - How to retrieve values from controller without creating a view
[英]MVC creating model and controller from view
我正在使用MVC模式实现应用程序,在某些情况下,我已经从视图创建了模型和控制器,并在视图内部调用了控制器的适当方法。 这是一个设计问题吗?如果可以的话,那将是解决方案,我无法在主面板中创建所有模型,因为其中一些是通过单击GUI来调用的。
这是一个示例代码:
public class MyView extends JInternalFrame{
private JComboBox<String> myList;
private JButton button;
public MyView(){
super("MyView", true, // resizable
false, // closable
false, // maximizable
true);// iconifiable
setSize(250, 150);
setLocation(300,300);
myList= new JComboBox<String>();
button= new JButton("Click");
button.addActionListener(new Listener());
JLabel label = new JLabel("Example");
Border lineBorder = BorderFactory.createTitledBorder("Example UI");
JPanel panel= new JPanel();
panel.setBorder(lineBorder);
panel.setLayout(new GridLayout(3, 1));
panel.add(label);
panel.add(list);
panel.add(button);
add(panel);
}
class Listener implements ActionListener{
@Override
public void actionPerformed(ActionEvent actionEvent) {
String button = actionEvent.getActionCommand();
if(button==button.getText()){
Model model = new Model ();
Controller controller = new Controller (model, MyView.this);
controller.doSomething();
}
}
}
经过一周的研究,我得出的结论是我的工作方式很好。 用户与与控制器交互的视图交互。 没有约定在何处以及如何创建模型和控制器。 这完全取决于所使用的特定问题模式。 只要应用了MVC的主要原理,您就可以自由。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.