时间: 2021-07-31 作者:daque
用户界面组件与考证文献edit.jsp中的<h:form>元素包括多个用户界面组件,咱们将会在反面精细地引见。各组件的html代码都是由jsf组件标志爆发,比方:<h:input_textarea>,该标志中大概还会包括其余jsf标志,如:<f:validate_required>标志,该标志使jsf确认用户输出了消息。 处置用户输出的组件运用属性valueref="pbean.property"与javabean属性绑定起来。jsf赢得和树立处置bean属性值已在前方引见了。有的jsf组件标志不会处置任何用户输出。比方<h:output_text>可用来输入文本或javabean只读属性的值。每个组件都有独一的id,id可在id属性中指定或由jsf机动天生。要举行考证的用户界面组件须要id属性再不考证缺点不妨与<h:output_errors for="id"/>一道表露打字与印刷出来。
screen.width-430)this.width=screen.width-430" align=center border=0 dypop="按此在新窗口欣赏图片">图2:考证缺点 文本域text area jsf表单的文本域让用户输出将会由pbuilder.java天生并由view.jsp表露的某些笔墨段落等实质。edit.jsp表露一个由<h:output_text>决定的标签并运用<h:input_textarea>天生3行30列的<textarea>html元素。<f:validate_required>标志备案一个jsf考证器,即使用户在文本域中的输出为空则发堕落误旗号。缺点消息将表露在<h:output_errors>标志的场所,除去表露缺点外该标志不会做其余任何操纵。<h:output_errors>标志的for属性值与<h:input_textarea>的id属性值沟通。<f:use_faces> <h:form formname="pform"> <p><h:output_text value="text:"/><br> <h:input_textarea id="text" valueref="pbean.text" rows="3" cols="30"> <f:validate_required/> </h:input_textarea> <br><h:output_errors for="text"/> .......... </h:form></f:use_faces>上头的jsp代码天生底下的html片断:<form method="post" action="/usingjsf/faces/edit.jsp"> <p>text:<br> <textarea name="text" cols="30" rows="3">javaserver faces</textarea> <br> ..........</form><h:input_textarea>的属性valueref="pbean.text"使jsf搜索id为pbean的javabean范例,而且将用户输出的文本保存到javabean范例的text属性中。当html的表单被天生后,jsf会将text属性值插入到<textarea>html元素中。类pbean实行了get和set本领可让jsf赢得或窜改属性的值:public class pbean implements java.io.serializable { private string text; public string gettext() { return text; } public void settext(string text) { this.text = text; } .......... }除去<h:input_textarea>,jsf还供给了很多天生单列文本域(text field)的标志:<intput_text><h:input_number><input_secret>(暗号输出)<input_date><input_datetime><input_time><input_hidden>可被用来湮没的表单域单列文本域(text field)edit.jsp文献的单列文本域组件只承诺输出1至7之间的数字。由<h:input_number>天生这段html代码,该标志包括两个考证器。<f:validate_required>标志在前方仍旧引见了。<f:validate_longrange>标志是使考证器确认用户输出的数字在给定的范畴之内。即使胜过范畴,则向用户汇报考证缺点,缺点消息由<h:output_errors>爆发。[code]<f:use_faces> <h:form formname="pform"> .......... <p><h:output_text value="size: [1-7]"/><br> <h:input_number id="size" valueref="pbean.size" size="2"> <f:validate_required/> <f:validate_longrange minimum="1" maximum="7"/> </h:input_number> <br><h:output_errors for="size"/> .......... </h:form></f:use_faces>[/code]上头的jsp代码天生底下的html片断:<form method="post" action="/usingjsf/faces/edit.jsp"> .......... <p>size: [1-7]<br> <input type="text" name="size" id="size" value="3" size="2"> <br> ..........</form> 单列文本域被定于size,典型为整形(int)。size中value属性的值(3)是表白所天生的html表单数字输出地区的初值。假如没有展示考证缺点,当jsf收到包括新javabean size属性值的用户输出就会革新javabean。<h:input_number>标志的size属性是控制单列文本域的字符长度(2),不会对javabean属性有其余操纵。public class pbean implements java.io.serializable { .......... private int size; public int getsize() { return size; } public void setsize(int size) { this.size = size; } .......... }除去<f:validate_required>与<f:validate_longrange>标志,jsf还供给了几个考证器标志:<validate_doublerange><validate_stringrange><validate_length><validator> 结果一个为通用标志,不妨用它在用户界面组件中备案你本人的定制考证器。你也不妨创造本人的考证器标志库。 列表list box <h:selectone_listbox>与<h:selectmany_listbox>标志天生html元素<select>,网页欣赏器会将<select>表露为列表。前者承诺用户举行单项采用,后者用来多项采用。 文献edit.jsp运用<h:selectmany_listbox>标志天生一个含有几个字体称呼的列表。html的<option>元素设置列表中的选项,这由jsf标志<h:selectitem>天生:<f:use_faces> <h:form formname="pform"> .......... <p><h:output_text value="font:"/><br> <h:selectmany_listbox id="font" valueref="pbean.font"> <h:selectitem itemvalue="arial" itemlabel="arial"/> <h:selectitem itemvalue="courier new" itemlabel="courier new"/> <h:selectitem itemvalue="times new roman" itemlabel="times new roman"/> </h:selectmany_listbox> .......... </h:form></f:use_faces>上头的代码天生底下的html片断:<form method="post" action="/usingjsf/faces/edit.jsp"> .......... <p>font:<br> <select name="font" multiple size="3"> <option value="arial" selected>arial</option> <option value="courier new" selected>courier new</option> <option value="times new roman">times new roman</option> </select> ..........</form>列表被设置为font,典型为字符串数组(string[])。第一个getfont()本领运用clone()本领复制里面的数组并将其归来,里面数组必需从外部考察中获得养护。第一个setfont()本领用clone()本领复制所供给的数组并生存起来,所供给的数组可被第二个setfont()本领窜改。public class pbean implements java.io.serializable { .......... private string fontarray[]; public string[] getfont() {return (string[]) fontarray.clone();} public void setfont(string fontarray[]) {this.fontarray = (string[]) fontarray.clone();} public string getfont(int index) {return fontarray[index];} public void setfont(int index, string font) {if (fontarray == null)fontarray = new string[index+1];else if (fontarray.length <= index) {string oldfontarray[] = fontarray;fontarray = new string[index+1];for (int i = 0; i < oldfontarray.length; i++)fontarray[i] = oldfontarray[i];}fontarray[index] = font;} .......... }当天生html表单时,jsf将所选的html属性介入到列表选项,列表选项的值被生存在javabean模子的字体数组中。假如没有考证缺点,jsf会在接受到用户新的采用字体的输时髦革新javabean属性。87661JAVA开发精彩教程:JSF系列(二)56855xmlscript.src="http://guide.pconline.com.cn/comment/commentservice_js.jsp?"+(new date()); (根源:www.matrix.com)