《struts2权威指南》学习笔记之struts2 ajax动态表单

web.xml

 

<? xml version="1.0" encoding="GBK" ?>
< web-app  version ="2.4"  xmlns ="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >

< servlet >

    
< servlet-name > dwr </ servlet-name >
        
< servlet-class > uk.ltd.getahead.dwr.DWRServlet </ servlet-class >
        
< init-param >
           
< param-name > debug </ param-name >
           
< param-value > true </ param-value >
        
</ init-param >
    
</ servlet >

    
< servlet-mapping >
        
< servlet-name > dwr </ servlet-name >
        
< url-pattern > /dwr/* </ url-pattern >
    
</ servlet-mapping >



    
    
< filter >
        
< filter-name > struts2 </ filter-name >
        
< filter-class > org.apache.struts2.dispatcher.FilterDispatcher </ filter-class >
    
</ filter >


    
< filter-mapping >
        
< filter-name > struts2 </ filter-name >
        
< url-pattern > /* </ url-pattern >
    
</ filter-mapping >

</ web-app >

 struts.xml

 

<! DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd"
>

< struts >

    
< constant  name ="struts.custom.i18n.resources"  value ="messageResource" />
    
< constant  name ="struts.i18n.encoding"  value ="GBK" />

    
< package  name ="ajax"  extends ="struts-default" >
        
< action  name ="AjaxTest"  class ="lee.AjaxTestAction" >
            
< result > /AjaxResult.jsp </ result >
        
</ action >
        
< action  name ="Test3" >
            
< result > /testjs.jsp </ result >
        
</ action >
    
</ package >

</ struts >

 

AjaxTest.action

 

package  lee;

import  com.opensymphony.xwork2.Action;

import  java.io.Serializable;


public   class  AjaxTestAction  implements  Action, Serializable
{

    
private static int counter = 0;
    
private String data;

    
public long getServerTime()
    
{
        
return System.currentTimeMillis();
    }


    
public int getCount()
    
{
        
return ++counter;
    }


    
public String getData()
    
{
        
return "服务器提示:" + data;
    }


    
public void setData(String data)
    
{
        
this.data = data;
    }


    
public String execute() throws Exception 
    
{
        
        
return SUCCESS;
    }

}

 

AjaxResult.jsp

 

<% @ page contentType="text/html;charset=GBK" language="java"  %>
<% @ taglib prefix="s" uri="/struts-tags"  %>
<%
    request.setAttribute(
"decorator""none");
    response.setHeader(
"Cache-Control","no-cache"); //HTTP 1.1
    response.setHeader(
"Pragma","no-cache"); //HTTP 1.0
    response.setDateHeader (
"Expires"0); //prevents caching at the proxy server
%>

服务器计数器: 
< s:property  value ="count" />< br >
当前时间是:
< s:property  value ="serverTime" />< br >
服务器返回的提示是:
< s:property  value ="data" />

 

remoteform.jsp

<% @ page contentType="text/html;charset=GBK" language="java"  %>
<% @ taglib prefix="s" uri="/struts-tags"  %>
< html >
< head >
    
< title > 远程表单 </ title >
    
< s:head  theme ="ajax" />
</ head >
< body >
< div  id ='show'  style ="background-color:#bbbbbb;width:360px;height:80px" > 原始静态文本 </ div >
使用表单请求的返回值来填充另一个Div。
< br />
< s:form  id ='theForm1'
        
cssStyle ="border: 1px solid black;"
        action
='AjaxTest'
        
method ='post'
        
theme ="ajax" >
    
< s:textfield  name ='data'  label ="请输入您喜欢的图书" />
    
< s:submit  value ="修改上面的静态文本"  targets ="show" />
</ s:form >
使用表单请求的返回值来填充本Form
< br />
< s:form  id ='theForm2'
        
cssStyle ="border: 1px solid black;"
        action
='AjaxTest'
        
method ='post'
        
theme ="ajax" >
    
< s:textfield  name ='data'  label ="请输入您喜欢的图书" />
    
< s:submit  value ="修改Form本身"  targets ="theForm2" />
</ s:form >
直接运行远程JavaScript(通过指定executeScripts="true")
< br />
< s:form  id ='theForm3'
        
cssStyle ="border: 1px solid black;"
        action
='Test3'
        
method ='post'
        
theme ="ajax" >
    
< s:textfield  name ='data'  label ="请输入您喜欢的图书" />
    
< s:submit  value ="执行远程JS"  executeScripts ="true"  targets ="show" />
</ s:form >
</ body >
</ html >

 

testjs.jsp

 

<% @ page contentType="text/html;charset=GBK" language="java"  %>
<%
    request.setAttribute(
"decorator""none");
    response.setHeader(
"Cache-Control","no-cache"); //HTTP 1.1
    response.setHeader(
"Pragma","no-cache"); //HTTP 1.0
    response.setDateHeader (
"Expires"0); //prevents caching at the proxy server
%>

< script  language ="JavaScript"  type ="text/javascript" >
    alert(
'Spring2.0宝典');
</ script >
轻量级J2EE企业应用实战
< script  language ="JavaScript"  type ="text/javascript" >
    alert(
'基于J2EE的Ajax宝典!');
</ script >

 

住:第二种方式将响应填充form,不知道为什么提交数据后,我们输入的数据data项总是null,是bug?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值