下拉列表(Drop-Down List)
<h:selectone_menu>标记用于生成包含几个颜色选项的下拉列表。
<h:selectitem>标记包含在<h:selectone_menu>中,作为下拉列表的选项:
<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)
<h:selectone_radio>与<h:selectitem>标记用于生成一组单选钮:
<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>
&nbp; <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属性。
复选钮(Checkbox)
文件edit.jsp包含3个由<h:selectboolean_checkbox>标记生成的复选钮。这些界面组件包含在由<h:panel_grid>与<h:panel_group>标记生成的表格(table)中:
<f:use_faces>
<h:form formName="pform">
..........
<p><h:output_text value="Style:"/><br>
<h:panel_grid columns="3" border="0" cellspacing="5">
<h:panel_group>
<h:selectboolean_checkbox id="bold"
valueRef="pbean.bold"/>
<h:output_text value="Bold"/>
</h:panel_group>
<h:panel_group>
<h:selectboolean_checkbox id="italic"
valueRef="pbean.italic"/>
<h:output_text value="Italic"/>
</h:panel_group>
<h:panel_group>
<h:selectboolean_checkbox id="underline"
valueRef="pbean.underline"/>
<h:output_text value="Underline"/>
</h:panel_group>
</h:panel_grid>
..........
</h:form>
</f:use_faces>
上面代码生成如下片断:
<form method="post" action="/usingjsf/faces/edit.jsp">
..........
<p>Style:<br>
<table border="0" cellspacing="5">
<tr>
<td><input type="checkbox"
name="bold">Bold</td>
<td><input type="checkbox"
name="italic" checked>Italic</td>
<td><input type="checkbox"
name="underline">Underline</td>
</tr>
</table>
..........
</form>
3个复选钮分别与类型为布尔(Boolean)的bold、italic、underline绑定在一起:
public class PBean implements java.io.Serializable {
..........
private boolean bold;
public boolean isBold() {
return bold;
}
public void setBold(boolean bold) {
this.bold = bold;
}
private boolean italic;
public boolean isItalic() {
return italic;
}
public void setItalic(boolean italic) {
this.italic = italic;
}
private boolean underline;
public boolean isUnderline() {
return underline;
}
public void setUnderline(boolean underline) {
this.underline = underline;
}
..........
}
生成HTML表单时,JSF将checked属性加入到每一个JavaBean属性为真(true)的复选钮中。假如没有验证错误,JSF收到用户输入后就会刷新JavaBean属性。
在本例中,复选钮是分别独立生成的。JSF也提供了<h:selectmany_checkboxlist>标记用于生成一组复选钮。还提供了<h:panel_list>和<h:panel_data>两个标记用于从集合与数组中生成表格。
命令按钮(Command Buttons)
文件faces-config.xml定义了导航规则,决定JSF在用户点击网页中的命令按钮时做什么,网页的路径由<from-tree-id>标记(/edit.jsp)指定。由<navigation-case>元素分别定义了两个导航块(navigation case):
..........
<faces-config>
<navigation-rule>
<from-tree-id>/edit.jsp</from-tree-id>
<navigation-case>
<from-outcome>editOutcome</from-outcome>
<to-tree-id>/edit.jsp</to-tree-id>
</navigation-case>
<navigation-case>
<from-outcome>viewOutcome</from-outcome>
<to-tree-id>/view.jsp</to-tree-id>
</navigation-case>
</navigation-rule>
..........
</faces-config>
文件edit.jsp包含由标记<h:command_button>生成的两个按钮。每个都有一个标识(ID)、一个标签、一个命令名称(这里没有使用,但JSF需要)以及一个action或actionRef属性:
<f:use_faces>
<h:form formName="pform">
..........
<p>
<h:command_button id="view" label="View"
commandName="viewCmd" action="viewOutcome"/>
<h:command_button id="boldUpperCase"
label="Bold Upper Case / View"
commandName="boldUpperCaseCmd"
actionRef="pbean.boldUpperCaseAction"/>
</h:form>
</f:use_faces>
上面JSP代码生成如下HTML片断:
<form method="post" action="/usingjsf/faces/edit.jsp">
..........
<p>
<input type="submit" name="view" value="View">
<input type="submit" name="boldUpperCase"
value="Bold Upper Case / View">
</form>
JSF会在每次浏览器提交用户输入时验证表单中的数据。如果验证器没有发出错误信号而且没有类型转换错误,JSF便会分析导航块(navigation case)。对于第一个按钮,JSF会得到action属性的值viewOutcome,该值与第二个导航块的<from-outcome>元素中的文本匹配。因此,JSF将HTTP请求转发给view.jsp,文件view.jsp的路径包含在第二个导航块的<to-tree-id>元素中。
当用户点击第二个按钮时,JSF则调用PBean对象的getBoldUpperCaseAction()方法。该方法返回一个BoldUpperCaseAction的实例,BoldUpperCaseAction则是PBean的内部类。接着,JSF调用invoke()方法,该方法返回一个在运行时决定的结果而不是固定不变的HTML文件:
public class PBean implements java.io.erializable {
..........
public BoldUpperCaseAction getBoldUpperCaseAction() {
return new BoldUpperCaseAction();
}
public class BoldUpperCaseAction
extends javax.faces.application.Action {
public String invoke() {
String ucText = getText().toUpperCase();
if (isBold() && getText().equals(ucText))
return "viewOutcome";
else {
setBold(true);
setText(ucText);
return "editOutcome";
}
}
}
}
如果bold(粗体)属性的值为true(真)并且文本的所有字符为大写的,JSF就按照第二个导航块中的定义,与另一个按钮情况一样JSF将HTTP请求转发给view.jsp。另外,invoke()方法会将bold属性设为true,并将文本的所有字符改为大写的,最后返回字符串editOutcome,使JSF按照第一个导航块的定义,保持edit.jsp为当前页。
文章来源:www.Matrix.org.cn,转载请与原作者联系
|