Java EE Web 应用程序的简单 MVC?

发布于 2024-07-18 07:08:16 字数 406 浏览 8 评论 0原文

Web 开发的 MVC 方法的目标之一是分离模型、视图和控制器。

假设在类似 Java EE Struts 的环境中,您将 Struts Action 与控制器关联、Java bean 与模型关联、JSP 和/或 ActionForm 与视图关联。

我的问题是,您倾向于在哪里执行逻辑来填充 JSP 上的数据。 将任何类型的逻辑或处理放入视图 ActionForm 中似乎都是不好的做法,并且将任何类型的逻辑放入 JSP 中也是非常糟糕的做法。

您是否包含一个“管理器”类来在向用户显示之前操作视图表单中的数据。

这是一个简单的例子。 您从数据库中取出名字、姓氏,然后需要调整格式(将所有大写字符串转换为混合大小写)。 在该字符串到达​​ JSP 视图之前对其进行操作的逻辑是否会将该逻辑添加到管理器类中?

One of the goals of the MVC approach to web development is to separate out the model, view, controller.

Lets say in a Java EE Struts like environment, you associate the Struts Action with the controller, a Java bean with the model, and JSP and/or ActionForm with the View.

My question, where do you tend to perform any logic to populate data on the JSP. It seems bad practice to put any kind of logic or processing in the view ActionForm and very bad to put any kind of logic in the JSP.

Do you include a "Manager" class to manipulate the data in the View Form before it is display ed to the user.

Here is one simple example. You take First Name, Last Name out of the database and then you need to pretty the format (convert all upper case string to Mixed case). Would the logic to manipulate that string before it gets to the JSP view, add that logic in a manager class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小忆控 2024-07-25 07:08:16

如果逻辑只与视图有关(在您的示例中,使数据看起来“漂亮”),那么我会将其放在视图中。 但是,如果您稍后要保存修改后的数据,那么它应该位于业务层中。 不过,总的来说,我倾向于在视图和控件(您的 Manager 类)之间保留一个层,该层只关心显示问题和输入的高级验证,而不关心复杂的业务规则(确保非常糟糕的数据永远不会到达业务层)。

If it's logic that is only concerned with the view (in your example, making the data look "pretty"), then I would put it in the View. If, however, you would be later saving the modified data, then it should be in the business layer. In general, though, I tend to keep a layer between the View and the Control (your Manager class) that is only concerned with display issues and high level validation of input, but not complex business rules (ensure that very bad data never reaches the business tier).

枯寂 2024-07-25 07:08:16

这很大程度上取决于您使用的框架。 我认为这种事情应该在视图中完成,但具体如何做会有所不同。

如果您使用的是 JSP,那么编写一个自定义标记库来执行您想要的操作可能是有意义的 - 因此它与实际的 HTML 分开,但仍在“视图”内。

所以,你会得到类似的东西:

<td><mytag:mixedCase value="${user.firstname}" /></td>

等等。

This very much depends on what framework you're using. I think this kind of thing should be done in the view, but exactly how you would go about that will vary.

If you're using a JSP it might make sense to write a custom tag library to do what you want - so it's separate from the actual HTML but still within the 'view'.

So, you would have something like:

<td><mytag:mixedCase value="${user.firstname}" /></td>

Etc.

踏雪无痕 2024-07-25 07:08:16

对于简单的表示问题(例如将字符串大写),我会将其放在 JSP 中。 对于更复杂的业务逻辑,我会将其放在模型类中。 您不想让业务逻辑弄乱您的控制器。 将其放入模型中的另一个优点是可以对其进行单元测试。

For simple presentation concerns such as uppercasing a string, I would put it in the JSP. For more complex business logic, I would put it in the model class. You don't want to be cluttering your controllers with business logic. Another advantage of putting it in the model is that it can be unit tested.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文