Java MVC - 视图或控制器应该提示输入吗?
我的程序使用MVC。当用户点击“新文件”时,我需要检查以前打开的文件是否被编辑(如果是,则提示用户保存)。
据我了解,控制器应该单独执行所有验证和逻辑。那么我的控制器是否可以使用 JOptionPane 提示用户输入文件名、保存以前的文件等?或者所有输入都应该在 GUI 中进行吗?
谢谢!
My program uses MVC. When the user hits "New File", I need to check if the previous open file is edited (and prompt the user to save if so).
From what I've understood, the controller should solely perform all validation and logic. Is it then okay for my controller to prompt the user for file name, save previous file etc using JOptionPane? Or should all input be taken in the GUI?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 MVC 模式中,“控制器接收用户输入并通过调用模型对象来发起响应”。
考虑到您的问题的上下文,尤其是您提到的任务:
为了帮助您更好地理解,我将告诉您哪个组件应该执行什么任务。
您的模型应该具有执行 2 和 3 的逻辑。并且您的控制器应该调用(调用方法)您的模型,并根据返回的值调用视图上的其他功能,例如使用 JoptionPane 和其他类似的东西来提示用户文件名等。
总而言之,您的控制器应该只充当主持人,而不能自行执行任何操作。您提到的所有任务都将由模型或视图执行。控制者的工作就是实现它们。
祝你好运。
In the MVC pattern, "The controller receives user input and initiates a response by making calls on model objects".
Considering the context of your problem, especially the tasks which you mentioned:
To help you better understand, I'll tell you what task should be performed by which component.
Your model should have logic for performing 2 and 3. And your controller should invoke (call methods on) your model and depending on the returned values invoke yet other functionality on the view, like a using JoptionPane and other such things for prompting the user for file name etc.
So all in all, your controller should only act as a moderator and do nothing on it's own. All the tasks you mentioned will be performed either by Model or the View. It's the job of your controller to bring them about.
Good luck.
MVC 架构中控制器的角色
控制器接收请求。
控制器根据请求参数决定所请求的活动。
控制器根据请求参数委托要执行的任务。
控制器委托下一个要显示的视图。
经历这个
链接
将为您解决问题。
Role of controller in MVC architecture
Controller receives a request.
Controller decides the requested activities based on request parameters.
Controller delegates tasks to be performed based on the request parameters.
Controller delegates the next view to be shown.
Go through this
link
will give idea to your problem.
对于设计问题,很难给出准确的答案,但在这种情况下,我非常倾向于建议您在您看来实现此功能,原因是您在这里所做的纯 UI 是只是一组获取输入的操作,然后将其传递给控制器。
让我们这样说,你的控制器不应该关心、知道或干预如何在客户端检索信息,它应该只处理特定的请求,控制器应该与 UI 无关,所以如果你获取文件,它的界面不应该改变通过 UI 或命令行或配置命名..
Well is very difficult to come up with an exact answer for design questions, but in this case, I would be very inclined to recommend you to implement this functionality in your view, the reason is that is pure UI what you are doing here, is just a set of operations to obtain an input that then you will pass to your controller.
Let's put it this way, your controller shouldn't care or know or intervene in how information is retrieved in your client, it should only handle specific requests, the controller should be UI agnostic so its interface shouldn't change if you get the file name through the UI or command line, or configuration..