【问题标题】:Post processing Wicket response (Rhino, jQuery)后处理 Wicket 响应(Rhino、jQuery)
【发布时间】:2010-12-08 11:06:44
【问题描述】:

我的问题是,是否有办法简单地发布处理检票口 HTML 响应?

我想做的是使用 Rhino (http://www.mozilla.org/rhino/) 和 jQuery 对生成的 HTML 应用一些 DOM 转换。 有人想过吗?有什么建议从哪里开始?

最好, Maciej Wrzalik


好的,我知道了:

public class MyRequestCycle extends WebRequestCycle {
    public MyRequestCycle(WebApplication application, WebRequest request, WebResponse response) {
        super(application, request, response);
    }

    @Override
    protected void onEndRequest() {
        String responseString = response.toString();
        //String newResponseString = process(responseString);
        //replace old response content with the newResponseString 
        super.onEndRequest();
    }
}

onEndRequest 方法中,字符串 responseString 包含 HTML 代码,我将使用 Rhino、Envjs 和 jQuery 以某种方式进行更改,但问题是如何替换旧响应内容与新响应内容?

【问题讨论】:

    标签: javascript jquery wicket rhino


    【解决方案1】:

    Envjs 模拟 Rhino 下的浏览器环境,特别允许您使用 jQuery 在服务器端进行 DOM 操作。我以前在我的项目中使用过它,并且取得了很好的成功。相关资源:

    【讨论】:

      【解决方案2】:

      如果您希望在服务器上完成后处理,最好的办法是实现Servlet Filter,它会在响应发送到客户端之前对其进行修改。

      当您处理呈现的 HTML 时,这与 Wicket 没有什么特别的关系,并且可以应用于任何 Java 框架生成的 html。

      【讨论】:

        【解决方案3】:

        As suggested,一个普通的 Java EE 过滤器可以正常工作,如果没有任何特定于 Wicket 的处理需要。

        但是,如果您想在 Wicket 中执行此操作,出于某种原因,我想您可以创建自己的 RequestCycle 实现 (MyRequestCycle extends WebRequestCycle) 并在那里进行处理(可能通过覆盖 onEndRequest 和/或 getWebResponse)。

        要使用自定义 RequestCycle,请在您的 Application 类中覆盖 newRequestCycle:

        @Override
        public RequestCycle newRequestCycle(Request request, Response response) {
            return new MyRequestCycle(this, (WebRequest) request, response);
        }
        

        我自己在一些事情上使用了自定义的 RequestCycle(例如this)——它简单明了——但我不能 100% 确定它是否符合您的需求。 (我的 Wicket 经验仍然有限。)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-05-15
          • 2019-04-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多