使用dwr我们可以动态请求一个页面内容到当前页:一下是代码:
1,后台方法:
public String getInclude() throws ServletException, IOException {
WebContext ctx = WebContextFactory.get();
System.out.println(ctx.getCurrentPage());
ctx.getHttpServletRequest().setAttribute("test", "hello ,test forward");
return ctx.forwardToString("/iclude.jsp");
}
2,dwr配置:
3,页面:
function getInClude(){
testinclude.getInclude(
function(data){
dwr.util.setValue("fq",data,{ escapeHtml:false });
}
);
}
οnclick="javascript:getInClude()" />
2,其中使用了
WebContext ctx = WebContextFactory.get();
ctx.forwardToString("/iclude.jsp");
查看dwr源代码:
getServletContext().getRequestDispatcher(url).forward(realRequest, fakeResponse);
其中:fakeResponse
HttpServletResponse fakeResponse = new SwallowingHttpServletResponse(realResponse, sout, realResponse.getCharacterEncoding());
而SwallowingHttpServletResponse使用了WriterOutputStream
public SwallowingHttpServletResponse(HttpServletResponse response, Writer sout, String characterEncoding)
{
super(response);
pout = new PrintWriter(sout);
outputStream = new WriterOutputStream(sout, characterEncoding);
this.characterEncoding = characterEncoding;
}
WriterOutputStream:也就是对 ServletOutputStream的封装。
public final class WriterOutputStream extends ServletOutputStream