何时使用 Tiles 视图准备器
我是 JSP 和 Tiles 以及 Java 的新手。 我们目前正在使用这些重新构建我们的网站平台,但我很困惑什么时候应该将某些内容放入视图准备器中,而不是来自控制器。
例如,我正在处理的当前页面将有一个用于分页的图块,包括内容类型(Y 内容类型的 X)。 我最初的计划是使用视图准备器来获取控制器(HashMap)发送的数据,并为分页图块输出几个属性,但一位同事告诉我这应该在控制器中完成。
如果是这样的话,视图准备器还有什么意义呢? 我只是有点困惑。 我检查了 Tiles 文档,它们非常基本/无法使用。
有人能给我一个视图准备器的正确用例吗?
I'm new to JSP and Tiles, as well as Java. We are currently replatforming our site using these, but I am confused as to when something should be put into a view preparer vs coming from the controller.
For example, the current page I am working on will have a tile for pagination, including the content type (X of Y content-type). My original plan was to use a View Preparer to take the data being sent in by the controller (HashMap) and output a couple attributes for the pagination tile, but a co-worker told me this should be done in the controller instead.
If this is the case, what is the point of the View Preparer? I'm just slightly confused. I checked the Tiles docs and they are pretty basic/unusable.
Can someone give me a proper use case for a View Preparer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
控制器用于执行业务或事务逻辑。 也就是说,为了响应用户操作,应用程序需要执行一个或多个操作,然后决定渲染哪个视图。 这就是为什么它被称为控制器,因为它“控制”应用程序的流程。 当控制器执行其工作时,数据的更改必须对用户可见。 然而,准备用于显示的数据并不是控制器的工作。 只需确保完成必要的操作并且数据可用即可。
视图(在本例中为 jsp 页面)将获取数据并显示它。 据我了解,视图准备器可以帮助您分解视图的某些方面,以便准备器可以完成可能必须在多个不同视图中完成的准备工作。
因此,文档中准备菜单的示例是一个很好的用例。 菜单有时是动态的,因为它取决于系统的状态,确切地向用户显示什么。 假设您希望在用户未登录时在菜单中显示登录链接,并在用户登录时从菜单中删除该链接。而不是将该逻辑编码到必须显示菜单的每个页面中可以使用 ViewPreparer 来生成实现任何必要逻辑的菜单。 然后,该视图准备器可以与多个页面关联。
更多地将其视为操纵数据以供查看,而不是业务逻辑。
The controller is for executing business or transactional logic. That is, in response to a user action the application needs to perform one or more actions and then decide which view to render. Thats why its called a controller because it "controls" the flow of an application. As the controller performs its job the changes to the data will have to become visible to the user. However, preparing that data for display is not the job of the controller. Just making sure that the necessary actions are done and that the data is available.
The view, in this case your jsp pages, will then take the data and display it. As I understand it the View Preparer helps you factor some aspects of the view out so that a preparation that may have to be done in several diffent views may be done by the Preparer.
So the example in the docs of preparing a menu is a good use case. Menus are sometimes dynamic, in the sense that it depends on the state of the system what exactly is displayed for the user. Let us say that you want to display a login link in the menu when the user is not logged in and remove that link from the menu when the user is logged in. Rather than code that logic into every page that has to display a menu you can use a ViewPreparer that generates the menu implementing any logic that is necessary. That View Preparer can then be associated with several pages.
Think of it more as manipulating the data for viewing rather than business logic.