关于DWR中WebContextFactory的测试(学习中ing~~~~)

来源:互联网 发布:淘宝卖电影票货源 编辑:程序博客网 时间:2024/06/11 02:14
首先是第一个页面aa.jsp:
<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="STYLESHEET" type="text/css"
    href="jspAgent/_css/headagent.css">
<script type='text/javascript' src='/tgtc/dwr/interface/tomajax.js'></script>
<script type='text/javascript' src='/tgtc/dwr/engine.js'></script>
<script type='text/javascript' src='/tgtc/dwr/util.js'></script>
<script type='text/javascript'>
function call(data){
    window.document.write(data);
}
</script>
</head>

<body topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0">
<form method="get" action="bb.jsp"><input type="text" name="hxj"
    value="nihao"> <input type="button" value="nihao"
    onclick="javascript:tomajax.myage('hxj',call)"><br>
<input type="text" id="nn"></form>
</body>
</html>


ajax的java代码:
package hxj.tgtc.actions.dwr;

import javax.servlet.http.HttpServletRequest;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;

public class Test_ajax {

    public Test_ajax() {
    }

    public String myage(String name) {
        WebContextFactory factory = new WebContextFactory();
        WebContext context = factory.get();
        HttpServletRequest httpServletRequest = context.getHttpServletRequest();
        httpServletRequest.setAttribute("hh","Address is haoxiaojie");
        try {
            return context.forwardToString("/bb.jsp");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return "bb.jsp";
        }
    }
} // end_all

 
跳转的页面bb.jsp:
<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Insert title here</title>
<script type='text/javascript' src='/tgtc/dwr/interface/tomajax.js'></script>
<script type='text/javascript' src='/tgtc/dwr/engine.js'></script>
<script type='text/javascript' src='/tgtc/dwr/util.js'></script>
<script type='text/javascript'>
function call(data){
    $('nn').value = data;
}
</script>
</head>
<body>
<%
    System.out.println(request.getAttribute("hh"));
    System.out.println("号小姐");
%>
<input type="text" value="<%=request.getAttribute("hh") %>"><br>
<input type="text" value="123" id="nn"><br>
<input type="button" value="click" onclick="javascript:tomajax.myage('hxj',call);">
</body>
</html>


dwr.xml的配置:
  <create creator="new" javascript="tomajax" scope="session">
            <param name="class" value="hxj.tgtc.actions.dwr.Test_ajax"></param>
            <include method="myage"/>
        </create>


执行的结果: