jsf将一个bean的字符串传递给另一个bean的字符串

问题描述:

在我的jsf 2.0代码中,当你完成一个表单时,它会考虑到你在一个表单中声明的​​所有“错误”值并将其添加到字符串缓冲区当按下命令按钮时,我将它转换为一个字符串。现在我想要的是这个字符串作为一个值到下一个bean,这样在下一个jsf页面上输入的文本区域就可以在那里添加那些信息。但令人遗憾的是,我对如何实现这一目标留下了空白。这是我迄今写下的内容。我没有从网页或豆类获得任何类型的错误,它只是没有显示任何帮助,将不胜感激,并非常好的爱。jsf将一个bean的字符串传递给另一个bean的字符串

public String fillForm(){ 

    boolean failTest = false; 
    String errorCodeForNcpForm = ""; 
    StringBuffer buffer = new StringBuffer(); 

    buffer.append (" "); 

    if (!unitSerialValue.equalsIgnoreCase("P")) 
    { 

     buffer.append (1 + unitSerialValue ); 
     failTest = true; 
     buffer.append(" "); 
    } 

    if(!screenProValue.equalsIgnoreCase("P")) 
    { 

     buffer.append (2 + unitSerialValue );  
     failTest = true; 
     buffer.append(" "); 
    } 

    if(failTest) 
    { 
     //gets the whole error code 
     errorCodeForNcpForm = buffer.toString(); 

     NcpFormBean ncpForm = new NcpFormBean(); 

     //Sets the error code to the value 
     ncpForm.setproblemQcValue(errorCodeForNcpForm); 

     return "ncpFormQc"; 
    } 

    else 
     return "index"; 
} 

这里是包含值的代码的xhtml部分。

<b>Problem Description: </b> 
<h:inputTextarea value="#{ncpFormBean.problemQcValue}" id="problemDec" required="true" cols="30" rows="10"> 
    <f:validateLength minimum="1" maximum="30" /> 
</h:inputTextarea> <br/> 
<h:message for="problemDec" style="color:red" /> 


第二豆“ncpFormBean”没有任何关系,但目前标准的getter和setter,无一不豆会话范围。

在你的第一个bean
+0

什么是您的第一个和第二个bean的范围? –

+0

两者都是会话。发送字符串的第一个bean和接收数据的第二个字符串都是会话。 –

宣布第二豆状:

// change the value as the name of your real managedbean name 
@ManagedProperty(value="#{ncpFormBean}") 
private NcpFormBean ncpForm; 

与它的制定者:

public void setNcpForm(NcpFormBean ncpForm) { 
     this.ncpForm = ncpForm; 
    } 
现在

在fillForm方法:

public String fillForm(){ 
     boolean failTest = false; 
     String errorCodeForNcpForm = ""; 
     StringBuffer buffer = new StringBuffer();   
     buffer.append (" ");   
     if (!unitSerialValue.equalsIgnoreCase("P")){   
      buffer.append (1 + unitSerialValue ); 
      failTest = true; 
      buffer.append(" "); 
     } 

     if(!screenProValue.equalsIgnoreCase("P")){   
      buffer.append (2 + unitSerialValue );  
      failTest = true; 
      buffer.append(" "); 
     } 

     if(failTest){ 
      //gets the whole error code 
      errorCodeForNcpForm = buffer.toString(); 

      this.ncpForm.setproblemQcValue(errorCodeForNcpForm); 

      return "ncpFormQc"; 
     } else 
      return "index"; 
    } 

传递数据现在可以在你的第二个bean的第二个视图中访问(因为你的bean是会话范围的)。

UPDATE:

可以说你有2种豆:Bean1和Bean2(均为sessionScoped):

,如果你想改变值

@ManagedBean(name="bean1") 
@SessionScoped 
public class Bean1 { 
    @ManagedProperty(value="#{bean2}") 
    private Bean2 ncpForm; 
    .... 
} 

..

@ManagedBean(name="bean2") 
@SessionScoped 
public class Bean2 { 
    .... 
    .... 
} 

NOT E:@ManagedProperty(值= “#{bean2}”)这个值/名称必须是完全相同这里的名称@ManagedBean(名称= “bean2”)

现在的XHTML:

bean1 .xhtml

.... 
<h:commandButton action="#{bean1.fillForm}"/> 
.... 
+0

当我添加“this.ncpForm.setProblemQcValue”时,我似乎从我的xhtml文件中收到错误。这是我得到的错误,我将使用xhtml代码更新我的问题。注意我只会在fillform方法中删除清除它时添加该行代码才会出错。 javax.el.E​​LException:/qcForm.xhtml在线路389和柱36动作= “#{qcFormBean.fillForm()}”:显示java.lang.NullPointerException 引起: java.lang中。NullPointerException - /qcForm.xhtml在第389行和第36列动作=“#{qcFormBean.fillForm()}”:java.lang.NullPointerException –

+0

猜我会问有关该错误,但我认为这似乎是有道理的。 –

+0

@GilbertV;你仍然有这个错误?,我会更新我的答案! –