【问题标题】:simple JSF commandButton not hitting the action简单的 JSF commandButton 没有击中动作
【发布时间】:2011-07-26 02:31:43
【问题描述】:

我有一个最简单的 JSF 小示例(带有 GlassFish 的 JSF2),但我无法弄清楚为什么命令按钮没有点击操作方法。这就是我所拥有的......当我点击按钮时,什么也没有发生。

我做错了什么?

testForm.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">

    <h:form>
        <h:messages />
        <p/>
        <h:inputText />
        <p/>
        <h:commandButton value="test1" action="#{testController.action1}" />
    </h:form>


</html>

faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>

<faces-config
    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-facesconfig_2_0.xsd"
    version="2.0">

    <managed-bean>
        <managed-bean-name>testController</managed-bean-name>
        <managed-bean-class>com.app.controller.TestController</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>

</faces-config>

TestController.java

package com.app.controller;

public class TestController {

    public String action1() {
        return "testPage2";
    }

}

【问题讨论】:

    标签: jsf


    【解决方案1】:

    尤里卡!从头开始重建 Eclipse 项目后,我意识到我做错了什么。 Apache MyFaces 位于项目路径中,应用程序部署在 GlassFish 上,GlassFish 拥有自己的 JSF 实现。这两个 JSF 实现不想很好地结合在一起。

    好痛啊。而且,你知道,我以前也犯过同样的错误。 Eclipse 应该对此发出警告,或者应该在 GlassFish 日志或 h:messages 标记中报告一些错误。

    【讨论】:

    • 再举一个例子,为什么在您的问题中提及您正在使用的 JSF impl/version 如此重要。之前已经提出并回答了完全相同的问题:stackoverflow.com/questions/6823719/…
    【解决方案2】:

    1)对于JSF2.0,不需要在Facesconfig.xml中配置托管bean。

    2)可以使用@managedban注解。

       package com.app.controller;
        import javax.faces.bean.ManagedBean;
        import javax.faces.bean.SessionScoped;
        @ManagedBean(name = "testController")
        @SessionScoped
        public class TestController {
            public String action1() {
                return "testPage2";
            }
    /** Constructor, getters and setters*/
        }
    

    【讨论】:

    • 这并不能说明问题。老式的 faces-config 声明在 JSF2 中应该也能正常工作。
    • 谢谢,我用注解和faces-config都试过了,它没有达到动作方法。此外,该 bean 配置正确,因为我添加了一个简单的属性来返回一个“测试字符串”并且它被很好地返回。不知道为什么它没有达到动作方法。 :(
    猜你喜欢
    • 2011-03-09
    • 1970-01-01
    • 1970-01-01
    • 2012-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-14
    • 2012-10-30
    相关资源
    最近更新 更多