中,作为下拉列表的选项:
<f:use_faces>
<h:form formName="pform">
..........
<p><h:output_text value="Color:"/><br>
<h:selectone_menu id="color" valueRef="pbean.color">
<f:validate_required/>
<h:selectitem itemvalue="black" itemLabel="Black"/>
<h:selectitem itemvalue="red" itemLabel="Red"/>
<h:selectitem itemvalue="blue" itemLabel="Blue"/>
<h:selectitem itemvalue="green" itemLabel="Green"/>
</h:selectone_menu>
<br><h:output_errors for="color"/>
..........
</h:form>
</f:use_faces>
|
上面的JSP代码生成下面的HTML片断:
<form method="post" action="/usingjsf/faces/edit.jsp">
..........
<p>Color:<br>
<select name="color" size="1">
<option value="black">Black</option>
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="green" selected>Green</option>
</select>
<br>
..........
</form>
|
下拉列表定义为color,类型为字符串(String):
public class PBean implements java.io.Serializable {
..........
private String color;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
..........
}
|
当HTML表单被生成时,JSF将HTML属性selected加入到值与JavaBean模型color属性相同的列表项中。假设没有验证错误,JSF收到包含新颜色值的用户输入后会刷新JavaBean属性。
单选钮(Radio Button)
与标记用于生成一组单选钮:
<f:use_faces>
<h:form formName="pform">
..........
<p><h:output_text value="Alignment:"/><br>
<h:selectone_radio id="align" valueRef="pbean.align"
layout="LINE_DIRECTION">
<f:validate_required/>
<h:selectitem itemvalue="left" itemLabel="Left"/>
<h:selectitem itemvalue="center" itemLabel="Center"/>
<h:selectitem itemvalue="right" itemLabel="Right"/>
</h:selectone_radio>
<br><h:output_errors for="align"/>
..........
</h:form>
</f:use_faces>
|
上面的JSP代码生成如下代码:
<form method="post" action="/usingjsf/faces/edit.jsp">
..........
<p>Alignment:<br>
<table border="0">
<tr>
<td><input type="radio" checked
name="align" value="left"> Left</td>
<td><input type="radio"
name="align" value="center"> Center</td>
<td><input type="radio"
name="align" value="right"> Right</td>
</tr>
</table>
<br>
..........
</form>
|
单选钮定义为align:
public class PBean implements java.io.Serializable {
..........
private String align;
public String getAlign() {
return align;
}
public void setAlign(String align) {
this.align = align;
}
..........
}
|
HTML表单生成时,JSF将HTML属性checked加入到与JavaBean模型的align属性值相同的单选钮中。假如没有验证错误,JSF收到新摆放位置的用户输入时刷新JavaBean属性。
1
2
下一页>>