1

I have completed an application using JSF 1.x, which is up and running.

The application has 2 parts, one being invoke a action event and display the same page, and the second being invoke a method and navigate to next page. Typical Hello world Example.

But the Second part of the application is not working as it is not finding the method in the bean class.

The 'Goodbye' button in hello.jspis not working.

I invoked the Url http://localhost:8090/balaji/faces/JSP/hello.jsp which is executing correctly displaying me the hello.jsp page.

But the Goodbye Button in hello.jsp page is not working, which when clicked will not change the URL. But i got the error on stack trace as shown below. The Method is existing and it is public, EL expression is correct, navigation rule is set, JSP page exist, but the method is not responding.

My Stack Trace show the following Error:

Stacktrace:

13 Dec, 2012 7:17:19 PM javax.faces.event.MethodExpressionActionListener processAction
SEVERE: Received 'org.apache.jasper.el.JspMethodNotFoundException' when invoking action listener '#{helloBean.goodbye}' for component 'goodbyeCommand'
13 Dec, 2012 7:17:19 PM javax.faces.event.MethodExpressionActionListener processAction
SEVERE: org.apache.jasper.el.JspMethodNotFoundException: /JSP/hello.jsp(40,2) '#{helloBean.goodbye}' Method not found: com.achutha.bean.HelloBean@46a5c4.goodbye(javax.faces.event.ActionEvent)
    at org.apache.jasper.el.JspMethodExpression.invoke(JspMethodExpression.java:73)
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:99)
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:771)
    at javax.faces.component.UICommand.broadcast(UICommand.java:372)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:475)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:756)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:265)
    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:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:931)
    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:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1822)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)

My Application Structure is as follows :

Package Structure :

poc

Maven Dependencies :

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.achutha.labs</groupId>
    <artifactId>02JsfExample</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>02JsfExample</name>

    <dependencies>

        <dependency>
            <groupId>javax.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>1.2_14</version>
        </dependency>
        <dependency>
            <groupId>javax.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>1.2_14</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
        </dependency>

        <!-- EL -->
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>el-impl</artifactId>
            <version>2.2</version>
        </dependency>


    </dependencies>


    <build>
        <finalName>JavaServerFaces</finalName>

        <plugins>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>tomcat-maven-plugin</artifactId>
                <configuration>
                    <url>http://localhost:8090/manager/text</url>
                    <server>TomcatServer</server>
                    <path>/balaji</path>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Faces Configuration :

<?xml version="1.0"?>
<!DOCTYPE faces-config PUBLIC
     "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
     "http://java.sun.com/dtd/web-facesconfig_1_0.dtd">

<faces-config>
    <managed-bean>
        <description> The one and only HelloBean.</description>
        <managed-bean-name>helloBean</managed-bean-name>
        <managed-bean-class>com.achutha.bean.HelloBean</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>



    <navigation-rule>
        <description>Navigation From the Hello Page.</description>
        <from-view-id>/JSP/hello.jsp</from-view-id>
        <navigation-case>
            <from-outcome>success</from-outcome>
            <to-view-id>/JSP/goodbye.jsp</to-view-id>
        </navigation-case>
    </navigation-rule>

</faces-config>

Deployment Descriptor (web.xml) :

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE web-app
        PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd">

   <web-app version="2.5"
      xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

        <display-name>Hello, World!</display-name>
        <description>Welcome to JavaServer Faces</description>

        <servlet>
            <servlet-name>Faces Servlet</servlet-name>
            <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>

        <servlet-mapping>
            <servlet-name>Faces Servlet</servlet-name>
            <url-pattern>/faces/*</url-pattern>
        </servlet-mapping>

        <welcome-file-list>
            <welcome-file>faces/JSP/hello.jsp</welcome-file>
        </welcome-file-list>


    </web-app>

hello.jsp

<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>

<f:view>
    <html>
<head>
<title>JSF In Action - Hello, world!</title>
</head>
<body>
    <h:form id="welcomeForm">
        <h:outputText id="welcomeOutput" value="Welcome to JavaServer Faces !"
            style="font-family: Arial, sans-serif; font-size: 23; color: green;" />

        <p>
            <h:message id="errors" for="helloInput" style="color: red" />
        </p>

        <p>
            <h:outputLabel for="helloInput">
                <h:outputText id="helloInputLabel"
                    value="Enter Number of controls to display: ">

                </h:outputText>

                <h:inputText id="helloInput" value="#{helloBean.numControls}"
                    required="true">
                    <f:validateLongRange minimum="1" maximum="500" />
                </h:inputText>
            </h:outputLabel>
        </p>

        <p>
            <h:panelGrid id="controlPanel" binding="#{helloBean.controlPanel}"
                columns="20" border="1" cellspacing="0" />
        </p>

        <h:commandButton id="redisplayCommand" type="submit" value="Redisplay"
            actionListener="#{helloBean.addControls}" />

        <h:commandButton id="goodbyeCommand" type="submit" value="Goodbye"
            actionListener="#{helloBean.goodbye}" immediate="true"/>

    </h:form>

</body>

    </html>

</f:view>

goodbye.jsp :

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<f:view>
    <html>
<head>
<title>Jsf in Action - Hello World!</title>
</head>
<body>
    <h:form id="goodbyeForm">
        <p>
            <h:outputText id="welcomeOutput" value="Good Bye"
                style="font-family: Arial, sans-serif; font-size: 24; font-style: bold; color: green;" />

        </p>
        <p>
            <h:outputText id="helloBeanOutputLabel"
                value="Number of controls displayed: " />
            <h:outputText id="helloBeanOutputLabel"
                value="#{helloBean.numControls}" />
        </p>
    </h:form>

</body>
    </html>
</f:view>

Bean class :

package com.achutha.bean;

import java.util.List;

import javax.faces.application.Application;
import javax.faces.component.UIComponent;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.component.html.HtmlPanelGrid;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

public class HelloBean {

    private int numControls;
    private HtmlPanelGrid controlPanel;

    public int getNumControls() {
        return numControls;
    }

    public void setNumControls(int numControls) {
        this.numControls = numControls;
    }

    public HtmlPanelGrid getControlPanel() {
        return controlPanel;
    }

    public void setControlPanel(HtmlPanelGrid controlPanel) {
        this.controlPanel = controlPanel;
    }

    public void addControls(ActionEvent actionEvent) {
        Application application = FacesContext.getCurrentInstance()
                .getApplication();

        List<UIComponent> children = controlPanel.getChildren();

        children.clear();

        for (int count = 0; count < numControls; count++) {
            HtmlOutputText output = (HtmlOutputText) application
                    .createComponent(HtmlOutputText.COMPONENT_TYPE);
            output.setValue(" " + count + " ");
            output.setStyle("color: blue");
            children.add(output);
        }
    }

    public String goodbye() {
        return "success";
    }

}

Clean Deployment : The application is deploying itself fine on Tomcat 7. I checked the deployed package structure on &tomcat_home%/webapps and all the JSP files are intact in proper place as expected.

I am not able to figure out the error, and what makes 'GoodBye' button trigger ?

bali208
  • 2,257
  • 7
  • 40
  • 43
  • 4
    I saw your previous questions as well. You're only facing "weird problems". It's more likely that your project setup is just a completely outdated mess (eeek, web.xml 2.3? faces-config.xml 1.0? JSP?). I strongly recommend to throw all away and restart clean with a sane JSF 2.x targeted tutorial. You should make absolutely sure that you're not looking at hopelessly outdated JSF 1.x targeted tutorials as it's basically out of life nowadays and would only cause confusion when looking for individual questions/answers which are mostly JSF 2.x targeted these days. – BalusC Dec 13 '12 at 14:22
  • 1
    You need action not actionListener in your GoodBye commandButton. Better understand between actionListener and action. Study this answer given by BalusC http://stackoverflow.com/questions/3909267/differences-between-action-and-actionlistener – Narayan Subedi Dec 13 '12 at 15:51
  • BaluC, I am doing a research study on both JSF 1 & 2 for the next 6 months. Once i cover with JSF 1.x i will move onto JSF 2.x. I may need your assistance in future. It is my keen interest to learn various UI frameworks, its comparative study over JSF 1.x & JSF 2.0. I am not into software development. Thanks for the suggestion. Lets be in touch. – bali208 Dec 14 '12 at 00:27

1 Answers1

3

Your goodbye method doesn't have the complete firm of an ActionListener

public String goodbye() {
    return "success";
}

It must receive an ActionEvent as a parameter like your other method does and return void, if I'm not mistaken. As it is, you might actually want to put it as an action instead of an actionListener in your commandButton, changing

<h:commandButton id="goodbyeCommand" type="submit" value="Goodbye"
        actionListener="#{helloBean.goodbye}" immediate="true"/>

to

<h:commandButton id="goodbyeCommand" type="submit" value="Goodbye"
        action="#{helloBean.goodbye}" immediate="true"/>

and leaving the goodbye method as it is.

Fritz
  • 9,987
  • 4
  • 30
  • 49
  • 1
    `addControls` does, but `goodbye` is being defined as an actionEvent where it seems to be actually an action... – Fritz Dec 13 '12 at 14:22
  • Gamb, I have done the example to my level best of correctness. I even tried your solution of adding ActionEvent Parameter to the method, but it is still giving the same error. I dont think so we need to give a parameter unless its needed. For simple page navigation no need to give a parameter as ActionEvent. – bali208 Dec 13 '12 at 14:25
  • 1
    @bali208 I've updated my answer, It seems you're trying to use an action instead of an actionListener – Fritz Dec 13 '12 at 14:26
  • Oh its action and not action Event , Oh my god..! I am trying it now, thanks – bali208 Dec 13 '12 at 14:28
  • Gameb, you were correct. It should be action and not actionListner. Thank you soo much. – bali208 Dec 13 '12 at 14:31
  • @bali208 You're welcome. I second BalusC's suggestion, you should switch to JSF 2.x. It's more approachable at first, basing it mostly in annotations making the overall structure less complicated. – Fritz Dec 13 '12 at 14:35
  • I am doing a research study on both JSF 1 & 2. Once i cover with JSF 1.x i will move onto JSF 2.x. – bali208 Dec 13 '12 at 14:37