【问题标题】:How to forward using h:commandButton如何使用 h:commandButton 转发
【发布时间】:2014-08-20 20:29:20
【问题描述】:

我有一个视图,其中我有一个带有 p:commandButton 的表单,并且一切正常,因为我正在使用 forward,这就是我需要的:

<h:form id="formularioAltas">
    // More code
    <p:commandButton value="Guardar" action="#{altasBean.agregarRefaccion()}" update="cboAlmacen cboEstado cboCategoria" />
</h:form>

agregarRefaccion() 方法是 void 因此不会更改页面,我认为forward 发生在该操作之后,因为它始终具有相同的 url。没关系,因为这是我需要的。

在同一个视图中,我对h:commandButton 有疑问。

<h:form id="myForm" enctype="multipart/form-data" prependId="false">
    // More code
    <h:commandButton id="button" style="background-color: black; color: aliceblue" value="Guardar imagen para #{altasBean.refaccion.idRefaccion}" action="#{altasBean.subirImagen()}" />
</h:form>

subirImagen() 方法也是 void 所以我认为在那个动作之后会发生前锋......但事实并非如此。页面更新并且 url 发生变化,这意味着重定向做到了。

我永远需要一个前锋。不管是p:commandButton还是h:commandButton

我认为与 PrimeFaces 以及 p:commandButton 与 JSF h:commandButton 的差异有关。

所以我需要一个前锋 h:commandButton

编辑

我总是有这样的网址 blabla/AlmacenGM/

但是当h:commandButton 出现时,网址是:blabla/AlmacenGM/faces/altas.xhtml

网址更改。我试图将字符串 "altas" 返回到方法 subirImagen() 中,因此应该进行转发,但它仍然会更改 url。

有什么想法吗?

【问题讨论】:

  • 当您单击p:commandButton 时看到的不是“转发”,它只是一个 AJAX 更新,您在update-属性中指定的所有组件都会以这种方式更新。 h:commandButton 不支持 AJAX,p:commandButton 那里有什么问题?
  • @user1983983, h:commandButton 不像 primefaces 那样被“ajaxified”,但是,它确实支持 ajax,只要你在其中使用 f:ajax 标签。

标签: jsf primefaces commandbutton


【解决方案1】:

p:commandButton 没有转发。它实际上是一个 ajax 请求,因为它是它的默认行为。

但是,标准 JSF h:commandButton 执行转发请求,只要您在操作方法中返回带有导航案例的 String。如果你看到那里发生了重定向,你一定是在你的 bean 端强制它。

另请参阅:

【讨论】:

  • 您必须在其中嵌入一个f:ajax 标签。查看相关链接
  • 嘿,问题是我有两个表格。当我指出@form 选择了哪些?我还需要更新表单本身。我放了这样的东西:&lt;f:ajax execute="@form" render=":myForm" /&gt; 但有时可以,但有些不行。
  • @KazMiller @form 是当前的表单选择器。 &lt;f:ajax execute="@form" render="@form" /&gt; 将处理您当前的表单并呈现它。然而,当两种形式出现在 JSF 2.x 分支中时存在已知问题,计划在 2.3 中修复。看看this。如果您只想呈现当前表单,这不会影响您。
猜你喜欢
  • 2014-02-03
  • 2011-06-19
  • 2012-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-27
  • 2013-06-08
相关资源
最近更新 更多