Jsf 2 JSF-JSF实现如何识别动作?
我试图理解JSF实现如何识别用户的各种可能操作。在我编写的简单应用程序中,我在Jsf 2 JSF-JSF实现如何识别动作?,jsf-2,Jsf 2,我试图理解JSF实现如何识别用户的各种可能操作。在我编写的简单应用程序中,我在login.xhtml页面中配置了以下字段 用户名-输入字段 密码-密码字段 登录按钮 取消按钮 login.xhtml <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="
login.xhtml
页面中配置了以下字段
用户名-输入字段
密码-密码字段
登录按钮
取消按钮
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>
<!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>
<?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>
从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"
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>
<!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>
<?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>
名称
密码
faces config.xml
<!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>
<!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>
<?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>
login.xhtml
/login.xhtml
成功
/welcome.xhtml
login.xhtml
/login.xhtml
失败
/login.xhtml
login.xhtml
/login.xhtml
取消
/login.xhtml
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" />
操作由控件的名称标识,JSF在内部将这些控件映射到bean中的实际方法 ViewState是呈现窗体之前的窗体状态,通过这种方式,可以轻松地将以前的窗体状态与当前窗体状态进行比较(以便触发更改事件等)