Tapstry 中 异步提交form请求的与页面响应(原创)

在Tapetry中异步提交form标单请求需要用到EventListener方法,要做到这一点其中有几个比较关键的参数:
1:elements(String [] ) 监听对象 即你所监听的dom对象,同属的讲就是你在 jwcid="XX@XX" 中"@"之前的部分。
2:targets(String []) 同上,与之不同的是在使用时该dom对象所在的标单会自动提交
3:events(String []) 监听的促发时间,改参数可以使用两套事件,一套是标准的js事件,另一套是dojo ajax框架自定的事件库。在考虑浏览器兼容的情况下建议使用dojo自定义的事件库。
此外EventListener方法是用注释方法注入到类中的,其做法如下:
@EventListener(param1,param2,...)
public void aaa(){
 .......
}
   当然要实现异步提交表单光靠EventListener方法是不够的,这里要用到一个form的关键属性 ascny 将其设置为 true,同时一般情况下,
对于异步提交的标单不需要listener属性,如果设置了,值得注意的是他的提交顺序,在T4.1.5中他是先提交EventListener所注释的方法,在提交listener所指向的方法。
    在提交表单后如果想异步刷新页面上某部分的内容需要用到的重要对象是ResponseBuilder。该对象可以在IRequestCycle中得到,同 时要注意的是貌似只有T4.1.5之后才能使用该对象。ok如果你将异步提交的标单中的值在页面上另一块地方异步刷新就要用到以下方法
    updateComponent(String compoentId) 你只要将该组件所用到的数据重新设置之后在调用该方法,就可以了。还要提醒大家一点,利用
Tapestry实现异步刷新的时候,对于输出之后的页面上不存在的dom对象是不起作用的。比如你再编写模版的时候用到了if组件,控制了某个
insert组件的现实与否,同时你再类文件中对该insert组件编写了异步
刷新代码,这就有问题了,当不符合if条件是改insert组件的内容(当然包括关键的id)是不会在render之后输出在客户端页面的,因此您之前在类中编写的异步刷新该insert组件的内容是不会起作用的。

以上就是一点工作中累积的小经验,希望对大家有所帮助 副代码如下:
showAccount.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head jwcid="@Shell" title="MTP4-Admin">
    <title>ShowAccount.html</title>
   
    < http-equiv="keywords" content="keyword1,keyword2,keyword3">
    < http-equiv="deion" content="this is my page">
    < http-equiv="content-type" content="text/html; charset=UTF-8">
      <link rel="stylesheet" type="text/css" href="/css/main.css" />
    <link rel="stylesheet" type="text/css" href="/css/table.css" />
    <link rel="stylesheet" type="text/css" href="/css/mtp4.css" />
    < src="/js/js.js" language="java"></>
  </head>
 
  <body jwcid="@Body" ="changeMTP4TableWidth();" οnresize="changePosistionAuto();">
   <div id="bg"></div>
   <div id="leader"><span key="comm_address"></span>:<span key="acc_manager"></span>>><span key="showAccount"></span></div>
   <div id="navigation">
      <ul>
        <li style="background:url(/img/bg_freeItem.jpg);"><a href="#" target="_self" style="color:#FFFFFF;"><span key="showAccount"></span></a></li>
            <li><a jwcid="@DirectLink" listener="listener:linkAMDeposit" target="_self"><span key="showAccount_moneyM"></span></a></li>
            <li><a jwcid="@DirectLink" listener="listener:linkAddNewCurrent"><span key="showAccoun_InstrumentM"></span></a></li>
          </ul>
    </div>
    <div id="searchItem">
    <span key="comm_getItemPlate" style="margin-right: 24px;"></span>
    <input type="button" class="searhButton" value="" ="showMTP4ItemPlate('MTP4_itemPlate_account');"/>
    </div>
    <div id="content">
    <div style="clear: both;">
        <ul class="MTP4_AccountShow_Top" >
            <li style="float: left;" class="MTP4_AccountTitle"><span key="showAcount_accountNum"></span>:</li>
            <li style="float: left;" class="MTP4_AccountShow">
            <span jwcid="saccount@Insert" value="/ognl:showAccount.account" renderTag="true"></span>
            </li>
        </ul>
    </div>
    <table class="MTP4_accountShowTable" cellspacing="0" cellpadding="0">
     <tr>
        <td class="title"><span key="showAccount_Group">组别</span>:</td>
        <td>
        <span jwcid="sgroup@Insert" value="/ognl:showAccount.accountStrategy.groupName" renderTag="true"></span>
        </td>
     </tr>
     <tr>
          <td class="title"><span key="showAccounts_IB">IB</span>:</td>
          <td>
          <span jwcid="sib@Insert" value="/ognl:showAccount.accountStrategy.ibName" renderTag="true"></span>
          </td>
     </tr>
     <tr>
        <td class="title"><span key="showAccount_acName">账户名称</span>:</td>
        <td>
        <span jwcid="sacName@Insert" value="/ognl:showAccount.accountStrategy.acName" renderTag="true"></span>
        </td>
     </tr>
     <tr>
        <td class="title"><span key="showAccount_isTradeAble">是否可交易</span>:</td>
         <td>
         <span jwcid="sTradeable@Any" style="ognl:tradeAbleShow" >
         <span key="comm_yes"></span>
         </span>
         <span jwcid="sdisTradeable@Any" style="ognl:disTradeAbleShow" >
         <span key="comm_on"></span>
         </span>
         </td>
     </tr>
     <tr>
        <td class="title"><span key="showAccount_nople">NOPL</span>:</td>
          <td>
          <span jwcid="sNOPL@Insert" value="/ognl:showAccount.accountStrategy.NOPL" renderTag="true"></span>
          </td>
      </tr>
      <tr>
        <td class="title"><span key="showAccount_basicCurrency">基本货币</span>:</td>
          <td>
          <span jwcid="sbc@Insert" value="/ognl:showAccount.accountStrategy.basicCurrency" renderTag="true"></span>   
          </td>
      </tr>
     <tr>
        <td class="title"><span key="showAccount_balance">平衡方式</span>:</td>
        <td>
        <span jwcid="srealTime@Any" style="ognl:showAccount.realTimeShow" >
         <span key="comm_bw_realTime"></span>
         </span>
         <span jwcid="sdaily@Any" style="ognl:showAccount.dailyShow" >
         <span key="comm_bw_daily"></span>
         </span>
         <span jwcid="sweekly@Any" style="ognl:showAccount.weeklyShow" >
         <span key="comm_bw_weekly"></span>
         </span>
         <span jwcid="smonthly@Any" style="ognl:showAccount.monthlyShow" >
         <span key="comm_bw_monthly"></span>
         </span>
         <span jwcid="snever@Any" style="ognl:showAccount.nerverShow" >
         <span key="comm_bw_weekly"></span>
         </span>
        </td>
     </tr>
      </table>
      <div id="MTP4_itemPlate_account"  style="display: none; z-index: 99; position: absolute; top: 70px; left: 150px;">
    <table id="itemTable" cellpadding="0" cellspacing="1px">
        <tr>
        <td colspan="3" class="toolBar">
        <input type="button" class="closeB" ="closeMTP4ItemPlate('MTP4_itemPlate_account');" ></input>
        </td>
        </tr>
        <tr>
            <td class="title"></td>
            <td class="title"><span key="comm_aeid_search"></span></td>
            <td class="title"><span key="comm_account_search"></span></td>
        </tr>
        <tr>
            <td class="title"><span key="comm_item_search"></span></td>
            <td class="date">
            <form jwcid="aeids@Form" async="true" >
            <input class="itemInput" type="text" jwcid="aeidInput@TextField" value="ognl:searchAeid" ></input>
            <input type="button" style="margin-left: 6px;" jwcid="padAeid@Button" class="searhButton" ></input>
            </form>
            </td>
            <td class="date">
            <form jwcid="accounts@Form" async="true">
            <input class="itemInput" type="text" jwcid="accountInput@TextField" value="ognl:searchAccount" ></input>
            <input type="button" style="margin-left: 6px;" jwcid="padAccount@Button" class="searhButton" ></input>
            </form>
            </td>
        </tr>
        <tr>
            <td class="title"><span key="comm_result_search"></span></td>
            <td class="date">
            <form jwcid="aeid@Form" async="true">
            <select class="itemSelect" multiple="multiple" jwcid="aeidSelect@PropertySelection" model="ognl:userLoginSelections" value="ognl:userLogin">
            </select>
            </form>
            </td>
            <td class="date">
            <form jwcid="account@Form" async="true">
            <select class="itemSelect" multiple="multiple" jwcid="accountSelect@PropertySelection" model="ognl:accountStragetySelections" value="ognl:accountStragety">
            </select>
            </form>
            </td>
        </tr>
        <tr>
            <td colspan="3" style="background-color: #ffffff; text-align:right; height:40px; line-height:40px;">
            <span jwcid="noAcMsg@Any" style="ognl:hasAccountMsg" >
                <span key="showAccount_no_account"></span>
            </span>
            <span jwcid="noUlMsg@Any" style="ognl:hasUserMsg" >
                <span key="showAccount_no_aeid"></span>
            </span>   
            </td>
        </tr>
    </table>
    </div>
    </div>
  </body>
</html>

showAccount.page
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE page-specification PUBLIC
  "-//Apache Software Foundation//Tapestry Specification 4.1//EN"
  "http://jakarta.apache.org/tapestry/dtd/Tapestry_4_1.dtd">
<page-specification class="allone.MTP4.admin.tapestry.page.account.ShowAccount">
<property name="aeIdSelection" />
<property name="showAccount"/>
</page-specification>

showAccount.java
package allone.MTP4.admin.tapestry.page.account;
import java.util.ArrayList;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.annotations.EventListener;
import org.apache.tapestry.annotations.InjectObject;
import org.apache.tapestry.html.BasePage;
import org.apache.tapestry.record.PropertyChangeObserver;

import allone.MTP4.admin.datastructure.AdminAccountStrategy;
import allone.MTP4.admin.datastructure.AdminUserLogin;
import allone.MTP4.admin.tapestry.selectmodels.AccountStrategySelectionModels;
import allone.MTP4.admin.tapestry.selectmodels.UserLoginSelectModels;
import allone.MTP4.admin.web.session.AdminSession;
import allone.MTP4.comm.datastruct.DB.AccountStrategy;
import allone.MTP4.comm.datastruct.DB.UserLogin;


public abstract class ShowAccount extends BasePage  {

    @Override
    public String getClientId() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void setClientId(String arg0) {
        // TODO Auto-generated method stub

    }
   
    @InjectObject("service:tapestry.globals.ServletContext")
    public abstract ServletContext getServletContext();

    @InjectObject("service:tapestry.globals.HttpServletRequest")
    public abstract HttpServletRequest getHttpServletRequest();
   
    public abstract String getSearchAeid();
    public abstract void setSearchAeid(String searchAeid);
    public abstract String getSearchAccount();
    public abstract void setSearchAccount(String searchAccount);
    public abstract UserLoginSelectModels getUserLoginSelections();
    public abstract void setUserLoginSelections(UserLoginSelectModels userLoginSelections);
    public abstract AdminUserLogin getUserLogin();
    public abstract void setUserLogin(AdminUserLogin userLogin);
    public abstract AccountStrategySelectionModels getAccountStragetySelections();
    public abstract void setAccountStragetySelections(AccountStrategySelectionModels asModel);
    public abstract AdminAccountStrategy getAccountStragety();
    public abstract void setAccountStragety(AdminAccountStrategy accountStragety);
    public abstract String getHasAccountMsg();
    public abstract void setHasAccountMsg(String hasAccountMsg);
    public abstract String getHasUserMsg();
    public abstract void setHasUserMsg(String hasUserMsg);
    public abstract AdminAccountStrategy getShowAccount();
    public abstract void setShowAccount(AdminAccountStrategy showAccount);
   
    public PropertyChangeObserver getPropertyChangeObserver() {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public void finishLoad() {
        // TODO Auto-generated method stub
        AdminSession session =AdminSession.getAdminSession( this.getHttpServletRequest().getSession());
        if(session.get_TL_Low_Value("asModel")==null){
            ArrayList<AdminAccountStrategy> adminAccountStrategys = new ArrayList<AdminAccountStrategy>();
            AccountStrategy as = new AccountStrategy();
            as.setAccount(0);
            AdminAccountStrategy aas = new AdminAccountStrategy();
            aas.setAccountStrategy(as);
            adminAccountStrategys.add(aas);
            AccountStrategySelectionModels asModel = new AccountStrategySelectionModels(adminAccountStrategys);
            session.set_TL_Low_Value("asModel", asModel);
            this.setAccountStragetySelections(asModel);
        }else{
            this.setAccountStragetySelections((AccountStrategySelectionModels)session.get_TL_Low_Value("asModel"));
        }
       
        if(session.get_TL_Low_Value("ulModel")==null){
            ArrayList<AdminUserLogin> userLogins = new ArrayList<AdminUserLogin>();
            UserLogin ul = new UserLogin();
            ul.setAeid("");
            AdminUserLogin aul = new AdminUserLogin();
            aul.setUserLogin(ul);
            userLogins.add(aul);
            UserLoginSelectModels ulModels = new UserLoginSelectModels(userLogins);
            session.set_TL_Low_Value("ulModel", ulModels);
            this.setUserLoginSelections(ulModels);
        }else{
            this.setUserLoginSelections((UserLoginSelectModels)session.get_TL_Low_Value("ulModel"));
        }
        AccountStrategy as = new AccountStrategy();
        as.setAccount(0);
        AdminAccountStrategy showAccount = new AdminAccountStrategy();
        showAccount.setAccountStrategy(as);
        this.setShowAccount(showAccount);
        this.setHasAccountMsg("display:none");
        this.setHasUserMsg("display:none");
        //System.out.println("ccc");
    }
   
    public void linkAMDeposit(){
       
    }
   
    public void inkAddNewCurrent(){
       
    }
   
    @EventListener(targets="padAeid",events="")
    public void searchAeIds(IRequestCycle cycle){
        String[] aeids = this.getSearchAeid().split(" ");
        AdminSession adminSession = AdminSession.getAdminSession( this.getHttpServletRequest().getSession());
        if(aeids.length==0){
            return;
        }
        ArrayList<AdminUserLogin> aastrategys = new ArrayList<AdminUserLogin>();
        for (String aeid : aeids) {
       
            try {
                UserLogin ul = new UserLogin();
                ul.setAeid(aeid);
                AdminUserLogin aa = new AdminUserLogin();
                aa.setUserLogin(ul);
                aastrategys.add(aa);
            } catch (RuntimeException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        UserLoginSelectModels ulModel = new UserLoginSelectModels(aastrategys);
        this.setUserLoginSelections(ulModel);
        adminSession.set_TL_Low_Value("ulModel", ulModel);
       
        cycle.getResponseBuilder().updateComponent("aeidSelect");

       
    }
   
    @EventListener(targets="padAccount",events="")
    public void searchAccounts(IRequestCycle cycle){
        String[] aeids = this.getSearchAccount().split(" ");
       
        AdminSession session =AdminSession.getAdminSession( this.getHttpServletRequest().getSession());
        if(aeids.length==0){
            return;
        }
        ArrayList<AdminAccountStrategy> aastrategys = new ArrayList<AdminAccountStrategy>();
        for (String aeid : aeids) {
           
            AccountStrategy ul = new AccountStrategy();
            try {
                ul.setAccount(Long.parseLong(aeid));
                AdminAccountStrategy aa = new AdminAccountStrategy();
                aa.setAccountStrategy(ul);
                aastrategys.add(aa);
            } catch (NumberFormatException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
               
            }
           
        }
        AccountStrategySelectionModels ulModel = new AccountStrategySelectionModels(aastrategys);
        session.set_TL_Low_Value("asModel", ulModel);
        this.setAccountStragetySelections(ulModel);
        cycle.getResponseBuilder().updateComponent("accountSelect");
    }
   
    @EventListener(targets="aeidSelect",events="onchange")
    public void searchAccountByAeId (IRequestCycle cycle){
        AdminUserLogin choseLogin = this.getUserLogin();
        if("".equals(choseLogin.getShowAeId())){
            return;
        }
        AdminSession session =AdminSession.getAdminSession( this.getHttpServletRequest().getSession());
        System.out.println(choseLogin.getUserLogin().getAeid());
        ArrayList<AdminAccountStrategy> aastrategys = new ArrayList<AdminAccountStrategy>();
        for (int i=50; i>0;i--) {
            AccountStrategy ul = new AccountStrategy();
            ul.setAccount(new Long(i));
            AdminAccountStrategy aa = new AdminAccountStrategy();
            aa.setAccountStrategy(ul);
            aastrategys.add(aa);
        }
        AccountStrategySelectionModels ulModel = new AccountStrategySelectionModels(aastrategys);
        session.set_TL_Low_Value("asModel", ulModel);
        this.setAccountStragetySelections(ulModel);
        cycle.getResponseBuilder().updateComponent("accountSelect");
        this.setHasUserMsg("display:block");
        cycle.getResponseBuilder().updateComponent("noUlMsg");
    }
   
    @EventListener(targets="accountSelect",events="onchange")
    public void searchAccountById(IRequestCycle cycle){
//        System.out.println(this.getAccountStragety());
        AdminSession session =AdminSession.getAdminSession( this.getHttpServletRequest().getSession());
        session.set_TL_Low_Value("showAccount", this.getAccountStragety());
        this.setShowAccount(this.getAccountStragety());
        cycle.getResponseBuilder().updateComponent("saccount");
        cycle.getResponseBuilder().updateComponent("sgroup");
        cycle.getResponseBuilder().updateComponent("sib");
        cycle.getResponseBuilder().updateComponent("sacName");
        cycle.getResponseBuilder().updateComponent("sTradeable");
        cycle.getResponseBuilder().updateComponent("sdisTradeable");
        cycle.getResponseBuilder().updateComponent("sNOPL");
        cycle.getResponseBuilder().updateComponent("sbc");
        cycle.getResponseBuilder().updateComponent("srealTime");
        cycle.getResponseBuilder().updateComponent("sdaily");
        cycle.getResponseBuilder().updateComponent("sweekly");
        cycle.getResponseBuilder().updateComponent("smonthly");
        cycle.getResponseBuilder().updateComponent("snever");
    }
   
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值