爪哇一角

          共同探討STRUTS#HIBERNATE#SPRING#EJB等技術
          posts - 3, comments - 6, trackbacks - 0, articles - 99
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          日歷

          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          隨筆檔案

          搜索

          •  

          最新評論

          十 自定義UIComponent

          Posted on 2009-01-20 14:50 非洲小白臉 閱讀(234) 評論(0)  編輯  收藏 所屬分類: JSF

          這里介紹的是讓Component自身負責編碼,解碼的例子。

          我們設置一個簡單的組件,一個文本框加一個按鈕。

          這里我們可以繼承已經實現的UIComponent組件UIInput

          package test;

          import java.io.IOException;

          import java.util.Map;

          import javax.faces.component.UIInput;

          import javax.faces.context.FacesContext;

          import javax.faces.context.ResponseWriter;

          繼承已經實現UIComponentUIInput

           
          public class UITextWithCmd extends UIInput {

          private static final String TEXT = ".text";

              private static final String CMD = ".cmd";

              //不使用Renderer進行解碼

          可以設定此組件的解碼,編碼不使用Renderer

           
              public UITextWithCmd() {

                  setRendererType(null); 

              }

              //--------編碼,生成HTML標簽--------------

              public void encodeBegin(FacesContext context)

          實現前臺表現層的編碼。可以編寫成HTML或者其他表現層語言。

           
                                                throws IOException {

                  ResponseWriter writer = context.getResponseWriter();

                  String clientId = getClientId(context);

                

                  encodeTextField(writer, clientId);

                  encodeCommand(writer, clientId);

              }

              private void encodeTextField(ResponseWriter writer,

                                  String clientId) throws IOException {

                  writer.startElement("input", this);

                  writer.writeAttribute("name", clientId + TEXT, null);

                

                  //getValue()繼承于UIInput

                  Object value = getValue();

                  if(value != null) {

                      writer.writeAttribute("value",

                                            value.toString(), null);

                  }

                  String size = (String) getAttributes().get("size");

                  if(size != null) {

                      writer.writeAttribute("size", size, null); 

                  }

                  writer.endElement("input");

              }

              private void encodeCommand(ResponseWriter writer,

                                  String clientId) throws IOException {

                  writer.startElement("input", this);

                  writer.writeAttribute("type", "submit", null);

                  writer.writeAttribute("name", clientId + CMD, null);

                  writer.writeAttribute("value", "submit", null);

                  writer.endElement("input");

              }

          實現標簽輸入值的解碼。取得標簽的輸入值綁定到響應的Bean

           
              // ..........解碼,取得標簽的輸入值...................................

              public void decode(FacesContext context) {

                  Map reqParaMap = context.getExternalContext().

                                          getRequestParameterMap();

                  String clientId = getClientId(context);

                  String submittedValue =

                             (String) reqParaMap.get(clientId + TEXT);

                  setSubmittedValue(submittedValue);

                  setValid(true);

              }

          }

          在配置文件faces-config.xml中聲明自定義組件。

          <!-- 自定義組件 -->

           <component>

               <component-type>test.TextWithCmd</component-type>

               <component-class>test.UITextWithCmd</component-class>

           </component>

          這樣,自定義的UIComponent組件就完成了。

           

          主站蜘蛛池模板: 抚宁县| 揭西县| 肇源县| 伊吾县| 启东市| 宜君县| 石楼县| 凌云县| 长春市| 衡阳市| 中西区| 当阳市| 静宁县| 漾濞| 松阳县| 武城县| 英吉沙县| 科技| 静宁县| 德钦县| 枣阳市| 新巴尔虎右旗| 安义县| 阿巴嘎旗| 海原县| 蒙阴县| 翼城县| 浦县| 青田县| 康定县| 五常市| 治多县| 平潭县| 山丹县| 和平区| 五华县| 任丘市| 女性| 娱乐| 大连市| 静乐县|