繁体   English   中英

如何使用jsp传递Object:将param标签包含到另一个jsp中

[英]How to pass Object using jsp:include param tag into another jsp

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

这是一个代码..

<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文件..

<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>

这里我试图从param获取属性值,它总是发送String Type Value。 有没有办法在attributeSubFeatureRemove jsp文件中发送Object(DTO)? 请帮忙。

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

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

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

对此

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

这将使“attribute”成为一个“requestScope”变量,该变量可以在c:import的其他JSP文件中使用。 (注意:forEach不支持范围属性,因此请使用c:set在每次迭代中将其范围限定。)

2)将原始jsp:include更改为c:import。 所以改变它:

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

对此

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

请注意,我们没有明确尝试将该属性作为参数传递,因为我们已经将它提供给“requestScope”中的所有c:import页面。

3)修改你的c:导入的JSP,使用requestScope引用属性,方法是:

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

对此

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

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

这项技术将更容易管理。

所以我通过使用标记文件解决了这个问题。 我现在不再使用jsp:include标记,因为它总是发送字符串类型。

这是一个解决方案..

<%@ taglib prefix="cms2" tagdir="/WEB-INF/tags/spine/surgery"%>
<c:forEach items="${attributeDTOList}" var="attribute" varStatus="status">  
     <cms2:attributeSubFeatureRemove attribute="${attribute}" /> 
</c:forEach>

attributeSubFeatureRemove.tag文件

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ attribute name="attribute" required="true" type="com.medtronic.b2b.core.dto.HCCB2BClassificationAttributeDTO" %>
<%@ taglib prefix="surgery" tagdir="/WEB-INF/tags/spine/surgery"%>               

    <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> 
        <surgery:attributeSubFeatureRemove attribute="${subAttribute}" />
     </c:forEach>

这里我给出了Type Attribute来访问标签文件中的Object。 它工作正常。

您不能使用jsp:include param标记直接将Object传递到另一个jsp中。

但是,您可以使用jsp:include param标记将该属性的NAME(作为字符串)传递到另一个jsp中。然后在包含的jsp中,您可以通过requestScope中的名称获取该属性。

在你的主JSP中:

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

在attributeSubFeatureRemove.jsp中:

object's name = ${param.objName}
object itself = ${requestScope[param.objName]}

Just for an easier access:
<c:set var="obj" value="${requestScope[param.objName]}" scope="page"></c:set>
obj=${obj}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM