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

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

问题描述

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

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:

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天全站免登陆