1

The form is posted anyway, but the bean method referenced by the button action parameter only gets triggered if I don't put the form inside a ui:fragment. I'm using Mojarra 2.2.5 on Tomcat 6.0.41.

(It also happens if I use primefaces' p:commandButton instead)

view.xhtml?edit=true:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">
    <f:metadata>
        <f:viewParam name="edit" id="edit" />
        <ui:param name="editMode" value="#{not empty edit and edit eq 'true' and saveBean.contentEditable}" />
    </f:metadata>
    <h:head>
        <title>Page title</title>
    </h:head>
    <h:body>
        <ui:fragment rendered="#{editMode}">
        </ui:fragment>
            <h:form id="saveForm">
                <h:commandButton 
                    id="btn2" 
                    type="submit" 
                        value="Triggers POST but no action" 
                    action="#{saveBean.saveAllAction}"
                />
            </h:form>
    </h:body>
</html>

Backing bean:

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@ManagedBean(name = "saveBean")
@ViewScoped
public class TestBean implements Serializable{
    private static final long serialVersionUID = 1L;
    private static final Logger log = LoggerFactory.getLogger(TestBean.class);
    public String saveAllAction() {
        log.info("Received save all command.");
        return "success";
    }
    public TestBean() {}
    public String getContentEditable() {
        return contentEditable;
    }
    public void setContentEditable(String contentEditable) {
        this.contentEditable = contentEditable;
    }
    private String contentEditable = "true";
}
NotGaeL
  • 8,344
  • 5
  • 40
  • 70

2 Answers2

1

The solution I've found independently was pretty close to what @Ali C proposed: The bottom line is that the rendered="..." condition gets reevaluated on form post request arrival. The conditional can be made accessible on this situation by making it a bean parameter (saveBean.editMode):

<f:metadata>
    <f:viewParam name="edit" id="edit" value="#{saveBean.edit}"/>
</f:metadata>
...

            <ui:fragment rendered="#{saveBean.editMode}">
            ...

and add the properties edit and editMode to the bean.

NotGaeL
  • 8,344
  • 5
  • 40
  • 70
1

Reading this answer to a related question, I suspect #5 is your issue where the rendered attribute is being switched on JSF after the Apply Request phase (probably a timing situation with ui:param), thus failing the security recheck afterwards. You can quickly verify that the issue is not with ui:fragment or the h:commandButton by hardcoding true for the rendered attribute.

To get around the issue, have the viewParam set a property in the backing bean, then ui:fragment can check that property instead of checking the ui:param.

 <f:metadata>
    <f:viewParam name="edit" id="edit" value="#{saveBean.editMode}"/>
</f:metadata>
  ...
   <ui:fragment rendered="#{saveBean.editMode}">
Community
  • 1
  • 1
Ali Cheaito
  • 3,746
  • 3
  • 25
  • 30