Java 将列表传递给另一个 jsp 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1538821/
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-12 13:58:05  来源:igfitidea点击:

Pass a List to another jsp file

javajsp

提问by Sergio del Amo

I wanted to pass a List type object which is in the main JPS to an include JSP ( jsp:include). Since the parm only support strings, I cannot use the parm tag to pass List type data to the include file.

我想将主 JPS 中的 List 类型对象传递给包含 JSP (jsp:include)。由于 parm 仅支持字符串,因此我无法使用 parm 标记将 List 类型数据传递给包含文件。

Uses examples:

使用示例:

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

Or:

或者:

<jsp:include page="/jsp/appList.jsp">
     <jsp:param name="applications" value="${confirmed_applications}"/>
</jsp:include>
<jsp:include page="/jsp/appList.jsp">
    <jsp:param name="applications" value="${unconfirmed_applications}"/>
</jsp:include>
<jsp:include page="/jsp/appList.jsp">
    <jsp:param name="applications" value="${canceled_applications}"/>
</jsp:include>

I could create a Simple Tag Handler but I would like to know if there is an easier way.

我可以创建一个简单的标签处理程序,但我想知道是否有更简单的方法。

回答by Jonathan Feinberg

This is indeed what jsp tags are meant for. You create a .tag file that accepts attributes(not parameters), which can be of arbitrary type.

这确实是 jsp 标签的用途。您创建一个接受属性(非参数)的 .tag 文件,该文件可以是任意类型。

See this articlefor a good tutorial.

请参阅这篇文章以获得一个很好的教程。

"Params" are analogous to the parameters you see in an HTTP query (the part of the URL following the '?').

“参数”类似于您在 HTTP 查询中看到的参数(“?”后面的 URL 部分)。

回答by ChssPly76

How are you obtaining that list in your main JSP?

您如何在主 JSP 中获取该列表?

If you're getting it from the model (direct / indirect request / session attribute) you can pass the nameof the attribute to your included JSP and re-obtain it from model there:

如果您从模型中获取它(直接/间接请求/会话属性),您可以将属性的名称传递给包含的 JSP 并从那里的模型中重新获取它:

<jsp:include page="/jsp/appList.jsp">
  <jsp:param name="applications" value="confirmedApplications"/>
</jsp:include>

If you're generating or modifying that list in the JSP itself you've really got bigger problems then this :-) but you can bindyour list to request under some attribute name and then use the above approach.

如果您在 JSP 本身中生成或修改该列表,那么您确实遇到了更大的问题:-) 但是您可以您的列表绑定到某个属性名称下的请求,然后使用上述方法。

回答by muthu

Either you can use request or session objects for transferring objects.

您可以使用请求或会话对象来传输对象。

Advantage

优势

Request - Use request as its lifetime is lesser compared to a session object.

请求 - 使用请求,因为与会话对象相比,它的生命周期较短。

Session - You can retrieve the same object sometime later in another page or action

会话 - 您可以稍后在另一个页面或操作中检索相同的对象

回答by svachon

Assuming that you put the list in request scope and using applicationsas the name of your parameter you can refer to the list like this ${requestScope[param.applications]}

假设您将列表放在请求范围内并applications用作参数的名称,您可以像这样引用列表${requestScope[param.applications]}

回答by Tobias M

Agree with ChssPly76 that its probably a sign of trouble if the List is being generated in the JSP, but another alternate approach to get the object to the other JSP is to add it to the HttpServletRequest objects attribute field using a scriplet:

同意 ChssPly76,如果在 JSP 中生成 List,这可能是一个麻烦的迹象,但另一种将对象获取到另一个 JSP 的替代方法是使用脚本将其添加到 HttpServletRequest 对象属性字段:

JSP with the List:

带有列表的 JSP:

<%
request.setAttribute("theList", ListObject);
%>

The other JSP:

另一个JSP:

<%
List myList = (List) request.getAttribute("theList");
%>

回答by Sèb

You could pass the List object via the requestScope :

您可以通过 requestScope 传递 List 对象:

<c:set var="listApplications" value="${applications}" scope="request"/>
<jsp:include page="/jsp/appList.jsp" />

And in appList.jsp you can retrieve the list using

在 appList.jsp 中,您可以使用检索列表

${requestScope.listApplications}

回答by ArunDhwaj IIITH

Assume you have parent.jsp and child.jsp.

假设您有 parent.jsp 和 child.jsp。

You want to:

你想要:

1) Include child.jsp from parent.jsp, 2) Send some parameters from parent.jsp to the child.jsp.

1) 从 parent.jsp 中包含 child.jsp, 2) 从 parent.jsp 发送一些参数到 child.jsp。

Do the following steps:

执行以下步骤:

1) In parent.jsp `

1) 在 parent.jsp 中`

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<c:set var="items" value="${itemsFromDB}" scope="request"/>
<jsp:include page="child.jsp"/>

` 2) In child.jsp

` 2) 在 child.jsp 中

<div>
alert("${requestScope.items}");
</div>

Thats all.

就这样。