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

时间:2014-02-13 14:41:49

标签: java jsf jsf-2

在我的jsf 2.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都是会话范围。

1 个答案:

答案 0 :(得分:1)

第一个bean中的

声明第二个bean,如:

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

使用它的setter:

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(都是sessionScoped):

如果您想更改值

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

...

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

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

现在是xhtml:

<强> bean1.xhtml

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