开发者

JSF - Updating Model Values in Controller Bean

开发者 https://www.devze.com 2023-01-01 07:09 出处:网络
I have a Controller bean (SearchController) that has two managed bean as managed properties (SearchCriteria, SearchResults; both of which are session scoped).

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 开发者_JAVA百科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));
}   
}


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

精彩评论

暂无评论...
验证码 换一张
取 消