【问题标题】:setting a button value on request scope and getting on other jsp在请求范围上设置按钮值并进入其他 jsp
【发布时间】:2012-11-28 19:00:07
【问题描述】:

我试图在请求范围内设置值并在我单击按钮弹出窗口打开并获取空值时获取另一个 jsp,因为我的要求是获取其他 jsp 上的按钮值以验证这两个部分的代码如果用户在按钮 1 上,然后 find by id 文本区域打开,如果单击按钮 2,则 find by name 文本区域打开,然后应该关闭弹出窗口

    <HTML>
    <HEAD>
        <TITLE>Using Buttons</TITLE>
        <script type="text/javascript">


               function button1()
               {
                var mywindow= window.open("file.jsp", "file","status=1,width=350,height=150");
         }    

        </SCRIPT>
    </HEAD>

    <BODY>
        <H1>Using Buttons</H1>

         <INPUT TYPE="BUTTON" name="butto" VALUE="Button 1" ONCLICK="button1()"/>
               <% request.setAttribute("button1" ,"butto");%>

       <% request.setAttribute("button1" ,"butto");%>



    </BODY>
</HTML>

这是我的文件 .jsp

<!

DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt"%>
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql"%>
<%@ taglib prefix="x" uri="http://java.sun.com/jstl/xml"%>
<%@page
    import="com.nousinfo.tutorial.employee.service.model.bo.EmployeeBO"%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>

</head>
<%= (String)request.getAttribute("button1") %>
<body>



</body>
</html>

为什么我得到空值的错误是什么

【问题讨论】:

    标签: jsp


    【解决方案1】:

    通过简单的调用window.open()它不会将你的请求范围对象转移到其他jsp。你不能在不使用forward()的情况下将数据从请求范围发送到另一个JSP;

    用法示例

    RequestDispatcher rd = servletContext.getRequestDispatcher("/pathToResource");
    rd.forward(request, response);
    

    但是,在您的示例中,仅调用 window.open() 是行不通的

    【讨论】:

    • MuraliPrasanth 如果我在我的 index.jsp 中使用它,那么它直接转发到文件 .jsp 我想点击按钮弹出将打开然后值将通过文件.jsp显示
    • 您可以简单地使用会话范围对象,而不是在您的场景中使用请求范围对象,就像您使用请求范围对象(如 session.setAttribute() 和 session.getAttribute())
    猜你喜欢
    • 2017-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多