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

首先是第一个页面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"
    οnclick="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" οnclick="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>


执行的结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值