JSF 不会使用提交的输入值填充 @Named @RequestScoped bean [英] JSF does not populate @Named @RequestScoped bean with submitted input values

查看:20
本文介绍了JSF 不会使用提交的输入值填充 @Named @RequestScoped bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

this is my first question in this beautiful site. I have googled a lot but I didn't find any solution.

我是 JSF 的新手,正在通过 Kent Ka lok Tong 的JSF 2 API 和 JBoss Seam"学习它.

I'm new to JSF and I'm learning it with "JSF 2 APIs and JBoss Seam" by Kent Ka lok Tong.

现在我有一个简单的登录实现问题.我有一个登录页面:

Now I have a problem with a simple login implementation. I have a login page:

<?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:

and a backing 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";

    }


}

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

When I run the application my username and password properties result null. I debugged my app and I saw that setters methods are properly invoked. The problem is that when the setUsername is called there is and instance of the LoginRequest and when it's called the setPassword function the instance is different! Seems that the application do this:

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();

我哪里有问题?错在哪里?

Where I'm in trouble? Where is the mistake?

非常感谢!

最好的问候

马可

推荐答案

来自你的 bean:

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

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

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

You're mixing CDI and JSF annotations. You can and should not do that. Use the one or the other. I don't know what's the book is telling you, but most likely you have chosen the wrong autocomplete suggestion during the import of the @RequestScoped annotation. Please pay attention to if whatever the IDE suggests you matches whatever the book tells you.

因此,您应该仅使用任一 CDI 注释

So, you should be using either CDI annotations only

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 个实例.一种是设置名称,一种是设置密码,一种是调用动作.

Otherwise the scope defaults to "none" and every single EL expression referring the bean would create a brand new and separate instance of the bean. With three EL expressions referring to #{loginRequest} you would end up with 3 instances. One where name is been set, one where password is been set and one where action is been invoked.

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

Unrelated to the concrete problem, the managed bean name already defaults to the classname with 1st character lowercased conform Javabean specification. You could just omit the ("loginRequest") part altogether.

这篇关于JSF 不会使用提交的输入值填充 @Named @RequestScoped bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆