【问题标题】:How to pass an Object from one JSP to other JSP?如何将对象从一个 JSP 传递到另一个 JSP?
【发布时间】:2016-03-11 09:28:24
【问题描述】:

我有两个 jsp 文件,jsp1 和 jsp2。当点击 jsp1 上的编辑按钮时,将调用 java 脚本函数。从javascript函数我调用jsp2。我使用 window.open 因为我需要这个 jsp2 作为弹出窗口打开。现在我想在单击编辑按钮时将一个对象从 jsp1 传递到 jsp2。我该怎么做?

    This is my code

    jsp1

    < script language="JavaScript">

            function edit(){

                    window.open("jsp2.jsp",'popuppage',"height=600,width=800,status=yes,toolbar=no,menubar=no,location=no");
                    return true;

                }
    < /script>

     < stripes:form name="upload"
                        action="/upload.action" enctype="multipart/form-data">
          < table>

         <% List<TableDto> list = (List<TableDto>)request.getAttribute("table"); %>

                            <%
                                if(list != null){
                                 for (TableDto dto : list) { 
                            %>
                      < tr>
                                <td><%=dto.getName()%></td>
                                <td><img src="<%=dto.getPath() %>" width="40px"
                                    height="40px" /></td>
                                <td><%=dto.getText()%></td>
                                <td>


                                <input type="image" name="edit"onclick="edit()"
                                    src="${globalPath}/images/edit_pen.jpg">

                    < /tr>

                            <%
                                } 
                                }
                            %>

          </table>
    < /stripes:form>

【问题讨论】:

标签: javascript java jsp


【解决方案1】:

1.使用url参数
可以添加“jsp2?xxx=xxx&xxx=xxx”等url参数。然后在controller(jsp2)中,可以通过

获取这些参数
request.getParameter("xx")

并将变量传递给jsp引擎。

2.使用cookies
可以编写js代码将数据保存在cookies中,然后在jsp2中读取数据

///write cookies
document.cookie="xx="+xx;
///read cookies
function getCookie(name)
{
    var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
    if(arr=document.cookie.match(reg))
        return unescape(arr[2]);
    else
        return null;
}

3.使用会话
在 controller1(jsp1) 中,通过

保存会话
request.getSession().setAttribute(name, value);

在jsp2中,读取会话

request.getSession().getAttribute(name);

【讨论】:

    【解决方案2】:

    你需要使用

    • 如果您使用从一个页面到另一个页面的转发(jsp:foprward 或 RequestDispatcher),则使用 request.setAttribute(..) 和 request.getAttribute(),因为您在同一个请求中

    • 如果您使用重定向(通过 response.sendRedirect()),则使用 request.getSession().setAttribute(..) 和 request.getSession().getAttribute()。

    【讨论】:

      【解决方案3】:

      以下是将值从一页传递到另一页的方法 -

      1. 将值放入会话对象中。
      2. 将值放入应用程序对象中
      3. 将值放在重定向 URL 的末尾。

      【讨论】:

        【解决方案4】:

        以下示例适用于我。第一页包含处理函数:

        function popup() {
            window.data = {name : 'myName'};
            var handle = window.open('importJSON.html', 'popup',"height=600,width=400,status=yes,toolbar=no,menubar=no,location=no");
        }
        

        第二页包含处理函数:

        function showName2() {
            alert(window.opener ? window.opener.data.name : undefined);
        }
        

        在 Chromium 47 和 Firefox 43 上测试。如果两个页面从同一个主机。如果可能的话,最好不要通过网络来回发送数据对象。

        【讨论】:

          【解决方案5】:

          您可以在属性中设置对象,该属性可以存储在会话中,而在另一个 jsp 中,您可以从会话中检索并使用。

          【讨论】:

          • 你能说一下,我在哪里设置了 Object in 属性?
          • 查看詹金周的答案
          猜你喜欢
          • 1970-01-01
          • 2014-04-17
          • 1970-01-01
          • 2011-07-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多