简体   繁体   English

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

[英]how to move a variable's value from jsf's bean page (i.e, bean.java) to another java class?

how to move a variable's value from jsf's bean page (ie, bean.java) to another java class? 如何将变量的值从jsf的bean页面(即bean.java)移动到另一个Java类? when i tried to do that, the value assinged to the variable in the second java class is NULL., 当我尝试执行此操作时,分配给第二个Java类中的变量的值为NULL。

I have used primefaces UI framework(something like jsf) and assigned every fields value in to a bean class. 我已经使用了primefaces UI框架(类似于jsf),并将每个字段值分配给了一个bean类。 the value assigned to every variable in bean class is proper. 分配给bean类中每个变量的值是正确的。 but when i tried to move those values to another .java file. 但是当我尝试将这些值移动到另一个.java文件时。 The scope of the variable dies, and the value is NULL. 变量的作用域消失,值为NULL。 Check out my codings.. 看看我的编码..

LOGIN.XHTML 登录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 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 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 如果我以管理员身份在文本字段中输入admin

i am receiving as 我收到

admin null null 管理员null null

You're manually creating the beans instead of letting JSF manage the beans. 您是在手动创建bean,而不是让JSF管理bean。 Manually created beans won't be used by JSF at all. 手动创建的bean完全不会被JSF使用。 You need to let JSF auto-create and manage those beans. 您需要让JSF自动创建和管理那些bean。 You can access other JSF managed beans by injecting it as @ManagedProperty : 您可以通过将其注入@ManagedProperty来访问其他JSF托管的bean:

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) (请注意,我已修复代码以正确遵守Java命名约定 。类名应以大写字母开头)

See also: 也可以看看:

in your receiveclass you create a completely new instance of loginBean. 在您的receiveclass中,您将创建一个全新的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 我这样做:我创建了一个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);
   }
}

If I need a value from another Bean it would look like this in your receiveclass: 如果我需要另一个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");    
   }                
}      

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM