【发布时间】:2013-12-21 17:17:01
【问题描述】:
我试图了解 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 实现如何确定是单击“登录”按钮还是“取消登录”按钮按钮被点击了?
问题 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-2