prototype写AJAX例子

本文介绍了一个使用Prototype库实现AJAX请求的具体示例,包括前端JavaScript代码和后端Struts框架处理流程。

最近才发现有这么多写AJAX的框架,在不知道DWR之前
我认为prototype是多么的好用。下面贴一个刚做的例子:

jsp页面代码:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path 
= request.getContextPath();
String basePath 
= request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  
<head>
    
<base href="<%=basePath%>">
    
    
<title>My JSP 'getaccount.jsp' starting page</title>
    
    
<meta http-equiv="pragma" content="no-cache">
    
<meta http-equiv="cache-control" content="no-cache">
    
<meta http-equiv="expires" content="0">    
    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    
<meta http-equiv="description" content="This is my page">
    
<!--
    
<link rel="stylesheet" type="text/css" href="styles.css">
    
-->
        
<script src="<%=request.getContextPath() %>/css/prototype.js"></script>
        
<script>
    
        function searchAccount(uid)
{
        var url 
= 'test.do';
        var pars
="id="+uid;
        var myAjax 
= new Ajax.Request(
        url,
        
{
        method: 
'get',
        parameters: pars,
        onComplete: showResponse
        }
);
        
        }


        function showResponse(originalRequest)
        
{        
        document.getElementById(
"linkid").innerHTML = originalRequest.responseText;
        }

            var sign
=false;
        function show(uid)
{
        
if(sign==false){
        searchAccount(uid);
        sign
=true;
        }
else if(sign==true){
        document.getElementById(
"linkid").innerHTML ="######";
        sign
=false;
        }

        }

        
</script>
    
</head>
    
<body>
        
<html:form action="/showaccount">
            
<span onclick="show('2')" id="linkid" style="cursor:hand">######</span>
            
<html:errors property="id"/><br/>
        
</html:form>
    
</body>
</html>

 

Struts Action中的代码如下:

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 
*/

package com.yourcompany.struts.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.yourcompany.struts.form.TestForm;

/** 
 * MyEclipse Struts
 * Creation date: 11-13-2007
 * 
 * XDoclet definition:
 * @struts.action path="/test" name="testForm" input="/form/test.jsp" scope="request" validate="true"
 
*/

public class TestAction extends Action {
    
/*
     * Generated Methods
     
*/


    
/** 
     * Method execute
     * 
@param mapping
     * 
@param form
     * 
@param request
     * 
@param response
     * 
@return ActionForward
     
*/

    
public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) 
{
        TestForm testForm 
= (TestForm) form;// TODO Auto-generated method stub
        response.setContentType("text/xml;charset=GBK");
        String uid
=request.getParameter("id");
         PrintWriter out
=null;
        
try {
            out 
= response.getWriter();
            
if("1".equals(uid)){
                out.println(
"1234.00");
            }
else if("2".equals(uid)){
                out.println(
"9876.00");
            }

            
        }
 catch (IOException e) {
            
// TODO 自动生成 catch 块
            e.printStackTrace();
        }


        
return null;
    }

}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值