h:commandButton is not working once I wrap it in a

后端 未结 1 1974
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-11 09:23

The reason why the topic has \"kind of\" is because I have an example in JSF 2.2 where I use a commandButton and call a bean function twice (depending on the url). It\'s bas

相关标签:
1条回答
  • 2020-12-11 09:48

    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:

    • commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated - point 6
    0 讨论(0)
提交回复
热议问题