【问题标题】:jsf passing a string made from one bean to a string in another beanjsf 将由一个 bean 制成的字符串传递给另一个 bean 中的字符串
【发布时间】:2014-03-12 12:18:19
【问题描述】:

在我的 jsf 2.0 代码中,当您完成表单时,它会考虑您在一个表单中声明的​​所有“错误”值,并在按下命令按钮时将其添加到字符串缓冲区,然后我将其转换为字符串。现在我想要的是让这个字符串作为一个值转到下一个 bean,以便下一个 jsf 页面上的文本区域输入中已经添加了该信息。但遗憾的是,我对如何实现这一目标持空白。这是我迄今为止所写的。我没有从页面或 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" />


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

【问题讨论】:

  • 第一个和第二个 bean 的 Scope 是什么?
  • 两者都是会话。发送字符串的第一个 bean 和接收数据的第二个字符串都是会话。

标签: java jsf jsf-2


【解决方案1】:

在你的第一个 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(都是 sessionScoped):

如果你想改变值

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

..

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

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

现在是 xhtml:

bean1.xhtml

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

【讨论】:

  • 当我添加“this.ncpForm.setProblemQcValue”时,我的 xhtml 文件似乎出现错误这是我遇到的错误,我将使用 xhtml 代码更新我的问题。请注意,当我在 fillform 方法中添加该行代码时,我只会收到错误,删除它会清除它。 javax.el.E​​LException: /qcForm.xhtml 在第 389 行和第 36 列 action="#{qcFormBean.fillForm()}": java.lang.NullPointerException 原因:java.lang.NullPointerException - /qcForm.xhtml 在第 389 行和第 36 列 action="#{qcFormBean.fillForm()}": java.lang.NullPointerException
  • 我想我将不得不询问该错误,但我认为这似乎是有道理的。
  • @GilbertV;你还有那个错误吗?,我会更新我的答案!
  • 欢迎您!如果您需要帮助,请写信给我,我们可以在聊天室继续
猜你喜欢
  • 2011-05-21
  • 1970-01-01
  • 2018-12-13
  • 1970-01-01
  • 1970-01-01
  • 2012-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多