【问题标题】:JSF does not populate @Named @RequestScoped bean with submitted input valuesJSF 不使用提交的输入值填充 @Named @RequestScoped bean
【发布时间】:2011-10-19 04:37:49
【问题描述】:

这是我在这个美丽的网站上的第一个问题。我用谷歌搜索了很多,但没有找到任何解决方案。

我是 JSF 新手,我正在通过 Kent Ka lok Tong 的“JSF 2 APIs and JBoss Seam”学习它。

现在我遇到了一个简单的登录实现问题。我有一个登录页面:

<?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">
    <h:head>
        <title>Login</title>
    </h:head>
    <h:body>
        <h1>Login</h1>
        <h:messages for="loginForm" />
        <h:form id="loginForm">
            <h:inputText id="username" value="#{loginRequest.username}" required="true" />
            <h:inputSecret id="password" value="#{loginRequest.password}" required="true" />
            <h:commandButton value="Login" action="#{loginRequest.doLogin}"></h:commandButton>
        </h:form>

    </h:body>
</html>

和一个支持 bean:

package app.controller;

import app.model.beans.User;
import javax.faces.bean.RequestScoped;
import javax.inject.Named;


@Named("loginRequest")
@RequestScoped
public class LoginRequest {

    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 LoginRequest(){
    System.out.println("created " + this.toString());
    }

    public String doLogin(){

        if(this.username != null && this.password != null){

            if(this.username.equals("user") && this.password.equals("password")){
            //this.userHolder.setCurrentUser(username);
            return "success";

            }
        return "failure";

        }
        return "failure";

    }


}

当我运行应用程序时,我的用户名和密码属性结果为空。我调试了我的应用程序,我看到 setters 方法被正确调用。问题是,当调用 setUsername 时,存在 LoginRequest 的实例,而当调用 setPassword 函数时,实例是不同的!似乎应用程序这样做:

obj1 = new LoginRequest() //username and password = null;
obj1.username = username;

obj1 = new LoginRequest() //username and password = null;
obj1.password = password;

obj1 = new LoginRequest() //username and password = null;
obj1.doLogin();

我在哪里遇到了麻烦?哪里错了?

非常感谢!

最好的问候

马可

【问题讨论】:

  • “运行应用程序”是什么意思?你打开登录页面,在文本框中输入你的名字和密码,然后点击登录按钮?
  • 是的,我通过在 glassfish 上部署应用程序来运行该应用程序,然后在浏览器中打开登录页面,输入用户名和密码并按下按钮。

标签: jsf jsf-2 cdi managed-bean


【解决方案1】:

来自你的 bean:

import javax.faces.bean.RequestScoped;
import javax.inject.Named;

@Named("loginRequest")
@RequestScoped
public class LoginRequest {

您正在混合使用 CDI 和 JSF 注释。您可以而且应该这样做。使用其中之一。我不知道这本书告诉你什么,但很可能你在导入 @RequestScoped 注释期间选择了错误的自动完成建议。请注意 IDE 建议的内容是否与书中告诉您的内容相符。

所以,你应该只使用 either CDI 注释

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

@Named("loginRequest")
@RequestScoped
public class LoginRequest {

仅限 JSF 注释

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean(name="loginRequest")
@RequestScoped
public class LoginRequest {

否则,范围默认为"none",并且每个引用 bean 的 EL 表达式都会创建一个全新的单独的 bean 实例。使用三个引用#{loginRequest} 的EL 表达式,您最终会得到3 个实例。一种是设置名称的,一种是设置密码的,一种是调用操作的。


与具体问题无关,托管 bean 名称已经默认为第一个字符小写的类名,符合 Javabean 规范。您可以完全省略 ("loginRequest") 部分。

【讨论】:

  • 我相信命名空间是一样的。 “Current”和“ManagedProperty”注释之间存在相同的区别!这是正确的?然后 not 混合 CDI 和 JSF 注释。非常感谢!
猜你喜欢
  • 2018-06-30
  • 2012-06-15
  • 2013-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多