jsp taglib 与包含 jsp 页面之间的根本区别是什么?
我有几个常见的元素(组件),它们会生成一些 html。 看来我的选择是创建一个 taglib,或者只是将该逻辑放入 jsp 页面并包含 jsp。
有什么不同? 积极与消极?
i have several common elements (components), that will generate some html. it seems my options are creating a taglib, or just putting that logic into a jsp page and including the jsp.
whats the difference? positives vs negatives?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
标签库允许您定义可以记录的(类型化)参数。 此外,标签库可以知道它们在对象树中的位置,因此在不同的上下文中表现不同; 或者一遍又一遍地调用特定模板来创建迭代器或语句结构。
您是否知道标记库不一定必须用 Java 编写? 还有一个称为标记文件的概念,它允许您在 JSP 中编写标记库; 通常更适合扁平组件...非常接近包含。
Taglibs allow you to define (typed) parameters which you can document. Also taglibs can be aware of their location in the object tree so act differently in a different context; or call a specific template over and over again to create iterators or statement constructs.
Are you aware that taglibs don't necessarily have to be written in Java? There is also a concept called tagfiles which allows you to write your taglib in JSP; often more suitable for flat compononents... quite close to includes.
当您使用 taglib 时,容器通常会:
这将所有代码保留在同一资源中(请求不会传递到另一个组件),因此允许您构建循环行为并访问当前页面上的其他组件。
学习标签库是有开销的。 但一旦你的第一个标签开始工作,一切就开始走下坡路了。 此外,最终结果对于非开发人员来说更容易理解(假设您为标签选择了好的名称)。
When you use a taglib the container typically:
This keeps all the code within the same resource (the request does not get passed on to another component) and hence allows you to build in looping behaviour and access other components on the current page.
There is overhead in learning Tag Libraries. But once you have got your first tag working its all downhill. Also the end result will be easier for non-developers to understand (assuming you choose good names for the tags).
标签(包括易于使用的类似 JSP 的标签文件机制)支持使用强类型命名参数进行调用。
JSP 标记的另一个非常有用但经常被忽视的功能是
JspFragment
属性类型。 这允许您将一段 JSP 代码作为参数传递到要调用的标记中,或许可以重复调用。包括缺乏这些强大的参数化功能。
Tags (which include the easy-to-use JSP-like tag file mechanism) support invocation with strongly-typed, named parameters.
Another incredibly useful and surprisingly often overlooked feature of JSP tags is the the
JspFragment
attribute type. This allows you to pass a chunk of JSP code, as a parameter, into a tag to be invoked, perhaps repeatedly.Includes lack these powerful parameterization features.
标记库使定义和处理参数变得更加容易,但开发它们的开销很大。 Includes 更简单,但功能较弱。 很大程度上取决于你的风格。
根据我的经验,人们通常只使用 include,因为他们不想花时间学习创建 tablib。 导致公平混乱。 只要您的团队规模较小并且您的内容不太复杂,那就还不错。 但这是一种代码味道。
taglibs make it easier to define and handle parameters, but there's a significant overhead to developing them. Includes are simpler, but less powerful. Much depends on your style.
In my experience, people generally just use includes because they don't want to take the time to learn to create tablibs. Leading to a fair mess. As long as your team small and your includes not too complex, it's not too bad. But it is a code smell.