简体   繁体   English

的行动 <p:commandButton> 内 <ui:fragment rendered> 没有被调用

[英]Action of <p:commandButton> inside <ui:fragment rendered> is not invoked

I want to do something like this (redirect to a different page depending on some condition!): 我想做这样的事情(根据某些条件重定向到不同的页面!):

<ui:fragment rendered="#{projectPageBean.availableMethods}">
    <p:commandButton id="test"
        value="View Instrument"
        action="instrumentLayout?faces-redirect=false"/>                                 
</ui:fragment>
<ui:fragment rendered="#{not projectPageBean.availableMethods}">
    <p:commandButton id="test1"
        value="View Instrument"
        action="methodLayout?faces-redirect=false"/>       
</ui:fragment>

But this construction doesn't seem to work; 但这种结构似乎不起作用; the page redirect in the action attributes is never carried out! 永远不会执行动作属性中的页面重定向!

How can I get this behaviour right? 我怎样才能正确行为?

During processing the form submit, the rendered attribute of the input/command component and all of its parents is re-evaluated as part of safeguard against tampered requests. 在处理表单提交期间,输入/命令组件及其所有父项的rendered属性将被重新评估,作为防止篡改请求的一部分。 If it evaluates false , then any child input/command components won't be decoded and the submitted values won't be set and invoked actions won't be queued. 如果它的计算结果为false ,则不会解码任何子输入/命令组件,也不会设置提交的值,并且不会对调用的操作进行排队。

Apparently that's what's happening here. 显然这就是这里发生的事情。 Placing the bean in the view scope should solve it. 将bean放在视图范围中应该解决它。

@ManagedBean
@ViewScoped
public class ProjectPageBean {}

This way the bean will live as long as you interact with the same view by (ajax) postbacks. 这样,只要您通过(ajax)回发与同一视图进行交互,bean就会存在。

See also: 也可以看看:


Unrelated to the concrete problem, it's however quite strange to use command buttons for plain page-to-page navigation. 具体问题无关 ,然而使用命令按钮进行普通的页面到页面导航却很奇怪。 Better use regular buttons for that. 更好地使用常规按钮。

<ui:fragment rendered="#{projectPageBean.availableMethods}">
    <p:button id="test"
        value="View Instrument"
        outcome="instrumentLayout"/>
</ui:fragment>
<ui:fragment rendered="#{not projectPageBean.availableMethods}">
    <p:button id="test1"
        value="View Instrument"
        outcome="methodLayout"/>
</ui:fragment>

This will also solve the concrete problem. 这也将解决具体问题。 It will just send a plain GET request on the specified URL instead of performing a postback causing all the JSF form processing to run which in turn ultimately sends another GET request as redirect. 它只会在指定的URL上发送一个普通的GET请求,而不是执行回发,导致所有的JSF表单处理运行,最终发送另一个GET请求作为重定向。 Much less clumsy thus. 因此更加笨拙。 Note that you can safely put those buttons outside <h:form> this way. 请注意,您可以安全地将这些按钮放在<h:form>之外。

If you make them <h:link styleClass="ui-button ui-state-default ui-corner-all"> instead of <p:button> , then it's even much more SEO friendly (ie searchbots like Googlebot can better find and index them). 如果你使它们<h:link styleClass="ui-button ui-state-default ui-corner-all">而不是<p:button> ,那么它甚至更加SEO友好(即像Googlebot这样的搜索机器人可以更好地找到和索引他们)。

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM