Tankhenk
Tankhenk

Reputation: 634

Search form that redirects to results page

Hello i have the following problem.

I have a search page lets call it search.xhtml and you can search for a bar-code. This value is unique so the result is always one or zero objects from the database

<p:panelGrid columns="1" style="margin:20px;">
  <h:form>
    <p:messages id="messages" globalOnly="true" showDetail="false" />
    <p:message for="barcode" />

    <p:inputText id="barcode" value="#{searchForm.barCode}"
                 required="true" requiredMessage="Value needed" />

    <p:commandButton value="search"
                     action="#{searchForm.searchBarcode}" id="search"/>

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

This is the backingbean:

@ManagedBean
@ViewScoped
public class SearchForm extends BasePage {
  private Long barCode;

  @ManagedProperty("#{daoManager}")
  public DaoManager daoManager;

  public void setDaoManager(DaoManager daoManager) {
    this.daoManager = daoManager;
  }
  public Long getBarCode() {
    return barCode;
  }
  public void setBarCode(Long barCode) {
    this.barCode = barCode;
  }
  public String searchBarcode() {
    //request to dao to get the object
    DataList<Data> data = daoManager.findbybarcode(barCode);
    if (data.size() == 0) {
      this.addMessage(FacesMessage.SEVERITY_ERROR,
                      "Not Found: " + barCode);
      return null;
    } else {       
      getFacesContext().getExternalContext().
        getRequestMap().put("id", data.getId());
      return "details";
    }
  }

So if i go to my details page which expect the parameter id this isnt send to the detail page.

backing bean details page:

@ManagedBean
@ViewScoped
public class DetailBean extends BasePage implements Serializable {
  @PostConstruct
  public void init() {
    if (id != null) {
      //Go on with the stuff
    } else {
      addMessage(FacesMessage.SEVERITY_ERROR,"Object not found");
    }       
  }
}

What am i doing wrong? And is this wrong use of JSF? I know i can generate a list and the click on the result but thats not what i want. Also i can take the barcode from the first bean and pass it as a parameter but i want the details page only to accept the id from the objects. So is my thinking wrong? Or is there a solution to get it like this?

Upvotes: 0

Views: 1662

Answers (1)

maple_shaft
maple_shaft

Reputation: 10463

If I understand correctly, you wish to pass the ID of the barcode to the details page and yes this is possible.

getFacesContext().getExternalContext().getRequestMap().put("id", data.getId()); 

The following line is putting the ID parameter into the request that the client just sent you, but the navigation action to details will result in a different request. Try this instead:

return "details?faces-redirect=true&id=" + data.getId();

This will return an HTTP GET navigation action with the ID of the barcode passed as a request parameter in the request.

Upvotes: 1

Related Questions