我试图了解JSF实现如何标识用户可能采取的各种操作。在我放在一起的简单应用程序中,我在login.xhtml页面中配置了以下字段。
User name - input field
Password - password field
Login button
Cancel button
login.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head id="head_id" ></h:head>
<h:body id="body_id">
<h:form id="loginForm_id">
<h:panelGrid id="loginPanelGrid_id">
<h:outputText id="nameLabel_id" value="Name"></h:outputText>
<h:inputText id="nameInput_id" value="#{loginBean.name}"></h:inputText>
<h:outputText id="passwordLabel_id" value="Password"></h:outputText>
<h:inputSecret id="passwordInput_id" value="#{loginBean.password}"></h:inputSecret>
</h:panelGrid>
<h:commandButton id="loginBtn_id" value="Do Login" action="#{loginBean.login}"></h:commandButton>
<h:commandButton id="CancelBtn_id" value="Cancel Login" action="#{loginBean.cancel}"></h:commandButton>
</h:form>
</h:body>
</html>LoginBean.java
package com.ila;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class LoginBean {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String login() {
if ("a".equals(name)) return "success";
else return "failure";
}
public String cancel() {
return "cancel";
}
}从login.xhtml生成html代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<form id="loginForm_id" name="loginForm_id" method="post"
action="/JSF2TestProject/faces/simple.xhtml"
enctype="application/x-www-form-urlencoded">
<input type="hidden" name="loginForm_id" value="loginForm_id" />
<table id="loginForm_id:loginPanelGrid_id">
<tbody>
<tr>
<td><span id="loginForm_id:nameLabel_id">Name</span></td>
</tr>
<tr>
<td><input id="loginForm_id:nameInput_id" type="text"
name="loginForm_id:nameInput_id" value="a" /></td>
</tr>
<tr>
<td><span id="loginForm_id:passwordLabel_id">Password</span></td>
</tr>
<tr>
<td><input id="loginForm_id:passwordInput_id" type="password"
name="loginForm_id:passwordInput_id" value="" /></td>
</tr>
</tbody>
</table>
<input id="loginForm_id:loginBtn_id" type="submit"
name="loginForm_id:loginBtn_id" value="Do Login" />
<input id="loginForm_id:CancelBtn_id" type="submit"
name="loginForm_id:CancelBtn_id" value="Cancel Login" />
<input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState"
value="H4sIAAAAAAAAAI1QTUoDMRj9Ou1QW2ztDwouBBduVJgD6MIiWCwqFUQRXWicie2UTBKTzHS6KR7BAwhewEuIawUXbryDHsCVSR2nuij4Qd6XfCHvvbz7d7C5FFDtoQg5ofKJs41kdw9xO//28Dh3/pIFqwlFwpDXRK5iogUF1RVYdhnxYr7RAFPT/SmNFb0sBQuEdXzaZCI48701jqTsM+G1KA+VHoQC6qe7IzmCaMdpX/Swq9Zvno/vKnKZWAAx1zy58AqGkDWM6c7mutJTfihg1fDEziVysXRcFnBGMVXOYasdKi23tC8Yx0INdvBAQlI1rSCgPHawRcPg9yVXYEeIhDgSkIuY78G4Yq5VVyapjv44UdQYL3xz+16a+cjCJmMEI/q0KK5fbz8/LMicJB5inhmah2UFJcJcRI7M9AAraaZV4Nz0Wa5Nz//JnaIAp5n/ZPaPVM2maKBkYMZAzUA90Ula/AXEIFMaOgIAAA=="
autocomplete="off" />
</form>
</body>
</html>faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_1.xsd"
version="2.1">
<navigation-rule>
<display-name>login.xhtml</display-name>
<from-view-id>/login.xhtml</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/welcome.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<display-name>login.xhtml</display-name>
<from-view-id>/login.xhtml</from-view-id>
<navigation-case>
<from-outcome>failure</from-outcome>
<to-view-id>/login.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<display-name>login.xhtml</display-name>
<from-view-id>/login.xhtml</from-view-id>
<navigation-case>
<from-outcome>cancel</from-outcome>
<to-view-id>/login.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>UI呈现(屏幕快照)

问题1
考虑到生成的html没有发送任何参数来标识单击哪个按钮(至少我看不到),JSF实现如何确定单击了'Do Login‘按钮还是单击了'Cancel Login’按钮?
问题2
两个隐藏字段(由JSF实现生成)的目的是什么(如下所示)?这些是否与问题1有关?
<input type="hidden" name="loginForm_id" value="loginForm_id" />
<input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState"
value="H4sIAAAAAAAAAI1QTUoDMRj9Ou1QW2ztDwouBBduVJgD6MIiWCwqFUQRXWicie2UTBKTzHS6KR7BAwhewEuIawUXbryDHsCVSR2nuij4Qd6XfCHvvbz7d7C5FFDtoQg5ofKJs41kdw9xO//28Dh3/pIFqwlFwpDXRK5iogUF1RVYdhnxYr7RAFPT/SmNFb0sBQuEdXzaZCI48701jqTsM+G1KA+VHoQC6qe7IzmCaMdpX/Swq9Zvno/vKnKZWAAx1zy58AqGkDWM6c7mutJTfihg1fDEziVysXRcFnBGMVXOYasdKi23tC8Yx0INdvBAQlI1rSCgPHawRcPg9yVXYEeIhDgSkIuY78G4Yq5VVyapjv44UdQYL3xz+16a+cjCJmMEI/q0KK5fbz8/LMicJB5inhmah2UFJcJcRI7M9AAraaZV4Nz0Wa5Nz//JnaIAp5n/ZPaPVM2maKBkYMZAzUA90Ula/AXEIFMaOgIAAA=="
autocomplete="off" />发布于 2013-12-21 17:32:13
这些操作由控件的名称标识,JSF内部将它们映射到bean中的实际方法。
ViewState是呈现表单之前的表单状态,通过这种方式,它很容易将以前的表单状态与当前的表单状态进行比较(以便触发更改事件等等)。
https://stackoverflow.com/questions/20721536
复制相似问题