文章出自:www.matrix.com,
用户界面组件与验证
文件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="sie" 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属性。
闽公网安备 35060202000074号