posts - 0,comments - 0,trackbacks - 0

          寫了一個簡單至極的JSF頁面,如下:

          <%@ page language="java" pageEncoding="UTF-8"%>
          <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
          <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
          <html>
              <head>
                  <title>Hello</title>
              </head>

              <body>
                  <f:view>
                      <h:form>
                          <h:commandButton actionListener="#{testBean.sayHello}" value="Hello!">
                         
                              <f:setPropertyActionListener target="#{testBean.userName}"
                                  value="zhangsan" />
                                 
                              <f:actionListener type="myex2.lc.MyActionListener"/>
                             
                          </h:commandButton>
                      </h:form>
                  </f:view>
              </body>
          </html>

           

           

          對應(yīng)的Bean如下:

           

          package myex2.lc;

          import javax.faces.event.ActionEvent;

          public class TestBean {
             
              private String userName;

              public String getUserName() {
                  return userName;
              }

              public void setUserName(String userName) {
                  this.userName = userName;
              }   
             
              public void sayHello(ActionEvent evt) {
                  System.out.println("sayWord: Hello, " + userName);
              }
          }

          控制臺上的輸出結(jié)果會是什么呢?答案如下:

          sayWord: Hello, null
          MyActionListener: Hello, zhangsan

           

          為什么兩個ActionListener(sayHello方法和 <f:actionListener type="myex2.lc.MyActionListener"/>)的輸出會不一樣?

          原因在于<f:setPropertyActionListener>也是一個ActionListener,總共3個ActionListener執(zhí)行的順序不同。

           

          正常情況下,attribute中聲明的方法ActionListener,總比<f:actionListener>類的ActionListener先執(zhí)行;

          而后<f:actionListener>類的ActionListener再按聲明的先后順序依次執(zhí)行。


          所以在這個例子里面執(zhí)行的順序是:#{testBean.sayHello} -> <f:setPropertyActionListener> -> <f:actionListener>。

          同時也可看出,對于attribute類的ActionListener,通過<f:setPropertyActionListener>傳遞參數(shù)似乎并不是一個好辦法,還需要構(gòu)建ValueExpression去求值,比較麻煩。

          而可以采用<f:attribute>代替,再用ActionEvent的getComponent() -> getAttributes()解析出參數(shù),相對方便一些。

          action屬性總是在各類ActionListener執(zhí)行之后才被調(diào)用,因此沒有此類干擾。

          以上情況,也可推及ValueChangeListener。



          摘自http://blog.csdn.net/gengv/article/details/4211794

          posted on 2012-12-12 15:52 Kevin_YK 閱讀(351) 評論(0)  編輯  收藏 所屬分類: JSF
          主站蜘蛛池模板: 祁门县| 眉山市| 石阡县| 麻江县| 纳雍县| 清苑县| 勃利县| 西充县| 海口市| 陕西省| 纳雍县| 遂宁市| 宁明县| 仙居县| 靖宇县| 松江区| 突泉县| 中西区| 五莲县| 凉城县| 宣汉县| 常宁市| 浮梁县| 房产| 德保县| 姜堰市| 咸阳市| 威海市| 客服| 永城市| 关岭| 瑞昌市| 衡山县| 贺州市| 渭南市| 蓬溪县| 山阳县| 彭水| 隆尧县| 乌拉特后旗| 若尔盖县|