当前位置: 代码迷 >> java >> 如何在提交表单时关闭弹出窗口时重新加载父窗口,而我正在使用java类显示子窗口内容
  详细解决方案

如何在提交表单时关闭弹出窗口时重新加载父窗口,而我正在使用java类显示子窗口内容

热度:23   发布时间:2023-07-25 19:41:12.0
function printPDF() {
var URL1= '<%=Handlers.getPath(PA.PDF_RH)%>';
var URL2 = "<%=jspBean.getPDFUrlContext()%>/" + 
                getValue(Obj.value, pdfFile);
            URL2 = URL2 + ".pdf";   
printFormat = window.open("","_blank","");
            printFormat.document.write("<html><head><title>Print Media Queue</title>");
            printFormat.document.write("<script> \n");
printFormat.document.write("function submit(){\n ");
printFormat.document.write("alert('Yeah ! I am inside submit');document.forms['myForm'].submit();\}");
            printFormat.document.write("function afterprint() { \n");
            printFormat.document.write("alert('i am inside After print method')\n");
            printFormat.document.write("self.close();\n");
            printFormat.document.write("opener.<%=jspBean.FORM_PRINT%>.submit();\n");
printFormat.document.write("} \n");
            printFormat.document.write("</");
            printFormat.document.write("script");
            printFormat.document.write(">");

            printFormat.document.write("</head><body onunload='afterprint()'><form  name='myForm' id='myForm' action='" + URL1 +"' method='POST' >");
            printFormat.document.write("<input type='hidden' name = 'printpdf' value='" + URL2 + "'>");
printFormat.document.write("</form>");
            printFormat.document.write("<");
            printFormat.document.write("script>submit(); ");
            printFormat.document.write("window.navigator.disablePACheck = false;print()");
            printFormat.document.write("</");
            printFormat.document.write("script");
            printFormat.document.write(">");

            printFormat.document.write("</body></html>");
            printFormat.document.close();



        }

我的java课

public class PdfRH { public void doRequest(ControlBlock oSCB) throws Exception {
        HttpServletResponse response;


        response = oSCB.getHttpServletResponse();
        String pdfPath = oSCB.getRequestParameter("printpdf") ;
        System.out.println("*********************************************"+pdfPath);
        File pdf = new File(pdfPath);
        String pdfName = pdfPath.substring(pdfPath.lastIndexOf("/") + 1, pdfPath.length());
        ServletOutputStream stream = null;
        BufferedInputStream buf = null;

        try{
            stream = response.getOutputStream();
            //    PrintWriter pw = response.getWriter();
            response.setHeader("Content-type","application/pdf");
            response.setHeader("Content-disposition","inline; filename=" + pdfName);  
            response.setHeader("Cache-Control", "no-cache"); 
            response.setHeader("Pragma", "No-cache");
            response.setDateHeader("Expires", 0);  
            response.setContentType("application/pdf");

            FileInputStream input = new FileInputStream(pdf);
           // response.setContentLength((int) pdf.length());
            buf = new BufferedInputStream(input);
            int readBytes = 0;
            /*pw.println("<html>");
            pw.println("<head><title>Hello World</title></title>");
            pw.println("<body>");
            pw.println("<h1>Hello World</h1>");
            pw.println("</body></html>");*/
            //response.getOutputStream().write(b);
            while ((readBytes = buf.read()) != -1)
                stream.write(readBytes);
        }
        catch(Exception ioe){
            throw new ServletException(ioe.getMessage());

        }
        finally 
        {
            if (stream != null)
                stream.close();
            if (buf != null)
                buf.close();
        }
    }

在按钮上单击,我调用一个javascript函数,并且在该javascript函数中,我编写了html编码,在其中创建了表单,并在提交时传递了类名和另一个变量,即将读取pdf的pdf路径。在弹出窗口中显示。
正如我们看到的那样,整个控件已经进入java类,但是我希望在关闭pdf窗口时应该重新加载父窗口

尝试将以下代码添加到您的Java类中...

String code_windowClose = "<script>window.onunload = refreshParent;
function refreshParent() {
    window.opener.location.reload();
}
</script>";
   stream.wrtie(code_windowClose);

此代码会将onunload事件脚本插入到您的子窗口中,该子窗口正试图关闭打开器(即父窗口)窗口。

请让我知道,如果你有任何问题

  相关解决方案