0

I can't pass values between two managed beans of different pages. I am implementing a search box JSF component in a Home page. I request some values and when the user hits search it goes to the search result page. The search result page has a JSF component SEARCH RESUKTS which needs to access the selection in the managed bean which correspond to the search box from the home page.

I have tried using injection but it seams that the Managed BEan box is reintialized, showing the default value. I pick an interest from the search box, i.e. Cinema, then I click search which takes me to search result, I hope to see cinema but I see Sport the defualt value. Please find the code below.

SEARCH RESULT MANAGED BEAN

import javax.el.ELContext;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@ApplicationScoped
public class ExpSearchResultsMB {

    /** Creates a new instance of ExpSearchResultsMB */
    public ExpSearchResultsMB() {
    }


 @ManagedProperty(value="#{expSearchBoxMB.selectedValue}")
      private String  selectedValue; // +setter
    @ManagedProperty(value="#{expSearchBoxMB.text}")
    private String prova;

    public String getProva() {
        return prova;
    }

    public void setProva(String prova) {
        this.prova = prova;
    }
    public String getSelectedValue() {

        return selectedValue;
    }

    public void setSelectedValue(String selectedValue) {
        this.selectedValue = selectedValue;
    }
}

SEARCH BOX MANAGED BEAN

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedProperty;


@ManagedBean
@ApplicationScoped
public class ExpSearchBoxMB {
    public Date date;
   public List<String> interests=new ArrayList<String>();

      public String selectedValue="Sport";
    public String getSelectedValue() {
        return selectedValue;
    }
    public void setSelectedValue(String selectedValue) {
        this.selectedValue = selectedValue;
    }

    public List<String> getInterests() {

        interests.add("Sport");
        interests.add("Musin");
        interests.add("Art");
        interests.add("Thatre");
        interests.add("Cinema");
        return interests;
    }

    public void setInterests(List<String> interests) {
        this.interests = interests;
    }

I would appreciate any help.

Cheers

Viola
  • 113
  • 4
  • 13

2 Answers2

-1

I would say debug and check if the correct value is set on selection change of interests.

If everythig is correct but still you see the incorrect results then try using the following code in ExpSearchResultsMB

FacesContext context = FacesContext.getCurrentInstance();
ExpSearchBoxMB expSearchBoxMBBean = (ExpSearchBoxMB) context.getApplication().evaluateExpressionGet(context, "#{expSearchBoxMB}", ExpSearchBoxMB.class);

expSearchBoxMBBean.getSelectedValue() 
sjngm
  • 12,423
  • 14
  • 84
  • 114
-1

You can try this:

<h:panelGrid columns="2" >
   <h:form>

      <h:outputLabel for="prova" value="Prova: " />
      <h:inputText binding="#{prova}" id="prova" />

      <h:outputLabel for="interest" value="Interest: " />
      <h:selectOneMenu binding="#{interest}" id="interest" >
         <f:selectItems value="#{expSearchBoxMB.interests}" var="i" 
                        itemLabel="#{i}" itemValue="#{i}" />
      </h:selectOneMenu>

      <h:button value="Search" outcome="Result">
         <f:param name="prova"    value="#{prova.value}" />
         <f:param name="interest" value="#{interest.value}" />
      </h:button>

   </h:form>
</h:panelGrid>

Then in your ExpSearchResultsMB, you can get the value like this:

@ManagedBean
@ViewScoped
public class ExpSearchResultsMB {
    private String interest;
    private String prova;
    private String statusMsg;


    @PostConstruct
    public void prepareResult() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        this.interest = request.getParameter("interest");
        this.prova    = request.getParameter("prova");

        if (interest == null || prova == null) statusMsg = "Please provide all information";
        else {
            // Prepare result to show to the user
        }
    }

    // Getters and Setters
}

If you use @RequestScoped for your ExpSearchResultsMB instead of @ViewScoped, you can use @ManagedProperty to get the submitted value as following:

@ManagedProperty(value="#{param.prova}"})
private String prova;
@ManagedProperty(value="#{param.interest}"})
private String interest;
Mr.J4mes
  • 9,168
  • 9
  • 48
  • 90