tags:

views:

31

answers:

1

I have a Controller bean (SearchController) that has two managed bean as managed properties (SearchCriteria, SearchResults; both of which are session scoped).

When the user hits the find button, the action method that is executed is in SearchController.

The SearchCreteria managed bean has a method called search(). This method returns a new SearchResults object. In the controller bean, I am setting the searchResults managed property to be this new SearchResults object. The searchResults object contains what I expect during that request, but the object does not persist in the managed bean.

I understand that I am changing what object that searchResults is referencing, but what I don't understand is why JSF isn't updating the model to use the new object. Any ideas what I'm missing or don't understand? I am using JSF 1.1 on WebSphere 6.1.

If I put the search method in the SearchResults managed bean, it works.

The line in SearchController.find() that is commented out is the one that presently works.

public class SearchController {

SearchCriteria searchCriteria;
SearchResults searchResults;
ResultsBacking resultsBacking;


public String find()
{

    setSearchResults(searchCriteria.search());
//      searchResults.findSearchResults(searchCriteria);

    if (!searchResults.resultsFound())
    {
        return "noresults";
    }

    return "success";
}


public class SearchCriteria {

public SearchResults search()
{
    SearchDAO sdao = new SearchDAO();
    ArrayList<Group> list = (ArrayList<Group>)sdao.findGroups(this); 

    SearchResults searchResults = new SearchResults();
    searchResults.setSearchResults(list);
    return searchResults;
}

}


public class SearchResults {

List<Group> searchResults;

public void findSearchResults(SearchCriteria criteria)
{
    SearchDAO sdao = new SearchDAO();   
    this.setSearchResults(sdao.findGroups(criteria));
}   
}
+1  A: 

In a nut, you've something like this:

@ManagedBean
public class SearchController {

    @ManagedProperty(value="#{searchCriteria}")
    private SearchCriteria searchCriteria;

    @ManagedProperty(value="#{searchResults}")
    private SearchResults searchResults;

    public void find() {
        searchResults = searchCriteria.search();
    }

}

And the #{searchResults} in the view doesn't contain the desired results?

This sounds like as if you're accessing the search results by #{searchResults.someData} instead of #{searchController.searchResults.someData} and you're expecting that overriding the SearchResults property inside the SearchController will replace the current session scoped managed bean.

This is wrong.

You need to solve it by either using #{searchController.searchResults.someData} instead

<h:outputText value="#{searchController.searchResults.someData}" />

Or by overriding (setting) the properties of SearchResults instead of overriding the whole managed property of SearchController:

    public void find() {
        searchResults.setSomeData(searchCriteria.search().getSomeData());
    }

Or by manually replacing the bean in session (not recommended).

    public void find() {
        searchResults = searchCriteria.search();
        FacesContext.getCurrentInstance().getExternalContext()
            .getSessionMap().put("searchResults", searchResults);
    }
BalusC
thanks BalusC, I've set the properties of SearchResults and that did the trick.Of the 3 you've listed, which one do you generally use in these situations?
Sean
Depends on the context and requirements. It's either first or second, but normally not the third. If `SearchResults` really needs to be an **independent** managed bean, then I'd go for second. But if it **cannot** survive without `SearchController` in any view, then I'd go for first and stop the `SearchResults` being a managed bean.
BalusC