h:form 和 p:ajax 的问题(Mojarra 2.0.2 和 Primefaces 2.0.2)

发布于 2024-10-06 04:23:17 字数 2560 浏览 4 评论 0原文

我有这个网站:

<!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:p="http://primefaces.prime.com.tr/ui">

<h:head></h:head>
<h:body>


    <h:form id="form-some">
        <h:inputText id="copingFilePhaseFocus">
            <p:ajax event="focus" actionListener="#{installationController.startCopyingWarFile}" />
        </h:inputText>
    </h:form>


</h:body>
</html>

和支持 bean:

@ManagedBean(name = "installationController")
@SessionScoped
public class InstallationController implements IPluginInstallationListener {

    // Some methods here (...)

    public void startCopyingWarFile(ActionEvent event) {
        System.out.println("\n\n\n\nStarted\n\n\n\n");
    }
}

此代码在 MyFaces 2.0.0 下工作。但在 MyFaces 2.0.2 或 Mojarra 2.0.2 下则不然。 通过告诉“不起作用”,我的意思是单击(聚焦)输入文本不会触发actionListener(文本“Started”不会出现在标准输出上)。 有人有类似问题吗?

编辑1(将 p:ajax 更改为 f:ajax 后):

    <p:outputPanel id="copingFilePhase">
        <p:accordionPanel speed="0.2"
            rendered="#{pluginInstallerWebBean.copingFilePhase}">
            <p:tab
                title="#{msg['installPlugin.copyingWar']} ... #{pluginInstallerWebBean.copingFilePhaseState}">
                <h:form prependId="false">
                    <p:focus for="copingFilePhaseFocus" />
                    <h:inputText id="copingFilePhaseFocus"
                        rendered="#{pluginInstallerWebBean.copingFilePhaseFocus}"
                        style="display:none;">
                        <f:ajax event="focus"
                            render="copingFilePhase obtainingPluginInformationPhase"
                            listener="#{installationController.startCopyingWarFile}" />
                    </h:inputText>
                </h:form>
                #{msg['installPlugin.copyingWarDescription']}
            </p:tab>
        </p:accordionPanel>
    </p:outputPanel>

    <p:outputPanel id="obtainingPluginInformationPhase">(...)</p:outputPanel>

错误是:

javax.faces.FacesException: 包含未知 ID “copingFilePhase” - 找不到它 在组件的上下文中 应对FilePhaseFocus

I have got this site:

<!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:p="http://primefaces.prime.com.tr/ui">

<h:head></h:head>
<h:body>


    <h:form id="form-some">
        <h:inputText id="copingFilePhaseFocus">
            <p:ajax event="focus" actionListener="#{installationController.startCopyingWarFile}" />
        </h:inputText>
    </h:form>


</h:body>
</html>

And backing bean:

@ManagedBean(name = "installationController")
@SessionScoped
public class InstallationController implements IPluginInstallationListener {

    // Some methods here (...)

    public void startCopyingWarFile(ActionEvent event) {
        System.out.println("\n\n\n\nStarted\n\n\n\n");
    }
}

This code was working under MyFaces 2.0.0. But under MyFaces 2.0.2 or Mojarra 2.0.2 does not.
By telling "does not work" I mean that clicking (focusing) input text does not triggering actionListener (Text "Started" does not appear on standard output).
Has anybody similar problem ?

EDIT 1 (After changing p:ajax to f:ajax):

    <p:outputPanel id="copingFilePhase">
        <p:accordionPanel speed="0.2"
            rendered="#{pluginInstallerWebBean.copingFilePhase}">
            <p:tab
                title="#{msg['installPlugin.copyingWar']} ... #{pluginInstallerWebBean.copingFilePhaseState}">
                <h:form prependId="false">
                    <p:focus for="copingFilePhaseFocus" />
                    <h:inputText id="copingFilePhaseFocus"
                        rendered="#{pluginInstallerWebBean.copingFilePhaseFocus}"
                        style="display:none;">
                        <f:ajax event="focus"
                            render="copingFilePhase obtainingPluginInformationPhase"
                            listener="#{installationController.startCopyingWarFile}" />
                    </h:inputText>
                </h:form>
                #{msg['installPlugin.copyingWarDescription']}
            </p:tab>
        </p:accordionPanel>
    </p:outputPanel>

    <p:outputPanel id="obtainingPluginInformationPhase">(...)</p:outputPanel>

And the error is:

javax.faces.FacesException:
contains an unknown id
'copingFilePhase' - cannot locate it
in the context of the component
copingFilePhaseFocus

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事灯 2024-10-13 04:23:17

这可能有两个原因:

  1. Primefaces 资源 servlet 未正确配置,这将导致无法加载必要的 JavaScript。当聚焦输入时,您应该能够通过检查网络浏览器中的 JS 错误控制台是否有任何 JS 错误来看到它。在 Firefox 中,可以通过按 Ctrl+Shift+J 访问控制台。

    资源 Servlet 将在 Servlet 3.0 环境(Glassfish v3、Tomcat 7、JBoss 6 等)中自动加载,但在较旧的环境中,您需要在 web.xml 中手动配置它:

    ;
        PrimeFaces 资源 Servlet
        org.primefaces.resource.ResourceServlet
    
    
        PrimeFaces 资源 Servlet
        /primefaces_resource/*
    
    
  2. 方法签名错误。您应该能够通过阅读服务器日志并在日志中看到 javax.el.MethodNotFoundException 来看到它。您问题中的代码示例是正确的,但 ActionEvent 中存在歧义。 java.awt.event 包中有一个同名的类。您可能不小心(自动)导入了它。验证它是否确实是 javax.faces.event.ActionEvent 而不是其他内容。

如果没有帮助,您可能需要考虑将 PrimeFaces p:ajax 替换为标准 JSF 2.0 f:ajax:

<f:ajax event="focus" listener="#{installationController.startCopyingWarFile}" />

其中

public void startCopyingWarFile(AjaxBehaviorEvent event) {
    // ...
}

AjaxBehaviorEventjavax.faces.event.AjaxBehaviorEvent

This can have two causes:

  1. The Primefaces resource servlet is not properly configured which will cause that the necessary JavaScripts won't be loaded. You should be able to see it by checking the JS error console in your webbrowser for any JS errors when focusing the input. In Firefox, the console is available by pressing Ctrl+Shift+J.

    The resource servlet will be loaded automatically in Servlet 3.0 environments (Glassfish v3, Tomcat 7, JBoss 6, etc), however in older environments, you need to configure it manually in web.xml:

    <servlet>
        <servlet-name>PrimeFaces Resource Servlet</servlet-name>
        <servlet-class>org.primefaces.resource.ResourceServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>PrimeFaces Resource Servlet</servlet-name>
        <url-pattern>/primefaces_resource/*</url-pattern>
    </servlet-mapping>
    
  2. The method signature is wrong. You should be able to see it by reading the server logs and seeing a javax.el.MethodNotFoundException in the logs. The code example in your question is correct, but there's ambiguity in ActionEvent. There's a class with the same name in java.awt.event package. You might have accidently (auto-)imported it. Verify if it is indeed javax.faces.event.ActionEvent and not something else.

If none helps, you may want to consider to replace the PrimeFaces p:ajax by the standard JSF 2.0 f:ajax:

<f:ajax event="focus" listener="#{installationController.startCopyingWarFile}" />

with

public void startCopyingWarFile(AjaxBehaviorEvent event) {
    // ...
}

where AjaxBehaviorEvent is javax.faces.event.AjaxBehaviorEvent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文