1

I am learning RichFaces. Added a4j:commandButton to .xhtml. Below is my .xhtml code,

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:rich="http://richfaces.org/rich"
xmlns:a4j="http://richfaces.org/a4j">
   <h:head>
   </h:head>

   <h:body>
    <h:form>
     <rich:panel>
         <h:panelGrid column="2">
            <h:inputText value="#{echoBean.name}"/>
            <h:outputText value="#{echoBean.name}" id="echoTxt" />
            <h:outputText value="Count" />
            <h:outputText id="countTxt" value="#{echoBean.count}" />
         </h:panelGrid>
         <a4j:commandButton value="Send" actionListener="#{echoBean.incrementCount}"  reRender="echoTxt, countTxt"/>
     </rich:panel>
     </h:form>
   </h:body>
</html>

I am using richfaces 4. So I have not given any a4j filters

But a4j:commandButton is not working. It is not doing any action on click. And no error in stack trace.

Am I missing something?

Thanks

Update:

I have replaced the the actionListener with action. Now onclick of the button it throws the following exception,

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.rangeCheck(ArrayList.java:604)
    at java.util.ArrayList.get(ArrayList.java:382)
    at javax.faces.component.AttachedObjectListHolder.restoreState(AttachedObjectListHolder.java:165)
    at javax.faces.component.UIComponentBase.restoreState(UIComponentBase.java:1560)
    at com.sun.faces.application.view.StateManagementStrategyImpl$2.visit(StateManagementStrategyImpl.java:267)
    at com.sun.faces.component.visit.FullVisitContext.invokeVisitCallback(FullVisitContext.java:151)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1590)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
    at javax.faces.component.UIForm.visitTree(UIForm.java:344)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
    at com.sun.faces.application.view.StateManagementStrategyImpl.restoreView(StateManagementStrategyImpl.java:254)
    at com.sun.faces.application.StateManagerImpl.restoreView(StateManagerImpl.java:188)
    at com.sun.faces.application.view.ViewHandlingStrategy.restoreView(ViewHandlingStrategy.java:123)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.restoreView(FaceletViewHandlingStrategy.java:453)
    at com.sun.faces.application.view.MultiViewHandler.restoreView(MultiViewHandler.java:148)
    at javax.faces.application.ViewHandlerWrapper.restoreView(ViewHandlerWrapper.java:303)
    at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:192)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:116)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:539)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:298)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

my managed Bean code is,

/** * */

package org.droidaceapps.src;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;

/**
 * @author yasodavenkat
 *
 */
@ManagedBean(name="echoBean")
@SessionScoped
public class EchoBean {

    private String name;
    private int count;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public EchoBean() {
    }

    public void incrementCount(ActionEvent e){
        count = name.length();
    }   
}

I understand that it is related the state management problem. Is my guess correct? Am I miss anything else?

Thanks

droidsites
  • 1,035
  • 4
  • 16
  • 29
  • any errors in server logs / firebug? – Daniel Apr 03 '12 at 10:52
  • @Daniel - No errors in server logs. Actually I am expecting the length of the given string in the text field to be displayed in OutputText. It is happening with normal commandButton. But if I use the a4j:commandButton it is not working at all. – droidsites Apr 03 '12 at 13:12

2 Answers2

1

You're using actionListener for your command button, you should use action to make it work.

<a4j:commandButton value="Send" action="#{echoBean.incrementCount}"
    reRender="echoTxt, countTxt"/>

Plus, don't forget to set the data you're sending by adding the execute attribute tag. By default, it will submit all the form. You can find more info in the component documentation and you can refer to the online showcase.

UPDATE:

The name variable is null, that's why its throwing that error. That's because you're using. This is because you're doing this

<h:inputText value="#{echoBean.name}"/>
<h:outputText value="#{echoBean.name}" id="echoTxt" />

The echoBean.name is being setted twice in your bean, the first with the inputText value (the text you've entered) and the second time with the outputText value (null). If you want this behavior, you should really look on the showcase link I've added in my post and analyze how it must be done.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • Thanks for the reply. Just watching your reply I tried adding action instead of actionListener. This it has taken click event but it is throwing an exception. Of course I dont think it is related to this but you may help me out in this. Please see my update in the question with the Managed Bean and the error trace. – droidsites Apr 03 '12 at 14:23
  • just asking on eagerness.... it it is the reason behind that error then I was thinking how it worked the time I used h:commandButton. Then answered it myself as when using h:commandButton it is refreshing the whole page so that those state values are available. But in case of a4j: how the states are updated? – droidsites Apr 03 '12 at 15:37
0

These are some steps to solve or check your problem solution. Please note this is not complete solution due to less info about your project (your backing bean not here)

  1. try 'h' or 't' tag and try to run it
  2. check your manage bean you mention your bean scope put it in sessionscope

If this not work then come with your backing bean and faces-config.xml

Sunil D.
  • 17,983
  • 6
  • 53
  • 65
AirSon
  • 31
  • 1