如何将变量的值从jsf的bean页面(即bean.java)移动到另一个java类?

发布于 2024-12-09 13:25:37 字数 2196 浏览 5 评论 0原文

如何将变量的值从jsf的bean页面(即bean.java)移动到另一个java类?当我尝试这样做时,分配给第二个java类中变量的值是NULL。

我使用了primefaces UI框架(类似于jsf)并将每个字段值分配给一个bean类。分配给 bean 类中每个变量的值都是正确的。但是当我尝试将这些值移动到另一个 .java 文件时。变量的作用域消失,值为 NULL。看看我的编码..

LOGIN.XHTML

<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.prime.com.tr/ui">


<h:head></h:head>
<h:body>

<p:panel header="Login" style="">
<h:form>

<h:panelGrid columns="2" cellpadding="2">
<h:outputText value="Username"></h:outputText>
<p:inputText id="userName" value="#{loginBean.userName}"></p:inputText>
<h:outputText value="Password"></h:outputText>
<p:password id="password" value="#{loginBean.password}"></p:password>
<p:commandButton value="Sign in" ajax="false" actionListener="#{loginBean.forward}"></p:commandButton>



</h:panelGrid>

</h:form> 

</p:panel>
</h:body>


</html>

loginBean.java

package bean;
import receive.*;
public class loginBean {
public String userName;
public 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 void forward()
{
System.out.println(getUserName());
receiveclass r=new receiveclass();
r.dbc();
}
}

receiveclass.java

package receive;
import bean.loginBean; 
public class receiveclass {
loginBean lb=new loginBean();

public void dbc()
{
  String s= lb.getUserName();
  String p=lb.getPassword();
  System.out.println(s);
  System.out.println(p);
  //System.out.println("hi");

}

}

输出是,

如果我作为管理员,在文本字段中给出管理员

i我以

管理员 身份接收 无效的 空

how to move a variable's value from jsf's bean page (i.e, bean.java) to another java class? when i tried to do that, the value assinged to the variable in the second java class is NULL.,

I have used primefaces UI framework(something like jsf) and assigned every fields value in to a bean class. the value assigned to every variable in bean class is proper. but when i tried to move those values to another .java file. The scope of the variable dies, and the value is NULL. Check out my codings..

LOGIN.XHTML

<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.prime.com.tr/ui">


<h:head></h:head>
<h:body>

<p:panel header="Login" style="">
<h:form>

<h:panelGrid columns="2" cellpadding="2">
<h:outputText value="Username"></h:outputText>
<p:inputText id="userName" value="#{loginBean.userName}"></p:inputText>
<h:outputText value="Password"></h:outputText>
<p:password id="password" value="#{loginBean.password}"></p:password>
<p:commandButton value="Sign in" ajax="false" actionListener="#{loginBean.forward}"></p:commandButton>



</h:panelGrid>

</h:form> 

</p:panel>
</h:body>


</html>

loginBean.java

package bean;
import receive.*;
public class loginBean {
public String userName;
public 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 void forward()
{
System.out.println(getUserName());
receiveclass r=new receiveclass();
r.dbc();
}
}

receiveclass.java

package receive;
import bean.loginBean; 
public class receiveclass {
loginBean lb=new loginBean();

public void dbc()
{
  String s= lb.getUserName();
  String p=lb.getPassword();
  System.out.println(s);
  System.out.println(p);
  //System.out.println("hi");

}

}

output is,

if i give as admin, admin in text fields

i am receiving as

admin
null
null

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拍不死你 2024-12-16 13:25:37

您手动创建 bean,而不是让 JSF 管理 bean。 JSF 根本不会使用手动创建的 bean。您需要让 JSF 自动创建和管理这些 bean。您可以通过将其注入为 < 来访问其他 JSF 托管 Bean code>@ManagedProperty

在您的特定情况下,以下内容应该有效:

@ManagedBean
@RequestScoped
public class LoginBean {

    private String userName;
    private String password;

    @ManagedProperty
    private ReceiveClass receiveClass;

    public void forward() {
        receiveClass.dbc(this);
    }

    // Add/generate getters and setters.
}

使用

@ManagedBean
@SessionScoped
public class ReceiveClass {

    public void dbc(LoginBean loginBean) {
        System.out.println(loginBean.getUserName());
    }

}

(请注意,我修复了代码以遵守 Java 命名约定正确。 /em>

另请参阅:

You're manually creating the beans instead of letting JSF manage the beans. Manually created beans won't be used by JSF at all. You need to let JSF auto-create and manage those beans. You can access other JSF managed beans by injecting it as @ManagedProperty:

In your particular case, the following should work:

@ManagedBean
@RequestScoped
public class LoginBean {

    private String userName;
    private String password;

    @ManagedProperty
    private ReceiveClass receiveClass;

    public void forward() {
        receiveClass.dbc(this);
    }

    // Add/generate getters and setters.
}

with

@ManagedBean
@SessionScoped
public class ReceiveClass {

    public void dbc(LoginBean loginBean) {
        System.out.println(loginBean.getUserName());
    }

}

(Note that I fixed the code to adhere the Java Naming Conventions properly. Class names ought to start with uppercase)

See also:

草莓味的萝莉 2024-12-16 13:25:37

在您的 receiveclass 中,您创建了一个全新的 loginBean 实例。值只能为空。

我这样做:我创建了一个 Java 类,其中有像这样的静态函数。

public class JSFHelper 
{
   public static Object getMyObject(String objname, Class<?> classname )
   {    
      FacesContext fCtx = FacesContext.getCurrentInstance();
      ELContext elCtx = fCtx.getELContext();
      ExpressionFactory ef = fCtx.getApplication().getExpressionFactory();
      ValueExpression ve = 
        ef.createValueExpression(elCtx, "#{" + objname+ "}",classname);
      return (Object) ve.getValue(elCtx);
   }
}

如果我需要来自另一个 Bean 的值,那么在您的 receiveclass 中它将如下所示:

public class receiveclass 
{        

   public void dbc()        
   {          
       loginBean lb=(loginBean)JSFHelper.getMyObject("loginBean",loginBean.class);
       String s= lb.getUserName();          
       String p=lb.getPassword();          
       System.out.println(s);          
       System.out.println(p);          
       //System.out.println("hi");    
   }                
}      

in your receiveclass you create a completely new instance of loginBean. Values can only be null.

I do it this way: I created a Java class in which I have static functions like this one

public class JSFHelper 
{
   public static Object getMyObject(String objname, Class<?> classname )
   {    
      FacesContext fCtx = FacesContext.getCurrentInstance();
      ELContext elCtx = fCtx.getELContext();
      ExpressionFactory ef = fCtx.getApplication().getExpressionFactory();
      ValueExpression ve = 
        ef.createValueExpression(elCtx, "#{" + objname+ "}",classname);
      return (Object) ve.getValue(elCtx);
   }
}

If I need a value from another Bean it would look like this in your receiveclass:

public class receiveclass 
{        

   public void dbc()        
   {          
       loginBean lb=(loginBean)JSFHelper.getMyObject("loginBean",loginBean.class);
       String s= lb.getUserName();          
       String p=lb.getPassword();          
       System.out.println(s);          
       System.out.println(p);          
       //System.out.println("hi");    
   }                
}      
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文