gdufo

           

          Appfuse2.x學習-Ajax

          AppFuse ships with a number of Ajax libraries [AppFuse裝載了許多Ajax庫]:

          DWR is Easy Ajax for Java. It allows you to easily expose Java objects (including Spring beans) as JavaScript objects. It's one of the best available libraries for Ajax development. To learn more about DWR and how it works, see Developing AJAX Applications the Easy Way by Joe Walker.

          DWR is Easy Ajax for Java。它允許你很容易地將JAVA對象(包含Spring beans)展現為JavaScript對象。它是最好的Ajax可利用開發庫之一。要對DWR和它的運作機制了解更多,請看Joe Walker寫的Developing AJAX Applications the Easy Way。

          Prototype and Script.aculo.us are both libraries that simplify Ajax development. By using them, you won't have to worry as much about browser compatibility issues.

          Prototype和Script.aculo.us都是能夠簡化Ajax開發的庫。通過使用它們,你不用過多地擔心瀏覽器兼容問題。

          For good documentation about Prototype, see:

          關于Prototype比較好的文檔:

          The best Script.aculo.us documentation is available on the Script.aculo.us Wiki.

          最好的Script.aculo.us文檔可從 Script.aculo.us Wiki上獲得。

          In addition to these libraries, Struts 2 and Tapestry both include Dojo. JSF and Spring MVC do not ship with any built-in Ajax libraries.

          除了這些庫以外,Struts 2 和Tapestry都包含Dojo。JSF和Spring MVC沒有攜帶任何內建的Ajax庫。

          Here is a dojo example using Struts2. There is a normal action with a method save and a set method for the object EvenSpan which holds an id and a name.

          下面是一個使用Struts2的dojo實例。對于一個包含id和名字的對象EvenSpan,有一個帶有save和set方法的正常動作。

          <script type="text/javascript">
          djConfig = { isDebug: false };
          </script>
          <script type="text/javascript" src="../scripts/dojo/dojo.js"></script>

          <script type="text/javascript">
          dojo.require("dojo.event.*");
          dojo.require("dojo.io.*");

          function callback(type, data, evt){
          if (type == 'error'){
          alert('Error when retrieving data from the server!');
          }
          }

          function doOnCellEdit(id,value){
          dojo.io.bind({ url: 'eventSpan.html',
          handler: callback,
          content: {'method:save':true, 'eventSpan.name':value,'eventSpan.id':id }
          });
          return true;
          }
          </script>

          ---------------------------------------------------------------------------------------------------------
          this part is an extension on the following fine tutorial:
          http://cwiki.apache.org/S2WIKI/struts-2-spring-2-jpa-ajax.html
          take care: the tutorial does not do anything apart from demonstrating the
          working of ajax. the java code is prototype only.

          這部分是指南http://cwiki.apache.org/S2WIKI/struts-2-spring-2-jpa-ajax.html的擴充。注意:指南除了闡述ajax的運作方式外不包含其它內容。java 代碼只有prototype。

          this tutorial misses out on 3 things:
          1a) integration it within appfuse setup, taking care of the dojo files
          1b) integration it within appfuse setup, taking care of the importing
          order of the js files
          2) getting it working with the decorator, or better without

          該指南遺漏了三件事:

          1)與appfuse的構建集成,注意dojo文件

          2)與appfuse的構建集成,注意js文件的導入順序

          3)和decorator一起運作,當然,沒有更好。

              a) the dojo files can't be run from the sturts.jar file.

                dojo文件不能在sturts.jar上運行。

          to solve this open the struts2-core-2.0.6.jar
          copy the dojo folder into
          src/main/webapp/struts/dojo

          解決這個問題,需打開struts2-core-2.0.6.jar,將dojo文件夾拷貝到 src/main/webapp/struts/dojo目錄下。

          open your web.xml and make sure the staticFIlter is including the right
          folder:

          打開Web.xml文件,確保staticFIlter包含正確的路徑。

          <filter>
          <filter-name>staticFilter</filter-name>
          <filter-class>org.appfuse.webapp.filter.StaticFilter</filter-class>
          <init-param>
          <param-name>includes</param-name>
          <param-value>/struts/dojo/*</param-value>
          </init-param>
          </filter>

          make sure the dojo files are imported first.

          確保 dojo文件第一個被導入。

          to do this open your /src/main/webapp/decoraters/default.jsp file and
          add this line above! all other js imports:
          <s:head theme="ajax" debug="true"/>

          (otherwise it will conflict with atleast the scriptaculous.js)

          打開/src/main/webapp/decoraters/default.jsp文件,在上面加入這一行導入其它全部js: <s:head theme="ajax" debug="true"/>

          (否則,它至少和scriptaculous.js沖突)

          2) make sure your ajax return string will not be decorated.
          there are many option but i like to do this:
          open your decorators.xml file:

              b)確保ajax返回字符串不會被修飾,要做到這點有很多選擇,但我喜歡這樣做:

          打開decorators.xml文件:

          <decorators defaultdir="/decorators">
          <excludes>
          <pattern>/*ajax=true*</pattern>
          <pattern>/*decorate=false*</pattern>
          <pattern>/struts/dojo/*</pattern> <!-- OK to remove if you're
          not using Struts -->
          <pattern>/resources/*</pattern>
          </excludes>
          <decorator name="default" page="default.jsp">
          <pattern>/*</pattern>
          </decorator>
          </decorators>

          i added the decorate=false part. urls with this parameter will not be
          decorated.

          加入“decorate=false”部分,帶有這一參數的url不會被修飾。

          so an example would be nice right:
          below an ajaxTest.jsp page which will be the caller.
          than an ajaxReturn.jsp page which will be the returned data.
          i expect you can make an AjaxAction with the methods String ajax(),
          getPersons() etc..by your self.
          than this is connected by struts like this:

          舉例說明:下面的ajaxTest.jsp頁是調用頁面,ajaxReturn.jsp是返回頁面。隨意包含一個Ajax操作。像下面這樣通過struts連接。

          <action name="ajax" class="ajaxTest">
          <result>/WEB-INF/pages/employee/ajaxTest.jsp</result>
          <result
          name="ajax">/WEB-INF/pages/employee/ajaxReturn.jsp</result>
          </action>

          the ajaxTest.jsp:

          <%@ taglib prefix="s" uri="/struts-tags"%>
          <head>
          <script type="text/javascript">
          dojo.event.topic.subscribe("/edit", function(data, type,
          request) {
          alert("type: "+type);
          alert("data: "+data);
          if(type="load"){
          document.getElementById("result").innerHTML=data;
          }

          });
          </script>
          </head>

          <!-- URL link to struts action-->
          <s:url id="ajaxText" action="ajax" method="ajax" >
          <s:param name="decorate" value="false" />
          </s:url>

          <!-- Div where content will be displayed -->
          <s:div theme="ajax" id="weather" href="${ajaxText}">
          loading content... from the ajax action the ajax method is called.
          than the ajaxReturn.jsp is rendered here.
          </s:div>


          <p>Persons</p>
          <s:if test="persons.size > 0">
          <table>
          <s:iterator value="persons">
          <tr id="row_<s:property value="id"/>">
          <td>
          <s:property value="firstName" />
          </td>
          <td>
          <s:property value="lastName" />
          </td>
          <td>
          <!-- call the remove method on the ajax action no return-->
          <s:url id="removeUrl" action="ajax" method="remove">
          <s:param name="id" value="id" />
          <s:param name="decorate" value="false" />
          </s:url>
          <s:a href="%{removeUrl}" theme="ajax" >Remove</s:a>

          <!-- call the edit method an the ajax action. the
          result (ajaxResult.jps)
          will be handed to the edit javascript mothed
          attached to dojo (above) -->
          <s:url id="editUrl" action="ajax" method="ajax">
          <s:param name="id" value="id" />
          <s:param name="decorate" value="false" />
          </s:url>
          <s:a href="%{editUrl}" id="a_%{id}" theme="ajax"
          notifyTopics="/edit">Edit</s:a>
          </td>
          <td>
          <a href=ajax!remove.html?id=2>remove me no ajax</a>
          </td>
          </tr>
          </s:iterator>
          </table>
          </s:if>

          <hr>
          <div id=result></div>

          the ajaxResult.jsp:

          <%@ taglib prefix="s" uri="/struts-tags"%>
          Make some nice form here. Now show all persons.
          <s:iterator value="persons">
          <table><tr><td><s:property value="firstName" /></td>
          <td><s:property value="lastName" /></td>
          </tr>
          </table>
          </s:iterator>

          ok here is my action to:

          這是我的做法:

          package nl.incipio.match.webapp.action;

          import java.util.List;

          import org.appfuse.model.User;

          import com.opensymphony.xwork2.Preparable;

          import nl.incipio.match.util.MyBaseAction;

          public class AjaxTestAction extends MyBaseAction implements Preparable {
          private static final long serialVersionUID = 378605805550104346L;

          private List<User> persons;

          private Long id;

          @Override
          public String execute() throws Exception {
          log.debug("just getting the stuf");
          persons = (List<User>) getRequest().getAttribute("persons");
          if (persons == null) {
          log.debug("just ones please");
          persons = userManager.getUsers(null);
          getRequest().setAttribute("persons", persons);
          } else {
          log.debug("persons" + persons.size());
          }
          return SUCCESS;
          }

          public List<User> getPersons() {
          return persons;
          }

          public void setPersons(List<User> persons) {
          this.persons = persons;
          }

          public String remove() throws Exception {
          log.debug("do some removing here when i feel like it id:" + id);
          if (persons != null) {
          log.debug("before persons" + persons.size());
          persons.remove((id.intValue() - 1));
          log.debug("after persons" + persons.size());
          }
          return SUCCESS;
          }

          public String save() throws Exception {
          log.debug("do some saving here when i feel like it");
          return execute();
          }

          public String ajax() {
          log.debug("ajax is doing something id:"+id);
          return "ajax";
          }

          public String edit() throws Exception {
          log.debug("do some editing here when i feel like it");
          return execute();
          }

          public Long getId() {
          return id;
          }

          public void setId(Long id) {
          this.id = id;
          }

          public void prepare() throws Exception {
          log.debug("i'm getting prepared!!!");

          }
          }

          spring config in applicationContext.xml:

          applicationContext.xml文件中的Spring配置:

          <bean id="ajaxTest"
          class="nl.incipio.match.webapp.action.AjaxTestAction">
          <property name="userManager" ref="userManager"/>
          </bean>

          posted on 2008-08-09 08:35 gdufo 閱讀(497) 評論(0)  編輯  收藏 所屬分類: Appfuse

          導航

          統計

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          收藏夾

          Hibernate

          友情鏈接

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 津市市| 乌兰县| 定襄县| 迁安市| 华宁县| 枣庄市| 南城县| 长海县| 灌云县| 津南区| 临桂县| 织金县| 宁德市| 山西省| 汉源县| 大同县| 宜阳县| 凯里市| 禄劝| 莒南县| 博罗县| 板桥市| 汾阳市| 扎鲁特旗| 宜昌市| 大洼县| 芮城县| 巨鹿县| 龙山县| 荥阳市| 勃利县| 阿拉善左旗| 仙桃市| 阿克陶县| 大同市| 中江县| 巴林右旗| 莱阳市| 获嘉县| 浦县| 天全县|