【问题标题】:Method Not Found in JSF Bean在 JSF Bean 中找不到方法
【发布时间】:2012-12-13 14:18:40
【问题描述】:

我已经使用 JSF 1.x 完成了一个应用程序,该应用程序已启动并正在运行。

应用程序有两个部分,一个是调用一个动作事件并显示相同的页面,第二个是调用一个方法并导航到下一页。典型的 Hello world 示例。

但是应用程序的第二部分没有工作,因为它没有在 bean 类中找到方法。

hello.jsp 中的“再见”按钮不起作用。

我调用了 Url http://localhost:8090/balaji/faces/JSP/hello.jsp,它正在正确执行,向我显示 hello.jsp 页面。

但是 hello.jsp 页面中的再见按钮不起作用,单击该按钮不会更改 URL。但是我在堆栈跟踪上得到了错误,如下所示。 Method存在且公开,EL表达式正确,导航规则已设置,JSP页面存在,但方法无响应。

我的堆栈跟踪显示以下错误:

堆栈跟踪:

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)

我的申请结构如下:

包结构:

Maven 依赖:

<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>

人脸配置:

<?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>

部署描述符(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>

豆类:

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";
    }

}

干净部署:应用程序在 Tomcat 7 上自行部署良好。我检查了 &tomcat_home%/webapps 上部署的包结构,所有 JSP 文件都按预期完好无损地放置在适当的位置。

我无法找出错误,是什么导致“再见”按钮触发?

【问题讨论】:

  • 我也看到了你之前的问题。你只面临“奇怪的问题”。您的项目设置更有可能只是完全过时的混乱(eeek,web.xml 2.3?faces-config.xml 1.0?JSP?)。我强烈建议扔掉所有东西,然后用理智的 JSF 2.x 目标教程重新开始。您应该绝对确保您没有看到过时的针对 JSF 1.x 的教程,因为它现在基本上已经过时了,并且只会在寻找目前主要针对 JSF 2.x 的个别问题/答案时引起混乱。
  • GoodBye commandButton 中需要 action 而不是 actionListener。更好地理解 actionListener 和 action。研究 BalusC stackoverflow.com/questions/3909267/… 给出的这个答案
  • BaluC,我将在接下来的 6 个月内对 JSF 1 和 2 进行研究。一旦我覆盖了 JSF 1.x,我将转向 JSF 2.x。我将来可能需要你的帮助。学习各种 UI 框架是我的浓厚兴趣,它对 JSF 1.x 和 JSF 2.0 的比较研究。我不喜欢软件开发。谢谢你的建议。让我们保持联系。

标签: java jsf button user-interface jsf-1.2


【解决方案1】:

您的goodbye 方法没有ActionListener 的完整公司

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

如果我没记错的话,它必须像您的其他方法一样接收ActionEvent 作为参数并返回 void。实际上,您实际上可能希望将其作为action 而不是actionListener 在您的commandButton 中,更改

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

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

并保持 goodbye 方法不变。

【讨论】:

  • addControls 确实如此,但goodbye 被定义为一个actionEvent,它似乎实际上是一个动作......
  • Gamb,我已经按照我的正确程度做了这个例子。我什至尝试了将 ActionEvent 参数添加到该方法的解决方案,但它仍然给出相同的错误。我认为除非需要,否则我们不需要提供参数。对于简单的页面导航,无需提供 ActionEvent 参数。
  • @bali208 我已经更新了我的答案,看来您正在尝试使用操作而不是 actionListener
  • 哦,它的动作而不是动作事件,哦,我的上帝..!我正在尝试,谢谢
  • Gameb,你是对的。它应该是 action 而不是 actionListner。非常感谢。
猜你喜欢
  • 2015-11-16
  • 2017-02-15
  • 1970-01-01
  • 2023-03-27
  • 2012-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多