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

时间:2010-12-04 10:41:39

标签: ajax jsf jsf-2 primefaces mojarra

我有这个网站:

<!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(文本“已启动”不会出现在标准输出上)。 有没有类似的问题?

编辑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' - 找不到它   在组件的上下文中   copingFilePhaseFocus

1 个答案:

答案 0 :(得分:4)

这可能有两个原因:

  1. Primefaces资源servlet未正确配置,这将导致无法加载必要的JavaScripts。在聚焦输入时,您应该能够通过检查webbrowser中的JS错误控制台来查看任何JS错误。在Firefox中,可通过按 Ctrl + Shift + J 来使用控制台。

    资源servlet将自动加载到Servlet 3.0环境(Glassfish v3,Tomcat 7,JBoss 6等)中,但在旧环境中,您需要在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. 方法签名错误。您应该能够通过阅读服务器日志并在日志中看到javax.el.MethodNotFoundException来查看它。您问题中的代码示例是正确的,但ActionEvent中存在歧义。 java.awt.event包中有一个同名的类。您可能会意外(自动)导入它。验证它确实是javax.faces.event.ActionEvent而不是其他。

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

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

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

    其中AjaxBehaviorEventjavax.faces.event.AjaxBehaviorEvent