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

3

20

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.

Hirsh answered 31/3, 2015 at 11:26 Comment(3)
@fiffy request.setAttribute wants variable. How i will define variable? It has to be set like this ${subAttribute}Hirsh
No thats not working :(Hirsh
https://mcmap.net/q/663709/-jsp-param-with-java-classDevour
S
22

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.

Sunburn answered 31/3, 2015 at 14:56 Comment(3)
Scope attribute is undefined for <c:forEach> tag. Use <c:set> tag instead. <c:set var="attribute" value="${attribute}" scope="request"></c:set>Cresol
Better you update your answer as suggest above in comment. Gr8 job.!!Sherrod
The answer could use some clarity. It wasn't clear to me first that 1) 2) were steps. After following it did solve my problem though. I did not know jsp:param only passes Strings.Mafala
F
2

You can not directly pass an Object using jsp:include param tag into another 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.

in your main 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:

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}
Fetiparous answered 9/8, 2017 at 13:38 Comment(0)
H
1

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.

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

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

Hirsh answered 31/3, 2015 at 12:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.