JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java 程式分享區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
作者 Facelets 下客製 input radio 元件
mylee





發文: 764
積分: 1
於 2009-10-16 12:53 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
JSF 內建的 SelectOneRadio 雖可滿足八九成的應用,但在一些特殊情形下,需要把同一組
的數個 input radio 分散在不同的 component 下,例如(用 dataGrid 的每一格中一個),那
就力有未逮了。這時候自製元件是比較好的做法,以下分享在 facelets 下的作法。
以上以儘量力求正確,若有照著做發生問題者,請在本篇下回文或是本站悄悄話皆可。

Step1. web.xml 加一段 <context-param>

1
2
3
4
5
6
  <context-param>
      <param-name>facelets.LIBRARIES</param-name>
      <param-value>
          /META-INF/custom.taglib.xml
      </param-value>
  </context-param>


Step2. 在 META-INF 下建立 custom.taglib.xml

1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE facelet-taglib PUBLIC "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN" "facelet-taglib_1_0.dtd">
<facelet-taglib>
    <namespace>http://idv.mylee.lib/component</namespace>
    <tag>
        <tag-name>InputRadio</tag-name>
        <component>
            <component-type>component.inputRadio</component-type>
        </component>
    </tag>
</facelet-taglib>


Step3. faces-config.xml 加一段 <component>

1
2
3
4
    <component>
        <component-type>component.inputRadio</component-type>
        <component-class>idv.mylee.lib.component.UIInputRadio</component-class>
    </component>


Step4. 建立 source code (pageckage idv.mylee.lib.component) UIInputRadio.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package idv.mylee.lib.component;
 
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Map;
 
import javax.el.ValueExpression;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
 
/**
 * UIInputRadio
 * @author mylee@javaworld
 * Licence: GPL V2
 */
 
public class UIInputRadio extends UIInput {
 
    private String name = null;
    private String styleClass = null;
    private String style = null;
    private String disabled = null;
    private String itemLabel = null;
    private String itemValue = null;
    private String onClick = null;
    private String onMouseOver = null;
    private String onMouseOut = null;
    private String onFocus = null;
    private String onBlur = null;
 
    @Override
    public String getFamily() {
        return "component.inputRadio";
    }
 
    @Override
    public void decode(FacesContext context) {
        if ((context == null))
            throw new NullPointerException();
        Map map = context.getExternalContext().getRequestParameterMap();
        String name = getName();
        if (map.containsKey(name) ) {
            String value = (String)map.get(name);
            if (value!=null)
                 setSubmittedValue(value);
        }
    }
 
//    @Override
//    public void encodeBegin(FacesContext context) throws IOException {
//      super.encodeBegin(context);
//    }
    
//    @Override
//    public void encodeChildren(FacesContext context) throws IOException {
//      super.encodeChildren(context);
//    }
//    
    @Override
    public void encodeEnd(FacesContext context) throws IOException {
      if (context == null)
            throw new NullPointerException();
 
      String clientId = getClientId(context);
      if (isRendered()) {         
            ResponseWriter writer = context.getResponseWriter();
            writer.startElement("input", null);
            writer.writeAttribute("type", "radio", null);
            writer.writeAttribute("id", clientId, null);
            writer.writeAttribute("name", getName(), null);
            writeOptionalAttribute(writer, "class", getStyleClass());
            writeOptionalAttribute(writer, "style", getStyle());
            writeOptionalAttribute(writer, "disabled", getDisabled());
            writeOptionalAttribute(writer, "value", getItemValue());
            writeOptionalAttribute(writer, "onclick", getOnClick());
            writeOptionalAttribute(writer, "onmouseover", getOnMouseOver());
            writeOptionalAttribute(writer, "onmouseout", getOnMouseOut());
            writeOptionalAttribute(writer, "onfocus", getOnFocus());
            writeOptionalAttribute(writer, "onblur", getOnBlur());
            if (getValue()!=null && getValue().equals(getItemValue()))
                writer.writeAttribute("checked", "checked", null);
            writer.endElement("input");
            if (getItemLabel()!= null) {
                writer.startElement("label", null);
                writer.writeAttribute("for", clientId, null);
                writer.writeText(getItemLabel(), null);
                writer.endElement("label");
            }
      }
    }
    
    public String getName() {
        return getAttribute("name", name);
    }
 
    public void setName(String name) {
       this.name = name;
    }
 
    public String getStyleClass() {
        return getAttribute("styleClass", styleClass);
    }
 
    public void setStyleClass(String styleClass) {
        this.styleClass = styleClass;
    }
 
    public String getStyle() {
        return getAttribute("style", style);
    }
 
    public void setStyle(String style) {
        this.style = style;
    }
 
    public String getDisabled() {
        return getAttribute("disabled", disabled);
    }
 
    public void setDisabled(String disabled) {
        this.disabled = disabled;
    }
 
    public String getItemLabel() {
        return getAttribute("itemLabel", itemLabel);
    }
 
    public void setItemLabel(String itemLabel) {
        this.itemLabel = itemLabel;
    }
 
    public String getItemValue() {
        return getAttribute("itemValue", itemValue);
    }
 
    public void setItemValue(String itemValue) {
        this.itemValue = itemValue;
    }
 
    public String getOnClick() {
        return getAttribute("onClick", onClick);
    }
 
    public void setOnClick(String onClick) {
        this.onClick = onClick;
    }
 
    public String getOnMouseOver() {
        return getAttribute("onMouseOver", onMouseOver);
    }
 
    public void setOnMouseOver(String onMouseOver) {
        this.onMouseOver = onMouseOver;
    }
 
    public String getOnMouseOut() {
        return getAttribute("onMouseOut", onMouseOut);
    }
 
    public void setOnMouseOut(String onMouseOut) {
        this.onMouseOut = onMouseOut;
    }
 
    public String getOnFocus() {
        return getAttribute("onFocus", onFocus);
    }
 
    public void setOnFocus(String onFocus) {
        this.onFocus = onFocus;
    }
 
    public String getOnBlur() {
        return getAttribute("onBlur", onBlur);
    }
 
    public void setOnBlur(String onBlur) {
        this.onBlur = onBlur;
    }
 
    public Object saveState(FacesContext context) {
        Object[] values = new Object[12];
        values[0] = super.saveState(context);
        values[1] = styleClass;
        values[2] = style;
        values[3] = disabled;
        values[4] = itemLabel;
        values[5] = itemValue;
        values[6] = onClick;
        values[7] = onMouseOver;
        values[8] = onMouseOut;
        values[9] = onFocus;
        values[11] = name;
        return (values);
    }
 
    public void restoreState(FacesContext context, Object state) {
        Object[] values = (Object[]) state;
        super.restoreState(context, values[0]);
        styleClass = (String) values[1];
        style = (String) values[2];
        disabled = (String) values[3];
        itemLabel = (String) values[4];
        itemValue = (String) values[5];
        onClick = (String) values[6];
        onMouseOver = (String) values[7];
        onMouseOut = (String) values[8];
        onFocus = (String) values[9];
        onBlur = (String) values[10];
        name = (String) values[11];
    }
    
    protected String getAttribute(String attributeName, String attributeValue) {
        return getAttribute(attributeName, attributeValue, false);
    }
    
    protected String getAttribute(String attributeName, String attributeValue, boolean required) {
        if (null!=attributeValue){
            return attributeValue;
        }
        ValueExpression ve = getValueExpression(attributeName);
        if (ve == null) {
          if (required)
            throw new javax.el.ELException("Value for InputRadio Tag: attribute [" + attributeName + "] is required");
          else
            return null;
        }
        return (String) ve.getValue(getFacesContext().getELContext());
    }
    
    protected void writeOptionalAttribute(ResponseWriter writer, String attributeName, String attributeValue) throws IOException {
        if (attributeValue!= null) {
          attributeValue = attributeValue.trim();
          if (attributeValue.length()>0)
            writer.writeAttribute(attributeName, attributeValue, null); 
        } 
    }
}


Step final. 在 (facelet ) JSF 檔案中使用此 component,首先是在適當位置加上 xmlns

1
    xmlns:comp="http://idv.mylee.lib/component"


最後使用如下:

1
2
3
4
      <comp:InputRadio name="rv" value="#{yourbackbean.yourProperty}" itemLabel="#{'30天內'}" itemValue="1m"/>
      <comp:InputRadio name="rv" value="#{yourbackbean.yourProperty}" itemLabel="#{'90天內'}" itemValue="3m"/>
      <comp:InputRadio name="rv" value="#{yourbackbean.yourProperty}" itemLabel="#{'1年內'}" itemValue="1y"/>
      <comp:InputRadio name="rv" value="#{yourbackbean.yourProperty}" itemLabel="#{'全部'}" itemValue="all"/>


mylee edited on 2009-10-16 13:40
reply to postreply to post
» JWorld@TW »  Java 程式分享區

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8