0

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

这就是为什么我在检查用户名和密码时在 goodBye 方法中得到 NullPointerException

登录.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>

登录Bean.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>
4

3 回答 3

0

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

于 2012-05-15T06:05:33.337 回答
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">是“用户名”

于 2012-05-15T06:32:31.107 回答
-1

在 validateLongRange 验证器中发现的问题。您输入了一个字符串,并且必须使用 validateLength。validateLongRange 用于双精度值

希望这个答案是写

于 2014-08-11T17:45:00.863 回答