简体   繁体   中英

Passing object as variable in JSP

I am trying to pass a List of objects from one JSP page to another which is a pop-up

In my first page I am using this submit

<input type="submit" id="changehost" class="button_host" name="changehost"  value="change"  onclick='openHostPicker();' />

which calls this function:

function openHostPicker()
{
    var url = 'hostSearch.jsp?hostlist='+'<c:out value="${hostlist}"></c:out>';
    window.open(url);
}

this parent page has an attribute '${hostlist}' which has been passed to the page by a controller and has been validated as not null at this stage.

I want to pass the 'hostlist' object to the pop-up , where it will be used to populate a table.

When I execute the code I can see the url is formed as:

http://localhost:8080/mainadmin/hostSearch.jsp?hostlist=[com.webapps.model.Host@2e96277c , com.webapps.model.Host@3fa55c9, com.webapps.model.Host@28160129, com.webapps.model.Host@3ca63f4e, com.webapps.model.Host@468638fd]

However I get no results in my popup page. Here is the code:

<c:forEach items="${hostlist}" var="host">
            <tr>
               <td style="border:1px solid black;"><c:out value="${host.getIdHost()}" /></td>
                <td style="border:1px solid black;"><c:out value="${host.getContactName()}" /></td>
                <td style="border:1px solid black;"><c:out value="${host.getAddress()}" /></td>
                <td style="border:1px solid black;"><c:out value="${host.getLocation()}" /></td>
                <td style="border:1px solid black;"><Button type='submit' name='hostrefNo' value='${host.getIdHost()}'>Select</button></td>
            </tr>
        </c:forEach>

I have put a small validation test in the page

<c:if test="${empty hostlist}">
            hostlist is empty or null.
        </c:if>
        <c:if test="${not empty hostlist}">
            hostlist is NOT empty or null.
        </c:if>

which reports 'hostlist is empty or null'

Any ideas what I am doing wrong ?

In your parent JSP set your hostlist into the session as

<c:set var="hostlist" value="${hostlist}" scope="session" />

Since, the popup page request will go as a new HttpServletRequest , this is required to move the list received in the request to Session .

The list should then become available to the <c:forEach> tag in the popup. The href link should no longer pass the hostlist query parameter.

function openHostPicker()
{
    window.open('hostSearch.jsp');
}

Your current approach doesn't work because only strings can be passed to the server as query parameters. A Java object cannot be correctly serialized/deserialized over a request parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM