如何在页面中重用 HTML/JSP?

发布于 2024-08-17 02:49:39 字数 399 浏览 3 评论 0原文

我是 JSP 新手,我正在尝试减少大量剪切和粘贴代码。
在项目的每个页面上,大约有 25 行混合的 JSP、Struts 标签、JSTL 标签和 HTML,它们已被剪切并粘贴到页面的各个位置。这大约 25 行重复使用的代码从一个页面到另一个页面根本不相似(大约有 250 个页面),但是每个页面内都完全相同。最终这个(业务逻辑)代码应该移出视图,但是这样做将是一个比我目前的日程允许的更大的项目,所以我想知道是否有一种简单的方法来重用混合标签+JSP作为临时修复,以便可以在时间允许的情况下分阶段重构代码。

为了清楚起见,我正在寻找一种封装代码而不创建新文件(/本地到页面范围)的方法 - 即它应该在调用它的同一页面中定义。

有些人建议这可以用 Tiles 来完成 - 如果是这样的话,请告诉我如何做。

I'm new to JSP, and I'm trying to reduce a massive amount of cut-and-pasted code.
On each page in the project, there are around 25 lines of mixed JSP,Struts tags,JSTL tags, and HTML, which have been cut and pasted at various points in the page. These ~25 lines of re-used code are not even remotely similar from page to page (and there ~250 pages), but exactly the same within each page. Ultimately this (business logic) code should be moved out of the View, but doing so would be a larger project than my schedule permits at the moment, so I'm wondering if there is a simple way to re-use Mixed Tags+JSP within a page, as a temporary fix so that the code can be refactored in stages as time permits.

For clarity, I am looking for a way to encapsulate code without creating a new file (/local to page scope) - i.e. it should be defined in the same page it is called from.

Some have suggested that this can be done with Tiles - if that is the case, please show me how.

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

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

发布评论

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

评论(3

南巷近海 2024-08-24 02:49:39

查看 Apache 磁贴。由于您正在使用 Struts,我很惊讶您还没有找到它。它基本上是一个模板引擎,我认为适合您的要求。

已经建议的 可以与 一起使用以传递变量。实际上

<jsp:include file="includedFile.jsp">
    <jsp:param name="username" value="jsmith" />
</jsp:include>

,如果只想包含 1 个带有公共代码的文件,我会推荐 的简单性而不是 Tiles 的强大功能。

Take a look at Apache tiles. Since you are working with Struts, I'm surprised you haven't found it already. It is basically a templating engine and I think fits your requirements.

The already suggested <jsp:include> can be used with <jsp:param> in order to pass variables. Like

<jsp:include file="includedFile.jsp">
    <jsp:param name="username" value="jsmith" />
</jsp:include>

Actually, if only wanting to include 1 file with the common code, I'd recommend the simplicity of <jsp:include> over the power of Tiles.

揪着可爱 2024-08-24 02:49:39

您可以创建一个包含文件并在需要的地方使用 jsp:include 。可变部分可以使用 jsp 条件、jstl EL 属性或 struts EL 来完成。

我不一定要重新编码现有页面,因为它们已经在工作了。

you could create one include file and jsp:include it wherever it's needed. the variable parts could be done with jsp conditionals, jstl EL attributes, or struts EL.

i wouldn't necessarily want to re-code existing pages tho since they're already working.

忘羡 2024-08-24 02:49:39

Struts Tiles 是 Struts 中用于模板化特定页面的最佳选择,这样就不需要每次都对所有页面通用的部分进行编码。
请参阅 http://struts.apache.org/1.x/struts-tiles/

如上所述,JSP 特有的一种更简单的方法是 jsp:include。

仅供参考,还有第三种方法,即 include 指令的方法

jsp:include 和 include 指令之间的区别如下

仅当以下情况时才应使用 include 指令 (<@include filerelativeURL>)
1) 如果文件包含静态文本
2)如果文件很少被改变(如果这种类型的包含文件被修改,JSP引擎可能不会重新编译JSP)
3) 如果您有一个可以在多个页面中重复使用的通用代码片段(例如页眉和页脚),

则应仅在以下情况下使用 jsp:include
1) 包含的JSP内容是动态的并且可以在运行时改变
2)选择在运行时渲染哪些内容(因为 page 和 src 属性可以采用运行时表达式)

我希望这将帮助您做出决定。

Struts Tiles are the best to be used in Struts for templating a particular page so that the part which is common to all pages need not be coded everytime.
Refer http://struts.apache.org/1.x/struts-tiles/.

A more simpler approach very specific to JSP is jsp:include as discussed above.

Just for information there is a third approach which is the approach of include directive

Difference between jsp:include and include directive is as follows

One should use the include directive (< @ include file relativeURL >) only if
1) if the file includes static text
2) if the file is rarely changed (the JSP engine may not recompile the JSP if this type of included file is modified)
3) if you have a common code snippet that you can reuse across multiple pages (e.g. headers and footers)

One should use the jsp:include only if
1) The content of the included JSP is dynamic and can change at runtime
2) to select which content to render at runtime (because the page and src attributes can take runtime expressions)

I hope this will help you to decide.

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