简体   繁体   中英

jsf passing a string made from one bean to a string in another bean

In my jsf 2.0 code when you complete a form it takes account of all the "error" values you your stated in one form and adds it to a string buffer when the commandbutton is pressed then i covert it to a string. Now what i want is for this string to go to the next bean as a value so that the text area input on the next jsf page would have that information already added there. But sadly I am drawing a blank on how i would like to make this happen Here is what i have written down so far. I am not getting any kind of error from either pages or beans its just nothing is showing up any help would be appreciated and very well love.

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";
}

here is the xhtml section that contains the code for the value.

 <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" />


The second bean "ncpFormBean" has nothing currently but the standard getters and setters,and both beans are session scope.

in your first bean declare the second bean like:

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

with the setter of it:

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

now in your fillForm Method:

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";
    }

the passed data is now reachable in your second bean for the second view (since your beans are session scoped).

UPDATE:

lets say you have 2 beans: Bean1 and Bean2 (both are sessionScoped):

if you want to change values

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

..

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

NOTE: @ManagedProperty(value="#{bean2}") this value/name MUST be exactly identical to the name here @ManagedBean(name="bean2")

now the xhtml:

bean1.xhtml

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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