未在Managed Bean中获取文本框值

时间:2012-05-15 05:39:52

标签: jsf-2 myfaces

我无法获取我在ManagedBean的xhtml页面上输入的用户名和密码值。

这就是为什么我在检查用户名和密码时在goodBye方法中得到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;
    }*/

}

面-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>

3 个答案:

答案 0 :(得分:0)

命令按钮上的immediate="true"会导致JSF跳过所有生命周期阶段,直到INVOKE APPLICATION。所以你的价值没有设定。删除此属性,它应该按预期工作。

答案 1 :(得分: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>      

><h:inputText id="username" value="#{loginBean.username}" required="true">的ID是“用户名”

答案 2 :(得分:-1)

validateLongRange验证程序中发现的问题。您输入了一个字符串,您必须使用validateLength。 validateLongRange用于双值

希望这个答案是写的