疯狂

STANDING ON THE SHOULDERS OF GIANTS
posts - 481, comments - 486, trackbacks - 0, articles - 1
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

DWR学习及应用(二,forward)

Posted on 2009-08-25 14:38 疯狂 阅读(750) 评论(0)  编辑  收藏 所属分类: dwr

使用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配置:

 <create javascript="testinclude" creator="new">
<param name="class" value="com.dwr.TestInclude"/>
</create>

 3,页面:

<html>
<head>
<base href="<%=basePath%>">
<script type="text/javascript" src="dwr/engine.js"></script>
<script type="text/javascript" src="dwr/util.js"></script>
<script type="text/javascript" src="dwr/interface/testinclude.js"></script>
<script type="text/javascript">
function getInClude(){
testinclude.getInclude(
function(data){
dwr.util.setValue("fq",data,{ escapeHtml:false });
}
);
}
</script>
</head>
<body>
<input type="button" value="include page"
onclick="javascript:getInClude()" />
<div id="fq">
</div>
</body>
</html>

 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

 




只有注册用户登录后才能发表评论。


网站导航: