2012-05-15 35 views
0

我無法獲取我在ManagedBean的xhtml頁面上輸入的用戶名和密碼值。未在Managed Bean中獲取文本框值

這就是爲什麼我在告別方法得到的NullPointerException當我檢查用戶名和密碼

Login.xhtml

<?xml version="1.0" encoding="UTF-8"?> 
<!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:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:p="http://primefaces.org/ui" xmlns:lang="en"> 
<f:view> 
    <head> 
<title>Test Client Login</title> 
    </head> 
    <h:form id="loginForm"> 
     <table align="center" border="1" style="padding-top: 100px"> 
      <tr> 
       <td><h:outputLabel for="username2"> 
         <h:outputText id="usernameLabel" value="Enter Username:" /> 
        </h:outputLabel> 
       </td> 
       <td><h:inputText id="username" value="#{loginBean.username}" 
         required="true"> 
         <f:validateLongRange minimum="1" maximum="500" /> 
        </h:inputText> 
       </td> 
      </tr> 
      <tr> 
      <td> 
       <h:outputLabel for="password2"> 
        <h:outputText id="passwordLabel" value="Enter Password:" /> 
       </h:outputLabel></td> 
       <td><h:inputText id="password" value="#{loginBean.password}" 
        required="true"> 
        <f:validateLongRange minimum="1" maximum="500" /> 
       </h:inputText></td> 
      </tr> 
      <tr> 
      <td colspan="2"> 
       <h:commandButton id="goodbyeCommand" type="submit" value="submit" 
        action="#{loginBean.goodbye}" immediate="true" /> 
      </td> 
      </tr> 
     </table> 
    </h:form> 
</f:view> 


</html> 

LoginBean.java

package com.example.ws.ui; 

import org.apache.log4j.Logger; 

/** 
* Managed Bean for Login 
* 
*/ 
public class LoginBean { 

    private static final Logger logger = Logger.getLogger(LoginBean.class); 


    //EbzWS userManager; 

    private String username; 
    private String password; 
    public String getUsername() { 
     return username; 
    } 
    public void setUsername(String username) { 
     this.username = username; 
    } 
    public String getPassword() { 
     return password; 
    } 
    public void setPassword(String password) { 
     this.password = password; 
    } 
    public String goodbye() { 
    /* try { 

      userManager.getEbzById("asdad", "asdad"); 
      logger.info("method called"); 
     } catch (ServiceException e) { 
      System.out.println(e); 
     }*/ 
     String dest = "failure"; 
     if(username.equals("sunny") && password.equals("sunny")){ 
      dest = "success"; 
     } 
     return dest; 
    } 
    /*public EbzWS getUserManager() { 
     return userManager; 
    } 
    public void setUserManager(EbzWS userManager) { 
     this.userManager = userManager; 
    }*/ 

} 

faces-config.xml

<?xml version="1.0"?> 
<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_1_2.xsd" 
    version="1.2"> 
    <managed-bean> 
     <description>Login Bean</description> 
     <managed-bean-name>loginBean</managed-bean-name> 
     <managed-bean-class>com.example.ws.ui.LoginBean</managed-bean-class> 
     <managed-bean-scope>session</managed-bean-scope> 
    </managed-bean> 
    <managed-bean> 
     <description>EBZ Bean</description> 
     <managed-bean-name>getEbzByIdBean</managed-bean-name> 
     <managed-bean-class>com.example.ws.ui.GetEbzByIdBean</managed-bean-class> 
     <managed-bean-scope>request</managed-bean-scope> 
    </managed-bean> 
    <managed-bean> 
     <description>WsListing Bean</description> 
     <managed-bean-name>wsListingBean</managed-bean-name> 
     <managed-bean-class>com.example.ws.ui.WsListingBean</managed-bean-class> 
     <managed-bean-scope>request</managed-bean-scope> 
    </managed-bean> 
    <navigation-rule> 
     <description>Navigation from the hello page.</description> 
     <from-view-id>/login.xhtml</from-view-id> 
     <navigation-case> 
      <from-outcome>success</from-outcome> 
      <to-view-id>/wsListing.xhtml</to-view-id> 
      <redirect /> 
     </navigation-case> 
     <navigation-case> 
      <from-outcome>failure</from-outcome> 
      <to-view-id>/failure.xhtml</to-view-id> 
      <redirect /> 
     </navigation-case> 
    </navigation-rule> 
    <navigation-rule> 
     <description>Navigation from the hello page.</description> 
     <from-view-id>/wsListing.xhtml</from-view-id> 
     <navigation-case> 
      <from-outcome>ebzService</from-outcome> 
      <to-view-id>/ebzinput.xhtml</to-view-id> 
      <redirect /> 
     </navigation-case> 
     <navigation-case> 
      <from-outcome>filterEbz</from-outcome> 
      <to-view-id>/filterebzinput.xhtml</to-view-id> 
      <redirect /> 
     </navigation-case> 
    </navigation-rule> 


    <navigation-rule> 
     <description>Navigation from the hello page.</description> 
     <from-view-id>/ebzinput.xhtml</from-view-id> 
     <navigation-case> 
      <from-outcome>success</from-outcome> 
      <to-view-id>/ebzoutput.xhtml</to-view-id> 
      <redirect /> 
     </navigation-case> 
    </navigation-rule> 
    <application> 
     <variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver> 
     <locale-config> 
      <default-locale>en</default-locale> 
      <supported-locale>en</supported-locale> 
      <supported-locale>es</supported-locale> 
     </locale-config> 
     <message-bundle>messages</message-bundle> 
    </application> 
</faces-config> 
+1

能告訴我們您正在使用的JSF版本.. ???你也可以展示你如何定義Managed Bean .. ?? – Mango

+0

@Mango請在附件中找到faces-config.xml –

回答

0

immediate="true"你的命令按鈕使JSF跳過所有生命週期階段,直到調用程序。所以你的價值觀沒有設定。刪除此屬性,它應該按預期工作。

+0

我正在使用faces-config.xml,所以我指定了ManagedBean外層。然後它也不能正常工作 –

+0

好的,更新了我的答案。 –

+0

你能詳細解釋一下我還是給我提供一些鏈接,以便我可以閱讀。 –

0

你管理的Bean應以下列方式

<managed-bean> 
    <managed-bean-name>loginBean</managed-bean-name> 
    <managed-bean-class>com.example.ws.ui.LoginBean</managed-bean-class> 
    <managed-bean-scope>view</managed-bean-scope>  
</managed-bean> 

也是在你的XHTML頁面:

<td><h:outputLabel for="username">  
<h:outputText id="usernameLabel" value="Enter Username:" />       </h:outputLabel>      </td>  

爲ID爲><h:inputText id="username" value="#{loginBean.username}" required="true">的 「用戶名」

+0

也嘗試從中刪除type =「submit」和immediate =「true」 – Mango

-1

的問題validateLongRange發現驗證器。你輸入了一個字符串,你必須使用validateLength。 validateLongRange用於雙重價值

希望這個答案寫