Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java jsf将一个bean中的字符串传递给另一个bean中的字符串_Java_Jsf_Jsf 2 - Fatal编程技术网

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

Java jsf将一个bean中的字符串传递给另一个bean中的字符串,java,jsf,jsf-2,Java,Jsf,Jsf 2,在我的JSF2.0代码中,当您完成一个表单时,它会考虑您在一个表单中声明的所有“错误”值,并在按下commandbutton时将其添加到字符串缓冲区,然后我将其转换为字符串。现在我想让这个字符串作为一个值转到下一个bean,以便下一个jsf页面上的文本区域输入已经添加了该信息。但可悲的是,我在如何实现这一点上一片空白。这是我迄今为止写下的。我没有收到任何错误,无论是从网页或豆类它只是什么都没有显示任何帮助将被感谢和非常好的爱 public String fillForm(){ bool

在我的JSF2.0代码中,当您完成一个表单时,它会考虑您在一个表单中声明的所有“错误”值,并在按下commandbutton时将其添加到字符串缓冲区,然后我将其转换为字符串。现在我想让这个字符串作为一个值转到下一个bean,以便下一个jsf页面上的文本区域输入已经添加了该信息。但可悲的是,我在如何实现这一点上一片空白。这是我迄今为止写下的。我没有收到任何错误,无论是从网页或豆类它只是什么都没有显示任何帮助将被感谢和非常好的爱

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" />
问题描述:



第二个bean“ncpFormBean”目前只有标准的getter和setter,这两个bean都是会话范围。

在第一个bean中声明第二个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是会话范围的)

更新:

假设您有2个bean:Bean1和Bean2(都是会话范围):

如果要更改值

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

注意:@ManagedProperty(value=“#{bean2}”)此值/名称必须与此处的名称@ManagedBean(name=“bean2”)完全相同。

现在是xhtml:

bean1.xhtml

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

第一个和第二个bean的范围是什么?都是会话。发送字符串的第一个bean和接收数据的第二个字符串都是会话。当我添加“this.ncpForm.setProblemQcValue”时,我似乎从我的xhtml文件中得到了一个错误。这是我得到的错误。我将用xhtml代码更新我的问题。注意,只有当我在fillform方法中添加那行代码时,我才会得到这个错误。javax.el.ELException:/qcForm.xhtml第389行和第36列action=“#{qcFormBean.fillForm()}”:java.lang.NullPointerException引起的异常:java.lang.NullPointerException-/qcForm.xhtml第389行和第36列action=“#{qcFormBean.fillForm()}”:java.lang.NullPointerException我想我得问问这个错误,但我认为这似乎是有道理的。@GilbertV;你还有那个错误吗?我会更新我的答案!不客气!。如果你需要帮助,写信给我,我们可以在聊天室继续让我们
....
<h:commandButton action="#{bean1.fillForm}"/>
....