增强的jsp:include实现 [英] Enchanced jsp:include implementation

查看:48
本文介绍了增强的jsp:include实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一直困扰着我关于<jsp:include..>的一件事是,不可能将非String值传递给包含的页面作为页面的不同输入.例如,我希望能够执行以下操作:

One of the things that has always bothered me about <jsp:include..> is that it's not possible to pass non-String values into the included page as distinct inputs to the page. For example, I'd like to be able to do the following:

<c:forEach var="item" items="${listOfItems}">
    <jsp:include page="mypage.jsp">
        <jsp:attribute name="item" value="${item}/>
    </jsp:include>
</c:forEach>

这样的想法是,通过声明要在jsp:include节点内作为属性传递的项目,该代码使它清楚了将参数提供给包含的页面的意图.

The idea being that, by declaring the item being passed in as an attribute inside the jsp:include node, the code is making it clear what the intent was... to supply that parameter to the included page.

当前唯一可用于执行此操作的机制是全局"定义属性,然后让包含的页面从全局空间读取它.不幸的是,这失去了使用< jsp:param>的相同意图".提供.此外,由于与任何编程环境中的全局变量相同的原因,这使得代码更难调试.

The only mechanism currently available for doing this is to define the attribute "globally", and then let the included page read it from the global space. Unfortunately, this looses that same "intent" that using <jsp:param> provides. Additionally, it makes the code harder to debug, for the same reason that globals in any programming environment do.

是否有人知道执行jsp:include功能但允许传入非String值的include机制的实现吗?或者,如果不是这样,我将对其他想法持开放态度,这些想法可以保持相同的意图目标,并且易于调试.

Does anyone know of an implementation of an include mechanism that performs the functions of jsp:include, but allows for passing in non-String values? Or, if not, I'd be open to alternative ideas that preserve the same goal of intent and ease of debugging.

请注意,当包含的页面引发错误时,我很乐意看到一个内置的捕获"机制:

As a side note, I'd love to see a builtin "catch" mechanism for when the included page throws an error:

<abc:include page="mypage">
    <abc:param name="something" value="some string value"/>
    <abc:attribute name="somethingelse" value="${nonStringValue}"/>
    <abc:catch var="ex">
        The page failed, so the content it generated will not be sent to the output
        stream. Instead, we can collapse the part of the page that it's content
        would be... possibly putting a comment in the page with
        <!-- There was an exception generating this module: 
             <c:out value="${ex.getMessage}/>
          -->
    </abc:catch>
</abc:include>

推荐答案

在这里阅读了答案,并对这个问题进行了进一步的研究之后,我提出了以下建议:

Having read the answers here, plus doing additional research on the subject, I've come up with the following:

  • 您可以序列化对象,然后在随附的JSP中反序列化它们.不喜欢这样做,因为它会使代码更复杂(您传递的每个对象都必须可序列化,等等)
  • 您可以使用标签库.不喜欢这样做,因为我觉得它们的作用与JSP包含文件不同
  • 您可以在请求范围内定义变量,这将使它们可用于随附的JSP.对此并没有太大的兴趣,因为它没有显示出程序员的意图(将值传递到包含的页面中,仅用于此目的).
  • 确实没有(我可以找到)实现我想要的东西的实现,但是可能是通过自定义标签构建的东西.

我做了一些工作,将代码组合在一起以实现所需的内容,然后将其放在sourceforge .它允许您以我描述的方式指定输入:

I did some work and put together the code to achieve what I was looking for, and then threw it up on sourceforge. It allows you to specify the inputs in the way I was describing:

<inc:include page="normal.jsp">
    <inc:param name="param1" value="param1value" />
    <inc:param name="param2" value="param2value" />
    <inc:attrib name="attrib1" value="${attrib1value}" />
    <inc:attrib name="attrib2" value="${attrib2value}" />
    <inc:catch var="ex">
       This block was not rolled up because there wasn't an error. 
       Should never see this, but just in case, the exception was: ${ex.message}
    </inc:catch>
</inc:include>

(此时)唯一的问题是,我将属性添加到请求范围中,然后才包含页面,然后将其删除(如果已经存在,则将其重置).相反,我想做的是包裹请求对象,并覆盖属性方法以自动包括传入的值...我仍在努力.

The only problem with it (at this time) is that I add the attributes to the request scope before the page is included, then remove them after (or reset them if they already existed). What I'd like to do, instead, is wrap the request object instead and override the attribute methods to automatically include the values passed in... I'm still working on that.

这篇关于增强的jsp:include实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆