MVC 与 Taglibs,意见/替代方案?

发布于 2024-10-16 01:23:55 字数 1705 浏览 3 评论 0原文

我是一名前端开发人员,我发现自己经常处于 jsp 视图层中,并且看到了很多将数据(模型)推送到视图中的解决方案。最近我遇到了一个 taglib 解决方案,它将数据拉入 jsp 中,对我来说这似乎更自然、更明智。

首先是问题

给定一个页面并将其视为一个单独的实体,MVC 绝对有意义,但是单个页面可能非常复杂,并且很可能重用其他页面上使用的组件/服务。结果,控制器也变得相当复杂。

根据我的经验,页面也是可变的,因为客户喜欢在下一次“重新设计”时改变功能或将整个网站翻过来。这通常会导致相当乏味的重构项目,实际上所有内容都需要重写。

然后是一致性问题,在一个页面上,数据集作为“列表”放入 ModelView 中,而在“列表”可能抽象的另一页面上,它作为“specificList”放入 ModelView 中。在项目生命周期中保持一致性成为一项乏味的日常任务,通常可以避免,但这正是纯 MVC 解决方案所发生的情况。

解决方案?

因此,在我最近继承的一个项目中,我看到了两种将数据拉入 pageView 的解决方案。第一个是相当难看的,使用 jsp:include 调用 jsp 页面并触发另一个控制器。

我发现第二个相当优雅,他们使用标签库将特定数据集拉入页面视图。 taglib 记录在 TLD 内,使用起来很愉快。突然之间,我可以在多个页面上重复使用数据,而无需弄乱控制器。

因此,在这个项目中,我必须实施“重新设计”,所有数据拉取解决方案使我的工作变得更加轻松,但是在他们使用数据注入(MVC)的地方,这是一个痛苦的事情(我不是一个java 开发人员)和 java 开发人员提供帮助的地方很少甚至没有。

此外,正确编写的标记库只能写入一次,而使用数据注入 (MVC) 可能会成为您需要经常照顾的孩子(在 jsp 之上)。

Taglib 示例

假设我们有具有以下标签定义/实现的 services.tld。
- 获取员工地址
- getEmployees

<services:getEmployees filter="a">
   <!-- loop, get addresses, otherwise if empty list, render nothing -->
</services:getEmployees>

这允许我(前端)在几乎任何页面上显示员工及其地址,从而使 Java 开发人员能够腾出时间来执行更重要的任务。该服务可以与 pageView 控制器分开进行测试,pageView 控制器变得不那么复杂(例如仅处理身份验证和站点范围的功能),并且生活(至少对我来说)似乎更有趣。

我的问题

实际上是多部分的:

1.)我的推理是垃圾吗?如果是,为什么以及从什么角度? =P

2.) 有更好的选择吗? (例如,我也使用过瓷砖)。

3.) 您是否使用上述 taglib 解决方案?您对此的体验如何?

4.) 从 java 开发人员的角度来看,上述 taglib 解决方案的成本/收益/风险是什么?

我理解为什么 MVC 使 Java 开发人员变得简单,但根据我的经验(到目前为止),它只是将困难转移到 jsp 层,就像我有时需要为每个页面学习单独的 API...哦,作为一名前端开发人员,我确实承认使用 Ajax 和所有这些杂乱的数据提取对我来说更加自然,在页面加载时拥有所有可用数据在我的领域是一种反模式......

I'm a frontend developer and I find myself in the jsp view layer a lot and seen quite a few solutions to push data (model) into a view. Recently I've come across a taglib solution that pulls data into the jsp, which to me seems more natural and sensible.

First the problem

Given a single page and seeing it as a seperate entity, MVC makes absolute sense, however a single page can be quite complex and most likely reuses components/services that are used on other pages. As a result the controller becomes rather complex as well.

In my experience a page is mutable as well since clients love to shift functionality around or turn the whole site inside out at the next "redesign". This usually leads to a rather tedious refactoring project where literally EVERYTHING needs to be rewritten.

Then there's the issue of consistency, on one page a dataset is put into the ModelView as "list" and on another page where "list" might be to abstract, it's put into the ModelView as "specificList". Maintaining consistency during a project lifecycle becomes a tedious mundane task and is usually avoided, but this is exactly what happens with a pure MVC solution.

Solution?

So in a recent project I inheritted, I've seen two solutions that pull data into a pageView. The first is rather ugly using a jsp:include to call a jsp page and fire off another controller.

The second I found rather elegant, they used a taglib to pull specific datasets into a pageView. The taglib was documented inside the TLD and it was a joy to use. Suddenly I could reuse data on a multitude of pages without messing about with controllers.

So in this project I had to implement a "redesign" and all the data pulling solutions made my job a lot easier, however at the points where they used data injection (MVC) it was a pain in the arse (I'm not a java developer) and java developers to help out where few to none.

Also taglibs when written properly can be write-once only whereas using data injection (MVC) can become a child you constantly need to care for (on top of the jsp's).

Taglib example

Let's say we have services.tld with the following tag definitions/implementations.
- getEmployeeAddress
- getEmployees

<services:getEmployees filter="a">
   <!-- loop, get addresses, otherwise if empty list, render nothing -->
</services:getEmployees>

This allows me (the frontender) to display employees and their addresses on virtually any page, frees up a java dev for more important tasks. The service is testable seperate from the pageView controller, the pageView controller becomes a lot less complex (say for example only handle authentication and site wide functionality) and life (to me at least) seems a lot more fun.

My question

Multipart actually:

1.) Is my reasoning crap and if so, why and from what perspective? =P

2.) Are there better alternatives? (I've also used tiles for example).

3.) Are you using the above mentioned taglib solution and how is your experience with that?

4.) What's the cost/benefit/risk of above taglib solution from a java dev's view?

I understand why MVC makes it simple for Java devs, but in my experience (sofar) it just moves the hardship to the jsp layer, it's like I sometimes need to learn a seperate API for each page... Oh and being a frontend developer, I do admit data pulling comes more natural to me using Ajax and all that shizzle, having all the data available at page load is an anti-pattern in my area...

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

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

发布评论

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

评论(1

痴情 2024-10-23 01:23:55

您显然已经听过这样的口头禅:在 JSP 中实现业务逻辑是不好的。但你明白为什么吗?

原因之一是技术问题,即 HTTP 响应何时提交的问题。

在基于 MVC 的系统中,控制器检查参数、决定需要做什么并尝试去做。然后在此基础上设置响应状态代码并提取要在响应中显示的数据。最后,它选择一个视图(例如 JSP)并将控制权交给 JSP 引擎...该引擎提交 HTTP 响应。

如果您尝试在 JSP 中完成所有操作,则会遇到 HTTP 响应可能提交得太早的问题;即在您的业务逻辑完成确定正确的 HTTP 状态代码是什么之前。


您对 MVC 的不满似乎很大程度上源自您的假设:控制器和视图(JSP)是由不同的人实现的:即“前端开发人员”和“Java 开发人员”,他们拥有不重叠的技能和职责范围。我的看法是,你应该只拥有具备在“栅栏”两边工作的技能的“开发人员”。事实上,根本不应该有栅栏。

You've clearly heard the mantra that it is the bad to implement business logic in JSPs. But do you understand why?

One of the reasons is the technical problem the problem of when the HTTP response is committed.

In an MVC based system, the controller checks parameters, decides what needs to be done and tries to do it. Then on the basis of that it sets a response status code and pulls data to be displayed in the response. Finally, it selects a view (e.g. a JSP) and hands control to the JSP engine ... which commits the HTTP response.

If you try to do everything in a JSP, you have the problem that the response is HTTP likely to be committed too soon; i.e before your business logic has done the things that will determine what the correct HTTP status code is.


Your dissatisfaction with MVC seems to be largely derived from your assumption that the controller and the view (the JSP) are implemented by different people: i.e. "front end developers" and "java developers" who have non-overlapping skillsets and areas of responsibility. My take is that you should just have "developers" who have the skills to work on both sides of "the fence". In fact, there shouldn't be a fence at all.

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