h:commandButton is not working once I wrap it in a <h:panelGroup rendered>

只谈情不闲聊 提交于 2019-11-28 12:45:57
BalusC

It's because #{param['action'] == 'edit'} didn't evaluate true during processing the form submit and therefore the <h:panelGroup> is basically not rendered during processing the form submit, including the <h:commandButton> sitting in there. The form submit namely did not pass that parameter back to JSF. This way JSF won't see the <h:commandButton> and therefore never be able to decode and queue its action event. You need to make sure that all your rendered conditions evaluate the same during processing the form submit as they were during displaying the form (this is part of JSF's safeguard against tampered/hacked requests wherein the enduser would otherwise be able to execute unrendered command buttons like admin-only buttons).

So, you basically need to retain that parameter during the postbacks as well so that the rendered expression still evaluates true during processing the form submit. This can be achieved in several ways:

  1. Add it as <f:param> inside the <h:commandButton> of relevance:

    <h:commandButton action="#{usersBean.addUser(editUser)}" value="Edit" type="submit">
        <f:param name="action" value="#{param.action}" />
    </h:commandButton>
    

    (note: param['action'] and param.action are equivalent; those brackets are only useful if the 'action' contains periods, or represents another variable)

  2. Set it as a bean property by <f:viewParam>. Requirement is that the bean needs to be in view scope or broader. You've it in the session scope already, which is okay (but which is IMO way too broad, but that aside):

    <f:metadata>
        <f:viewParam name="action" value="#{usersBean.action}" />
    </f:metadata>
    ...
    <h:panelGroup rendered="#{usersBean.action}">
        ...
    </h:panelGroup>
    
  3. If you're already using JSF utility library OmniFaces, use its <o:form> instead of <h:form> which enables you submit to the current request URI instead of to the current JSF view ID. This way the GET request string will automagically be retained and you don't need to copypaste <f:param> over all command buttons like as in #1:

    <o:form useRequestURI="true">
        ...
    </o:form>
    

See also:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!