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

Fre*_*ded 19 html java jsp jstl jsp-tags

我试图使用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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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

alf*_*ema 17

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

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

<c:forEach items="${attributeDTOList}" var="attribute" varStatus="status">
Run Code Online (Sandbox Code Playgroud)

对此

<c:forEach items="${attributeDTOList}" var="attribute" varStatus="status">
    <c:set var="attribute" value="${attribute}" scope="request"/>
Run Code Online (Sandbox Code Playgroud)

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

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

<jsp:include page="attributeSubFeatureRemove.jsp" >
    <jsp:param name="attribute" value="${attribute}" />
</jsp:include>
Run Code Online (Sandbox Code Playgroud)

对此

<c:import url="attributeSubFeatureRemove.jsp"/>
Run Code Online (Sandbox Code Playgroud)

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

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

<c:set value="${param.attribute}" var="attribute" />
<c:forEach items="${attribute.subFeatures}" var="subAttribute">
Run Code Online (Sandbox Code Playgroud)

对此

<c:forEach items="${requestScope.attribute.subFeatures}" var="subAttribute">
Run Code Online (Sandbox Code Playgroud)

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

这项技术将更容易管理.

  • 对于<c:forEach>标记,未定义范围属性.请改用<c:set>标签.<c:set var ="attribute"value ="$ {attribute}"scope ="request"> </ c:set> (2认同)