如何使用jsp传递Object:将param标签包含到另一个jsp中 [英] How to pass Object using jsp:include param tag into another jsp

查看:341
本文介绍了如何使用jsp传递Object:将param标签包含到另一个jsp中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用jsp:include标记将DTO对象从一个jsp发送到另一个jsp。但它总是把它视为字符串。我无法在包含的jsp文件中使用DTO。



这里是一个代码..

 < c:forEach items =$ {attributeDTOList}var =attributevarStatus =status> 
< jsp:include page =attributeSubFeatureRemove.jsp>
< jsp:param name =attributevalue =$ {attribute}/>
< / jsp:include>
< / c:forEach>

attributeSubFeatureRemove.jsp文件..

 < c:set value =$ {param.attribute}var =attribute/> 
< c:forEach items =$ {attribute.subFeatures}var =subAttribute>
< c:forEach items =$ {subAttribute.attributeValues}var =subValue>
< c:if test =$ {subValue.preSelectionRequired}>
< c:set var =replaceParamsvalue =:$ {subAttribute.name}:$ {subValue.name}/>
< / c:if>
< / c:forEach>
< jsp:include page =attributeSubFeatureRemove.jsp>
< jsp:param name =subAttributevalue =$ {subAttribute}/>
< / jsp:include>
< / c:forEach>

这里我试图从param中获取属性值,它总是发送字符串类型值。有什么办法可以在attributeSubFeatureRemove jsp文件中发送Object(DTO)?请帮助。

解决方案

我不认为您真的想在此标记文件。对于你想要完成的事情来说,这太过分了,而且太混乱了。你需要花时间了解范围。而不是标记文件,我会:

1)通过更改此行,将属性更改为请求范围而不是默认页面范围:

 < c:forEach items =$ {attributeDTOList}var =attributevarStatus =status> b 




code>< c:forEach items =$ {attributeDTOList}var =attributevarStatus =status>
< c:set var =attributevalue =$ {attribute}scope =request/>

这会使attribute成为requestScope变量,可用于其他JSP文件C:进口。 (注意:forEach不支持scope属性,所以使用c:set将其范围限定在每次迭代中。)


$ b $ 2将原始jsp:include更改为c:import 。因此,请将其改为:

 < jsp:include page =attributeSubFeatureRemove.jsp> 
< jsp:param name =attributevalue =$ {attribute}/>
< / jsp:include> b




code>< c:import url =attributeSubFeatureRemove.jsp/>

请注意,我们没有明确地尝试将该属性作为参数传递,因为我们已经做出了它可用于requestScope中的所有c:导入的页面。


$ b 3)修改您的c:导入的JSP,使用requestScope引用属性, p>

 < c:set value =$ {param.attribute}var =attribute/> 
< c:forEach items =$ {attribute.subFeatures}var =subAttribute> b




code>< c:forEach items =$ {requestScope.attribute.subFeatures}var =subAttribute>

这里我们不再需要c:set,因为您已经拥有可用的属性。我们只需要确保我们查看该变量的requestScope,而不是默认的pageScope或作为参数(因为我们不再将它作为参数传递)。



这项技术对您来说更容易管理。


I am trying to send DTO Object from one jsp to another jsp using jsp:include tag. But it is always treating it as String. I can't able to use DTO in my included jsp file.

Here is a code ..

<c:forEach items="${attributeDTOList}" var="attribute" varStatus="status">  
         <jsp:include page="attributeSubFeatureRemove.jsp" >
             <jsp:param name="attribute" value="${attribute}" />
         </jsp:include>
</c:forEach>

attributeSubFeatureRemove.jsp file ..

<c:set value="${param.attribute}" var="attribute" />
<c:forEach items="${attribute.subFeatures}" var="subAttribute">
                                    <c:forEach items="${subAttribute.attributeValues}" var="subValue">
                                       <c:if test="${ subValue.preSelectionRequired}">
                                         <c:set var="replaceParams" value=":${subAttribute.name}:${subValue.name}" />
                                         <c:set var="removeURL" value="${fn:replace(removeURL, replaceParams, '')}" />
                                      </c:if>
                                    </c:forEach> 
                                    <jsp:include page="attributeSubFeatureRemove.jsp">
                                        <jsp:param name="subAttribute" value="${subAttribute}" />
                                     </jsp:include> 
</c:forEach>

Here I am trying to get attribute value from param, it is always sending String Type Value. Is there any way to send Object (DTO) in attributeSubFeatureRemove jsp file ? Please help.

解决方案

I don't think you really want tag files here. That's way overkill and too confusing for what you want to accomplish. You need to spend time understanding "scope". Instead of tag files, I would:

1) Change your attribute to be in the "request" scope instead of the default "page" scope by changing this line:

<c:forEach items="${attributeDTOList}" var="attribute" varStatus="status">

to this

<c:forEach items="${attributeDTOList}" var="attribute" varStatus="status">
    <c:set var="attribute" value="${attribute}" scope="request"/>

That will make "attribute" a "requestScope" variable that can be used in other JSP files that are c:imported. (Note: forEach doesn't support scope attribute so use c:set to scope it inside each iteration.)

2) Change your original jsp:include to c:import. So change it from:

<jsp:include page="attributeSubFeatureRemove.jsp" >
    <jsp:param name="attribute" value="${attribute}" />
</jsp:include>

to this

<c:import url="attributeSubFeatureRemove.jsp"/>

Note that we don't explicitly try to pass the attribute as a parameter, because we have already made it available to all c:imported pages in the "requestScope".

3) Modify your c:imported JSP to reference the attribute using the requestScope by changing this:

<c:set value="${param.attribute}" var="attribute" />
<c:forEach items="${attribute.subFeatures}" var="subAttribute">

to this

<c:forEach items="${requestScope.attribute.subFeatures}" var="subAttribute">

Here we no longer need the c:set because you already have the attribute available. We just need to make sure we look in the requestScope for that variable, instead of in the default pageScope or as a parameter (because we are no longer passing it as a parameter).

This technique will be a lot easier for you to manage.

这篇关于如何使用jsp传递Object:将param标签包含到另一个jsp中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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