3 Replies Latest reply on Jun 27, 2007 7:43 AM by sutermar

    Rerender component with backing bean (binding attribute)

    sutermar

      Hi,

      i want to rerender a backing bean, if i click on a link with an action listener.
      Is that possible?

      I've made the following sample.
      The outputText with id="rep" will be updated successfully (i use a value binding).
      The outputText with the backing bind (binding) doesn't update.

      Environment:
      Java 1.5.0_11
      Tomahawk 5.5
      MyFaces 1.1.5
      Ajax4JSF 1.1.1


      test.jsp:

      <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
      <%@ taglib prefix="h" uri="http://java.sun.com/jsf/html" %>
      <%@ taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
      <%@ taglib prefix="a4j" uri="https://ajax4jsf.dev.java.net/ajax" %>
      
      <%@ include file="pages/noCache.jsp" %>
      
      <f:view>
      <a4j:page format="html-transitional">
      
       <h:form>
       <a4j:log hotkey="M" />
      
       <a4j:commandLink actionListener="#{bean.perform}" reRender="rep,aaa">
       <h:outputText value="testlink"/>
       </a4j:commandLink>
      
       <h:inputText size="50" value="#{bean.text}" >
       <a4j:support event="onkeyup" reRender="rep"/>
       </h:inputText>
      
       <h:outputText value="#{bean.text}" id="rep"/>
      
       <h:outputText binding="#{bean.output}" id="aaa"/>
       </h:form>
      </a4j:page>
      </f:view>
      


      Simple Java bean:
      package com.assentis.docwrite.htmlclient.beans;
      
      import javax.faces.component.html.HtmlOutputText;
      import javax.faces.event.ActionEvent;
      
      public class Bean
      {
       private String text;
       private String aaa = "Nothing";
      
       public String getText() {
       return text;
       }
      
       public void setText(String text) {
       this.text = text;
       }
      
       public void perform(ActionEvent event)
       {
       this.text = "link performed";
       this.aaa = "output element changed";
       }
      
       public HtmlOutputText getOutput()
       {
       HtmlOutputText text = new HtmlOutputText();
      
       text.setValue(this.aaa);
      
       return text;
       }
      
      }
      


      in faces-config.xml:

      <managed-bean>
       <managed-bean-name>bean</managed-bean-name>
       <managed-bean-class>com.assentis.docwrite.htmlclient.beans.Bean</managed-bean-class>
       <managed-bean-scope>session</managed-bean-scope>
       <managed-property>
       <property-name>text</property-name>
       <value />
       </managed-property>
      </managed-bean>
      


        • 1. Re: Rerender component with backing bean (binding attribute)
          sutermar

          I've found a (very ugly) solution, but it works:

          1. added action="update" to the ajax command link

          2. added this navigation rule to faces-config.xml:

          <navigation-rule>
           <from-view-id>/test.jsp</from-view-id>
           <navigation-case>
           <from-outcome>update</from-outcome>
           <to-view-id>/test.jsp</to-view-id>
           </navigation-case>
          </navigation-rule>
          


          But now, the whole page will be refreshed!
          What I want is, that only the outputtext with id="aaa" and id="rep" will be updated on the view side...

          • 2. Re: Rerender component with backing bean (binding attribute)

            yes, it will be non-ajax response. Moreover, the component tree will be re-built.

            For the testing purpose, replace

            <a4j:commandLink actionListener="#{bean.perform}" reRender="rep,aaa">
             <h:outputText value="testlink"/>
            </a4j:commandLink>

            with
            <h:commandLink actionListener="#{bean.perform}">
             <h:outputText value="testlink"/>
            </h:commandLink>
            


            Does the values are updated as you expect?




            • 3. Re: Rerender component with backing bean (binding attribute)
              sutermar

              Hi Sergey,
              thanks for your response!

              yes, the values are updated too...

              But my goal is to use the ajax functionality and not to refresh the whole
              page... I have to find a solution to rerender a backing bean... is that possible?

              Maybe there's a chance to rebuild some parts of the jsf view in the bean.perform method, like this way:

              1. Click on the ajax command link
              2. JSF calls action listener on bean.perform
              3. Refreshing only the view from the output element with id = "aaa"
              4. on the view side: rerendering of "rep" and "aaa"