在不同页面的托管Bean之间传递值

时间:2012-01-11 11:03:21

标签: jsf communication managed-bean

我无法在不同页面的两个托管bean之间传递值。 我在主页中实现了一个搜索框JSF组件。我请求一些值,当用户点击搜索时,它会转到搜索结果页面。 搜索结果页面有一个JSF组件SEARCH RESUKTS,它需要访问托管bean中与主页上的搜索框对应的选项。

我尝试过使用注入,但是它会将Managed BEan框重新初始化,显示默认值。我从搜索框中选择了一个兴趣,即Cinema,然后我点击搜索,它带我去搜索结果,我希望看到电影,但我看到Sport的defualt值。 请找到以下代码。

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

我将不胜感激。

干杯

2 个答案:

答案 0 :(得分:0)

我会说调试并检查是否在选择更改兴趣时设置了正确的值。

如果everythig正确但仍然看到不正确的结果,请尝试在ExpSearchResultsMB中使用以下代码

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

expSearchBoxMBBean.getSelectedValue() 

答案 1 :(得分:0)

你可以试试这个:

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

然后在ExpSearchResultsMB中,您可以获得如下值:

@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
}

如果您使用@RequestScoped代替ExpSearchResultsMB代替@ViewScoped,则可以使用@ManagedProperty获取提交的值,如下所示:

@ManagedProperty(value="#{param.prova}"})
private String prova;
@ManagedProperty(value="#{param.interest}"})
private String interest;