3

The button invoke the action method only when in the JSF view I delete JSTL code, I know that it is abour lifecycle but I dont understand the cause/problem. I like to comprime because it ocurrs this behavior.

view code work fine

    <f:metadata>            
<f:viewParam name="idAsociacion" value="#{msgUsuario.asociacion}" converter="#
               {asociacionConverter}"
        converterMessage="#msg['aplicacion.asociacion.error.converter']}" required="true" 
        requiredMessage="#{msg['aplicacion.asociacion.error.required']}" />
<f:viewParam name="idMsg" value="#{msgUsuario.mensajeRespondido}" converter="#
     {mensajeConverter}" converterMessage="#{msg['aplicacion.mensaje.error.converter']}" 
     required="true" requiredMessage="#{msg['aplicacion.mensaje.error.required']}" />
    </f:metadata>

    <ui:decorate template="/WEB-INF/templates/mainUsuario-template.xhtml">
    <ui:define name="title">        
    <h:outputFormat value="#{msg['usuario.escribirMsj.title']}">
        <f:param value="#{msgUsuario.asociacion.nombre}" />
    </h:outputFormat>
    </ui:define>

    <ui:param name="descripcionView" value="#{msg['usuario.escribirMsj.descripcion']}" />

     <ui:define name="content">
<h:panelGroup layout="block"
          id="escribirMensajeContainer"styleClass="escribirMensajeContainer">
    <h:form>
        <p:panelGrid>
        <p:row>
        <p:column>
        <h:outputLabel for="texto" value="#{msg['usuario.escribirMsj.mensaje']}" 
                 styleClass="labelInput" />
        </p:column>
        </p:row>

        <p:row>
        <p:column>
        <p:inputTextarea  autoResize="true" cols="80" rows="15" id="texto" 
             maxlength="500" value="#{msgUsuario.texto}">

        </p:inputTextarea>
            </p:column>
        </p:row>

        <p:row>
    <p:column>
    <h:message for="texto" id="messageMensajeError" styleClass="messageError" />
    </p:column>                         
        </p:row>


        <p:row>         
             <p:column>
    <p:commandButton action="#{msgUsuario.enviar}" 
               alt="#{msg['usuario.escribirMsj.alt.enviar']}" tabindex="2" title="#
      {fn:replace(msg['usuario.escribirMsj.title.enviar'],0,msgUsuario.asociacion.nombre)}" 
       value="#{msg['usuario.escribirMsj.enviar']}" />
</p:column>                         
    </p:row>                            
    </p:panelGrid>
    </h:form>
    </h:panelGroup>         
    </ui:define>
    </ui:decorate>
    </html>

But if I add JSTL how I show below. The view render fine because the message is not responsed but the button never invoke the action method and the view reload.

      <c:choose>
      <c:when test="#{msgUsuario.mensajeRespondido.respondido==false}">
        <h:form>
        <p:panelGrid>
        <p:row>
        <p:column>
        <h:outputLabel for="texto" value="#{msg['usuario.escribirMsj.mensaje']}" 
                 styleClass="labelInput" />
        </p:column>
        </p:row>

        <p:row>
        <p:column>
        <p:inputTextarea  autoResize="true" cols="80" rows="15" id="texto" 
             maxlength="500" value="#{msgUsuario.texto}">

        </p:inputTextarea>
            </p:column>
        </p:row>

        <p:row>
    <p:column>
    <h:message for="texto" id="messageMensajeError" styleClass="messageError" />
    </p:column>                         
        </p:row>


        <p:row>         
             <p:column>
    <p:commandButton action="#{msgUsuario.enviar}" 
               alt="#{msg['usuario.escribirMsj.alt.enviar']}" tabindex="2" title="#
      {fn:replace(msg['usuario.escribirMsj.title.enviar'],0,msgUsuario.asociacion.nombre)}" 
       value="#{msg['usuario.escribirMsj.enviar']}" />
</p:column>                         
    </p:row>                            
    </p:panelGrid>
    </h:form>
    </c:when>
    <c:otherwise>
         <h:outputText value="MESSAGE RESPONSED" />
    </c:otherwise>
    </c:choose>

Note: Converts work fine in two case.

ManagedBean code

  @ManagedBean(name="msgUsuario")
  @ViewScoped
  public class EscribirMensajeUsuarioView implements Serializable {

private static final long serialVersionUID = 1L;
private static final Logger logger=Logger.getLogger(EscribirMensajeUsuarioView.class);

private Asociacion asociacion;

private Mensaje mensajeRespondido;

private Usuario usuario;


private String texto;

private boolean usuarioBloqueado;

@ManagedProperty(value="#{mensajeBO}")
private MensajeBO mensajeBo;

@ManagedProperty(value="#{usuarioBO}")
private UsuarioBO usuarioBo;


/* Never invoked with JSTL code in the view */

public String enviar(){
    logger.info("EscribirMensajesUsuarioView.enviar");

    TipoMensaje tipoMensaje=null;
    Mensaje mensaje=this.mensajeRespondido;

    .................
            .................
            .................
      }


/* Getters and Setters */
   }

I read this Balusc's comment

"JSF and JSTL doesn't run in sync as you'd expect from the coding. JSTL runs during build time of the view (when the JSF component tree is to be populated) and JSF runs during render time of the view component tree (when HTML output is to be generated). You can visualize it as follows: JSTL runs first from top to bottom and then hands over the result to JSF which in turn runs from top to bottom again."

I understand this in dinamyc datatable for example, but not in this case. Because JSTL render fine the view being that mensajeRepetido exit in the JSF tree, but button doesnt invoke the method.However the view reload and mensajeRepetido exits again by the converter.

Kind regards.

1 Answers1

3

Your problem is caused because you're binding JSTL attributes to a view scoped managed bean property. This works only if partial state saving is turned off. With partial state saving turned on (as by default), JSTL attributes gets its own second instance of the view scoped bean (with all properties set to default!) and not the instance which is stored in JSF view state and is been used by JSF components.

Just use JSF component's rendered attribute instead the usual way and use JSTL only to control the building the view, not to control the rendering of the view.

<h:form rendered="#{not msgUsuario.mensajeRespondido.respondido}">
    ...
<h:form>
<h:panelGroup rendered="#{msgUsuario.mensajeRespondido.respondido}">
     <h:outputText value="MESSAGE RESPONSED" />
</h:panelGroup>      

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank for your response. I dont understand this "use JSTL only to control the building the view, not to control the rendering of the view". What is the difference? – Rafael Ruiz Tabares May 25 '12 at 17:10
  • 2
    Building of the view is the conversion step of XHTML file to JSF component tree as available by `context.getViewRoot()`. Rendering of the view is the conversion step of JSF component tree to HTML output by calling `viewRoot.encodeAll(context)`. JSTL runs during building of the view and is not present in JSF component tree. The view is built on every GET request and is reused on every subsequent POST request which identifies the view by the hidden input field `javax.faces.ViewState`. – BalusC May 25 '12 at 17:11
  • 1
    In JSF 2.2 the view scoped is restored before the view is build, so there JSTL and other build-time tags could access view scoped beans (if this is good thing to do is another question). – Mike Braun May 25 '12 at 17:44
  • I have more clear the concept.I read the issue 1492 and I think that it is a good notice, although with rendered attribute this task is solved but it is better to have JSTL more near. Thank you very much¡ – Rafael Ruiz Tabares May 26 '12 at 16:45