gpt4 book ai didi

java - 如何使用jsp :include param tag into another jsp传递对象

转载 作者:技术小花猫 更新时间:2023-10-29 12:14:44 25 4
gpt4 key购买 nike

我正在尝试使用 jsp:include 标记将 DTO 对象从一个 jsp 发送到另一个 jsp。但它始终将其视为 String。我无法在包含的 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>

这里我试图从参数中获取属性值,它总是发送字符串类型值。有没有办法在 attributeSubFeatureRemove jsp 文件中发送对象(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 不支持 scope 属性,因此请使用 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:imported 页面。

3) 修改您的 c:imported 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 中或作为参数(因为我们不再将其作为参数传递)。

这项技术会让您更容易管理。

关于java - 如何使用jsp :include param tag into another jsp传递对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29367559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com