Search code examples
parameter-passingstripes

How do I pass a variable from one actionbean to another?


Currently, I have a Stripes form with a submit button that's binded to the "search" method of the following ActionBean:

public class SearchRepairActionBean extends BaseActionBean {
  private String searchType;
  private String searchValue;

  @DefaultHandler   
  public Resolution defaultHandler(){
    return null;
  }

  @HandlesEvent("search")
  public Resolution search(){   
    return new ForwardResolution("SearchRepairResult.action").addParameter("searchType", searchType).addParameter("searchValue", searchValue);;
  }

  public String getSearchType() {
    return searchType;
  }

  public void setSearchType(String searchType) {
    this.searchType = searchType;
  }

  public String getSearchValue() {
    return searchValue;
  }

  public void setSearchValue(String searchValue) {
    this.searchValue = searchValue;
  }
}

And SearchRepairResultActionBean attempts to resolve it thus:

public class SearchRepairResultActionBean extends BaseActionBean {
private String searchType;
private String searchValue;

@DefaultHandler 
public Resolution defaultHandler(){
    return new ForwardResolution("/jsp/searchRepairResult.jsp");
}

@HandlesEvent("SearchRepairResult")
public Resolution SearchRepairResult(){
    System.out.println(searchType);
    System.out.println(searchValue);    
    }
public String getSearchType() {
    return searchType;
}

public void setSearchType(String searchType) {
    this.searchType = searchType;
}

public String getSearchValue() {
    return searchValue;
}

public void setSearchValue(String searchValue) {
    this.searchValue = searchValue;
}
}

The problem is when I try to print "searchType" and "searchValue" - they return null. Am I passing parameters incorrectly? What would be the best way of doing this?


Solution

  • Why are you getting the type and value by calling getContext().getRequest().getParameter()? You should be able to just have methods on your SearchRepairResultActionBean like so:

    public void setType(String type) {
        this.type = type;
    }
    
    public void setValue(String value) {
        this.value = value;
    }
    

    And then Stripes will inject the necessary values in (as part of LifecycleStage.BindingAndValidation) before calling your event method.

    Actually, for that matter, why are you separating SearchRepairActionBean from SearchRepairResultActionBean? If the "result" page needs to view the data that came from the form that was submitted to the search event, then why not just perform the action in the search() method on SearchRepairActionBean, and then do a ForwardResolution on to your view? Is there a particular reason for using two ActionBeans here?