【问题标题】:Search form that redirects to results page重定向到结果页面的搜索表单
【发布时间】:2023-03-21 07:41:02
【问题描述】:

您好,我有以下问题。

我有一个搜索页面,我们将其命名为 search.xhtml,您可以搜索条形码。此值是唯一的,因此结果始终是数据库中的一个或零个对象

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

这是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";
    }
  }

因此,如果我转到我的详细信息页面,该页面需要参数 id,这不会发送到详细信息页面。

backing bean 详情页面:

@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");
    }       
  }
}

我做错了什么?这是对 JSF 的错误使用吗?我知道我可以生成一个列表并单击结果,但这不是我想要的。我也可以从第一个 bean 中获取条形码并将其作为参数传递,但我希望详细信息页面只接受来自对象的 id。那我的想法错了吗?或者有没有办法得到它?

【问题讨论】:

    标签: redirect jsf-2 primefaces


    【解决方案1】:

    如果我理解正确,您希望将条形码的 ID 传递到 details 页面,是的,这是可能的。

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

    下一行是将 ID 参数放入客户端刚刚发送给您的请求中,但是到 details 的导航操作将导致不同的请求。试试这个:

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

    这将返回一个 HTTP GET 导航操作,其中条形码的 ID 作为请求参数传递。

    【讨论】:

    • 好吧,我试过了,但请求映射也不包含 id。它是空的。
    • @Tankhenk 请参阅上面的编辑...我修复了请求字符串。要将其视为单独的请求,您必须传递 faces-redirect=true 请求参数才能实际执行重定向。
    • 非常感谢。我已经用 faces-redirect=true 试过了,但是没有用,因为我想输入一些想法: details?faces-redirect=true?id= 但是我当然需要使用 & 符号!谢谢你让我大开眼界!
    猜你喜欢
    • 1970-01-01
    • 2013-07-22
    • 2017-08-16
    • 2021-01-10
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多