Java 如何使用 jsp:include param 标签将 Object 传递到另一个 jsp

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29367559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:52:31  来源:igfitidea点击:

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

javahtmljspjstljsp-tags

提问by Free-Minded

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.

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

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

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>

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.

在这里,我试图从 param 获取属性值,它总是发送字符串类型值。有没有办法在 attributeSubFeatureRemove jsp 文件中发送对象(DTO)?请帮忙。

采纳答案by alfreema

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:

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

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

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

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

2)把你原来的jsp:include改成c:import。所以把它从:

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

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

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

3) 修改您的 c:imported JSP 以通过更改以下内容使用 requestScope 引用该属性:

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

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

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

这种技术会让你更容易管理。

回答by Free-Minded

So I have solved the issue by using tag file. I am no longer using jsp:include tag now because it will always send String Type.

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

Here is a solution ..

这是一个解决方案..

<%@ 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 file

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>

Here I am giving Type Attribute to access Object in tag file. And it works fine.

在这里,我提供了类型属性来访问标记文件中的对象。它工作正常。

回答by qiangbro

You can not directly pass an Object using jsp:include param tag into another jsp.

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

However, you can pass that attribute's NAME (as a string) using jsp:include param tag into another jsp, Then in the included jsp, you can get that attribute itself by its name from requestScope.

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

in your main JSP:

在您的主 JSP 中:

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

in attributeSubFeatureRemove.jsp:

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