Stripes使用Ajax

Stripes使用Ajax

最近在看JQuery,又在使用Stripes,于是想把他们结合起来实现Ajax效果

  1. 配置web.xml,导入stripes类库,在目录中加入jquery.js
  2. 在页面中实现/ajax/test1.jsp
     1 <% @ page language="java" contentType="text/html; charset=UTF-8"
     2    pageEncoding="UTF-8"
    %>
     3 <% @ taglib prefix="stripes" uri="http://stripes.sourceforge.net/stripes.tld"  %>
     4 <! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
     5 < html >
     6 < head >
     7 < meta  http-equiv ="Content-Type"  content ="text/html; charset=" UTF-8" >
     8 < title ></ title >
     9 < script  type ="text/javascript"  src ="../skin/js/jquery.js" ></ script >
    10 < script  type ="text/javascript" >
    11   $(document).ready(function(){
    12        alert("f");        
    13        $("#subAjax").click(function(){
    14        name=$("#name").val();
    15        des=$("#des").val();
    16          $.ajax({
    17           type:"post",
    18           url:"${pageContext.request.contextPath}/AjaxTest.action",
    19           data:"name="+name+"&describ="+des,
    20           success:function(result){                
    21                $('<p></p>')
    22               .html(result)
    23                .css('background', '#F0F0F0')
    24                .appendTo("body");                
    25           }

    26          }
    )
    27        return true;
    28        }
    )
    29       
    30   }
    );
    31
    </ script >
    32 </ head >
    33 < body >
    34    < p > 我要测试ajax </ p >
    35    < stripes:form  action ="${pageContext.request.contextPath}/AjaxTest.action" >
    36    < table >
    37     < tr >
    38     < td > 用户名: </ td >< td >   < stripes:text  name ="name"  id ="name" ></ stripes:text ></ td >
    39     </ tr >
    40     < tr >
    41     < td > 说明: </ td >< td >< stripes:text  name ="describ"  id ="des" /></ td >
    42     </ tr >
    43    </ table >  
    44    < stripes:button  name ="subAjax"  id ="subAjax"  value ="提交" ></ stripes:button >   
    45    </ stripes:form >
    46 </ body >
    47 </ html >
  3. 写ActionBean类 AjaxTestActionBean
     1 package  com.test.action;
     2
     3 import  java.io.StringReader;
     4
     5 import  net.sourceforge.stripes.action.ActionBean;
     6 import  net.sourceforge.stripes.action.ActionBeanContext;
     7 import  net.sourceforge.stripes.action.DefaultHandler;
     8 import  net.sourceforge.stripes.action.RedirectResolution;
     9 import  net.sourceforge.stripes.action.Resolution;
    10 import  net.sourceforge.stripes.action.StreamingResolution;
    11
    12 public   class  AjaxTestActionBean  implements  ActionBean  {
    13    private String name;
    14    private String describ; 
    15    private String result;
    16    private ActionBeanContext context;
    17    @DefaultHandler
    18    public Resolution subAjax(){
    19        result="Name is:"+name;
    20        result=result+"<br/>";
    21        result=result+"Description:"+describ;
    22        return new StreamingResolution("text",new StringReader(result));
    23        }

    24    
    25    public String getResult(){
    26        return result;
    27    }
        
    28    public void setName(String name) {
    29        this.name = name;
    30    }
        
    31    public void setDescrib(String describ) {
    32        this.describ = describ;
    33    }

    34    public ActionBeanContext getContext() {        
    35        return context;
    36    }

    37    public void setContext(ActionBeanContext arg0) {
    38        this.context=arg0;        
    39    }

    40    
    41}

    42


    访问页面http://localhost:8080/mytest/ajax/test1.jsp

你可能感兴趣的:(Stripes使用Ajax)