2015-03-31 22 views
9

Sto cercando di inviare oggetti DTO da un jsp a un altro jsp usando jsp: include tag. Ma lo tratta sempre come String. Non riesco a usare DTO nel mio file jsp incluso.Come passare Object usando jsp: include il tag param in un altro jsp

Ecco un codice ..

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

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

Qui sto cercando di ottenere valore da attribuire param, è sempre l'invio di stringa tipo di valore. C'è un modo per inviare Object (DTO) in attributeSubFeatureRemove file jsp? Per favore aiuto.

+0

@fiffy request.setAttribute vuole variabile. Come definirò la variabile? Esso deve essere impostato in questo modo $ {} sottoattributo –

+0

Nessun quello non lavorano :( –

+0

http://stackoverflow.com/a/13510064/2885897 – fiffy

risposta

9

Non penso che tu voglia veramente tagare i file qui. Questo è eccessivo e troppo confuso per quello che vuoi ottenere. È necessario dedicare del tempo a comprendere "ambito". Invece di file di tag, vorrei:

1) Modifica l'attributo di essere nella "richiesta" portata al posto della "pagina" di default portata modificando questa linea:

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

a questo

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

Ciò renderà "attributo" una variabile "requestScope" che può essere utilizzata in altri file JSP che sono c: importati. (Nota: forEach non supporta l'attributo scope, quindi usa c: set per portarlo all'interno di ogni iterazione.)

2) Cambia il tuo jsp originale: include in c: importa. Quindi cambiare da:

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

a questo

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

Nota che non abbiamo esplicitamente cerchiamo di passare l'attributo come un parametro, perché abbiamo già reso disponibile a tutti c: pagine importate nel "requestScope".

3) Modifica la c: importato JSP per fare riferimento l'attributo utilizzando il requestScope modificando questo:

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

a questo

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

Qui non abbiamo più bisogno della c: impostato perché si hai già l'attributo disponibile. Dobbiamo solo assicurarci di cercare nel requestScope per quella variabile, invece che nel pageScope predefinito o come parametro (perché non lo passiamo più come parametro).

Questa tecnica sarà molto più semplice da gestire.

+2

Ambito attributo non è definito per tag Utilizzare tag invece faizi

+1

Meglio aggiornare la risposta come suggerito sopra nel commento Lavoro Gr8. !! –

+1

persona bellissima, grazie lol –

1

Quindi ho risolto il problema utilizzando il file di tag. Non uso più jsp: includi tag ora perché invierà sempre String Type.

Ecco una soluzione ..

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

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

Qui sto dando Tipo di attributo per accedere oggetto nel file di tag. E funziona bene.

0

Non è possibile passare direttamente un oggetto utilizzando jsp: include il tag param in un altro jsp.

Tuttavia, è possibile passare NOME di tale attributo (come stringa) utilizzando jsp: include il tag param in un altro jsp, quindi nel jsp incluso, è possibile ottenere tale attributo in base al nome da requestScope.

nel vostro principale 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} 
Problemi correlati