【问题标题】:Capture current JSF page content捕获当前 JSF 页面内容
【发布时间】:2013-07-29 13:30:08
【问题描述】:

我想捕获当前页面并将其发送到将其转换为 pdf 的应用程序。

这是我正在使用的:

FacesContext facesContext=FacesContext.getCurrentInstance();


       HttpServletResponse response = (HttpServletResponse)
facesContext.getExternalContext().getResponse();
       HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
//        RequestPrinter.debugString();
       response.reset();
    // download a pdf file
       response.setContentType("application/pdf");
        response.setHeader("Content-Disposition","attachment;filename="+new Date().toString()+".pdf");
         prince.setVerbose(true);
        prince.setLog(logFile);

            try{


                //getPath() to the page the user is currently on
                URL pagePath=new URL(this.getPath());
                URLConnection urlConnection = pagePath.openConnection();

                urlConnection.setDoOutput(true);

                int length = urlConnection.getContentLength();

                //Lets use inputStream
                BufferedInputStream bis=new BufferedInputStream(urlConnection.getInputStream());
                response.setContentLength(length);
                //this.getPageUsingJSoup().data().getBytes();
                //call prince and pass params for inputstream  outputStream

                prince.convert(bis,response.getOutputStream());
                urlConnection.getInputStream().close();

            }catch(MalformedURLException mu){

                mu.printStackTrace();
            }
            catch(IOException ioe){
            ioe.printStackTrace();
            }

   facesContext.responseComplete();

由于网站需要认证,所以生成的pdf就是登录错误页面。

有没有办法捕获使用当前用户会话的页面内容?

提前谢谢你。

【问题讨论】:

    标签: session pdf jsf


    【解决方案1】:

    只需在与当前请求相同的 HTTP 会话中请求页面即可。如果您的 web 应用支持 URL 重写(默认情况下),那么只需将会话 ID 附加为 jsessionid 路径片段:

    String sessionId = ((HttpSession) externalContext.getSession()).getId();
    InputStream input = new URL("http://localhost:8080/context/page.jsf;jsessionid=" + sessionId).openStream();
    // ...
    

    或者,如果你的 webapp 不接受 URL 重写,而只接受 cookie,那么按照通常的方式将其设置为请求 cookie:

    URLConnection connection = new URL("http://localhost:8080/context/page.jsf").openConnection();
    connection.setRequestProperty("Cookie", "JSESSIONID=" + sessionId);
    InputStream input = connection.getInputStream();
    // ...
    

    请注意,我删除了setDoOutput(),因为您似乎对执行 POST 请求不感兴趣。

    【讨论】:

    • 嗨@BalusC,我们的网络应用程序支持 URL 重写。我尝试了您的第一个建议,但仍然收到错误登录页面。 webapp 使用 Seam Authentication 和 Prince(我用来打印 pdf 的应用程序)是我的 windows 机器上的安装。有没有办法通过登录或将新的 urlconnection 绑定到经过身份验证的会话。谢谢
    • 我对Seam Authentication不熟悉,所以我不能详细说明,但理论上它很可能需要一个额外的请求cookie。我会监控您的常规网络浏览器(在 Chrome/IE10/Firebug 中按 F12 并检查“Net”/“Network”部分)和网络服务器之间的 HTTP 流量,以查看请求标头中是否没有额外的 cookie。如果是这样,那么您应该在您的编程请求中模拟相同的 cookie。
    • ,在浏览器上(清除缓存并再次登录后),我只有一个 jsessionid cookie,会话存储为空。在 tomcat 管理器中,在 Active HttpSessions 信息下,活动会话具有列名称:Session Id、Guessed Locale、Guessed User name、Creation Time、Last Accessed Time、Used Time、Inactive Time 和 TTL。
    【解决方案2】:

    我不知道如何使用当前用户的会话捕获页面内容,但我可以建议另一种方法 - 您可以将 pdf 转换逻辑移动到 Selenium 测试用例中并使用测试用例进行导航并登录到需要认证的页面。自动tc登录后,您可以调用您的pdf转换逻辑...?

    【讨论】:

      【解决方案3】:

      是的,当然有。您正在发送此内容,因此您拥有它。您应该存储内容对象。如果没有,请检查您的字节流。内容应该在那里;)

      【讨论】:

      • 感谢@Evgheni 和 Stimpson Cat。我更喜欢捕获页面的内容。 Stimpson,您能否告诉我如何访问内容。printPDF() 方法与用于呈现页面的托管 bean 位于不同的托管 bean 中。
      • 对不起。我在 EJB 方面没有太多经验。也许我错了,我监督你正在处理这个糟糕的事情。我不确定,也许托管 Bean 受容器保护?我越想越幸运,我从未使用过 EJB ;)
      【解决方案4】:

      有几个网站允许您将整个页面转换为 pdf 并将其保存为 .pdf 文件。试试这个网站http://pdfcrowd.com/希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 2013-04-17
        • 2011-11-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-06
        • 1970-01-01
        • 2011-05-22
        • 1970-01-01
        相关资源
        最近更新 更多