2013-12-21 61 views
2

我想了解JSF實現如何標識來自用戶的各種可能的操作。在我放在一起的簡單應用程序中,我在login.xhtml頁面中配置了以下字段。JSF - JSF實現如何識別操作?

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呈現(截屏)

enter image description here

問題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" /> 

回答

1

這些操作由控件的名稱標識,JSF內部將這些操作映射到bean中的實際方法。

ViewState是呈現之前窗體的狀態,這樣它可以很容易地將先前窗體狀態與當前窗體狀態進行比較(以便觸發更改事件等)。