是否可以在JSP中创建本地页面范围? [英] Is it possible to create a local page scope in a JSP?

查看:93
本文介绍了是否可以在JSP中创建本地页面范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究基于JSP的组件化系统.这意味着可以通过拖放将JSP的一部分从一个JSP移到另一个JSP,并导致需要本地页面范围作为一个JSP中定义的组件的变量,而与另一个JSP相撞. 我可以使用Servlet 3.0/JSP 2.2/JSTL 1.2.

I'm working on a componentization system based on JSPs. This means that parts of the JSP can be moved from one JSP to an other one via drag and drop and leads to a need for a local page scope as variable of a component defined in one JSP my collide in an other JSP. I can use Servlet 3.0 / JSP 2.2 / JSTL 1.2.

++标记文件++

这样做的直接方法是为组件或类似组件创建标签文件,因为它们具有此本地页面范围.但是对于标记文件中的每个更改,都需要重新部署,而创建标记文件本身需要一些时间.但是,如果没有其他解决方案,那么这个方向(自定义标记,标记文件,JSP包括...)可能是解决之道.

The straight way of doing that would be to create a tag file for a component or something similar as they have this local page scope. But for every change in the tag file it would need to get redeployed and needing to create a tag file needs some time by itself. But if there is no other solution this direction (custom tags, tag files, JSP includes,...) is probably the way to go.

++命名空间前缀/后缀++

在Portlet中,应该将每个变量与Portlet响应对象提供的名称空间连接起来.我可以做类似的事情,但这可能导致副作用和意外行为.如果有人忘记为名称空间添加前缀/后缀,则该名称可能会工作一段时间,但在该组件移到另一个JSP且变量名冲突时,又会在不更改代码的情况下停止工作.

In Portlets one is supposed to concatenate each variable with a namespace provided from the portlet response object. I could do something similar but this can lead to side effects and unexpected behavior. If someone forgets to prefix/suffix the namespace it might work for some time but stops working at an other time without changing the code when the component moved to an other JSP with a conflicting variable name.

++自定义标签换行++

我希望作为组件框架开发人员的我可以用这样的组件标签的标签文件包装组件开发人员的组件代码

I was hoping that I as a component framework developer can wrap the component code of a component developer with a tag file for a component tag like this

<a:component>
    <div data-component-id="9">
        <c:set var="componentId" value="9"/>
        <c:set var="path" value='${abc:getCurrentPath()}_${componentId}'/>
        <c:set var="resource" value='${abc:getResourceFromPath(path)}'/>
        <c:set var="val" value="${resource.getValue('paragraphValue')"/>
        <p><c:out value="${value}"/></p>    
    </div>
</a:component>

使变量位于标记文件的本地页面上下文中.但是相反,它们是在周围页面的上下文中.我正在寻找这样的东西

to have the variable in the local page context of the tag file. But instead they are in the context of the surrounding page. I'm looking for something like this

<% { %>
    <div data-component-id="9">
        <%
        String componentId = 9;
        String path = obj.getCurrentPath()+componentId;
        Resource resource = otherObj.getResourceFromPath(path);
        String value = resource.getValue("paragraphValue");
        %>
        <p><c:out value="<%=value%>"/></p>  
    </div>
<% } %>

这将创建一个代码块,其中的变量具有自己的名称空间.但是,当然是JSTL和JSTL-EL而不是scriptlet代码. 我查看了JSTL规范,但没有发现是否有创建此类本地页面范围的方法.但是我没有检查所有内容,因为它很大,而且我不确定是否可以使用自定义标签.

which would create a code block in which variables have their own namespace. But of course for JSTL and JSTL-EL instead of scriptlet code. I had a look at the JSTL specification but did not find out if there is a way to create such a local page scope. But I didn't check everything as it's huge and I'm not sure if it's possible with custom tags.

对我来说很明显,更大的代码块不应该包含在JSP中,但是我希望借助该框架为较小的组件提供简单的解决方案.

It is clear to me that bigger code blocks should not be in the JSP but with the framework I would like to provide simple solutions for smaller components.

++更改JSP代码++

当组件最初放置在JSP上或通过拖放操作移动时,我实际上将组件的JSP代码从一个JSP移至另一个JSP或在JSP内.这意味着如果组件的JSP代码不太复杂,我还可以通过编程方式对其进行操作,这有助于解决问题.

When components are initially placed on a JSP or moved around via drag 'n drop I actually move the JSP code of a component from one JSP to an other or within a JSP. This means I can also programmatically manipulate the JSP code of a component if it doesn't get too complex and it helps solving the problem.

推荐答案

由于我认为自定义标签包装可能是理想的解决方案,因此我创建了另一个更具体的问题,并且得到了

As I thought that custom tag wrapping could be the ideal solution I created an other more specific question and I've got an answer here that solves the problem.

只需在评估正文之前删除所有pageContext属性,然后在doEndTag()处再次添加它们.

It's simply to remove all pageContext attributes before the evaluation of the body and adding them again at doEndTag().

这篇关于是否可以在JSP中创建本地页面范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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