我有两个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>

最佳答案

1.使用URL参数
您可以添加网址参数,例如“ jsp2?xxx = xxx&xxx = xxx”。然后在controller(jsp2)中,您可以通过以下方式获取这些参数

request.getParameter("xx")


并将变量传递给jsp引擎。

2.使用cookie
您可以编写js代码以将数据保存在cookie中,然后在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);

07-26 07:47