2012-06-07 52 views
2

我設法使用Primefaces運行WAB軟件包。問題是,有些示例正在運行,並且一些示例在嘗試實施時遇到問題。我測試了這個例子:嘗試實施Primefaces時出錯示例

<?xml version='1.0' encoding='UTF-8' ?> 
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"  
      xmlns:h="http://java.sun.com/jsf/html" 
      xmlns:f="http://java.sun.com/jsf/core" 
      xmlns:ui="http://java.sun.com/jsf/facelets" 
      xmlns:p="http://primefaces.org/ui" > 
     <h:head> 
      <ui:insert name="header">   
       <ui:include src="header.xhtml"/>   
      </ui:insert> 
     </h:head> 
     <h:body> 

      <h1><img src="resources/css/images/icon.png" alt="NVIDIA.com" /> History Center</h1> 
      <!-- layer for black background of the buttons --> 
      <div id="toolbar" style="margin: 0 auto; width:1180px; height:30px; position:relative; background-color:black"> 
       <!-- Include page Navigation --> 
       <ui:insert name="Navigation">   
        <ui:include src="Navigation.xhtml"/>   
       </ui:insert> 

      </div> 

      <div id="logodiv" style="position:relative; top:35px; left:0px;"> 
       <h:graphicImage alt="Demo edit form" style="position:relative; top:-20px; left:9px;" value="resources/images/logo_linuxz.png" /> 
      </div> 
      <div id="main" style="margin: 0 auto; width:1190px; height:700px; position:absolute; background-color:transparent; top:105px"> 

       <div id="mainpage" style="margin: 0 auto; width:1190px; height:500px; position:absolute; background-color:transparent; top:80px"> 

        <div id="settingsHashMap" style="width:350px; height:400px; position:absolute; background-color:r; top:20px; left:1px"> 

         <h:form> 

          <p:growl id="growl" showDetail="true" sticky="true" /> 

          <p:panel header="Growl"> 
           <h:panelGrid columns="2"> 
            <h:outputText value="Your Name: *" /> 
            <p:inputText value="#{LinuxController.text}" required="true" label="Name"/> 
           </h:panelGrid> 

           <p:commandButton value="Save" actionListener="#{LinuxController.save}" update="growl"/> 
          </p:panel> 

         </h:form> 


        </div> 

       </div> 
      </div> 

     </h:body> 
    </html> 

import javax.faces.bean.ViewScoped; 

import java.io.Serializable; 

import javax.faces.application.FacesMessage; 
import javax.faces.context.FacesContext; 

// Update form example 
@Named("LinuxController") 
@ViewScoped 
public class Linux implements Serializable { 

    public Linux() { 
    } 

    private String text; 

    public String gettext() { 
     return text; 
    } 
    public void settext(String text) { 
     this.text = text; 
    } 

    public void save(ActionEvent actionEvent) { 
     FacesContext context = FacesContext.getCurrentInstance(); 

     context.addMessage(null, new FacesMessage("Successful", "Hello " + text)); 
     context.addMessage(null, new FacesMessage("Second Message", "Additional Info Here...")); 
    } 

} 

這是POM文件:

<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.DX_57</groupId> 
    <artifactId>History-Module-57</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <packaging>war</packaging> 

    <name>History-Module-57</name> 

    <properties> 
     <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    </properties> 

    <dependencies> 


     <dependency> 
      <groupId>org.primefaces</groupId> 
      <artifactId>primefaces</artifactId> 
      <version>3.3</version> 
      <type>jar</type> 

     </dependency> 

     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.8.2</version> 
      <scope>test</scope> 
     </dependency> 

     <dependency> 
      <groupId>org.osgi</groupId> 
      <artifactId>org.osgi.core</artifactId> 
      <version>4.3.0</version> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.osgi</groupId> 
      <artifactId>org.osgi.compendium</artifactId> 
      <version>4.2.0</version> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.glassfish</groupId> 
      <artifactId>osgi-cdi-api</artifactId> 
      <version>3.1-b41</version> 
      <type>jar</type> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.glassfish</groupId> 
      <artifactId>javax.faces</artifactId> 
      <version>2.1.9</version> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>javax</groupId> 
      <artifactId>javaee-web-api</artifactId> 
      <version>6.0</version> 
      <scope>provided</scope> 

     </dependency> 
    </dependencies> 

    <build> 
     <pluginManagement> 

      <plugins> 
       <plugin> 
        <groupId>org.apache.felix</groupId> 
        <artifactId>maven-bundle-plugin</artifactId> 
        <version>2.3.7</version> 
        <extensions>true</extensions> 
        <configuration> 
         <supportedProjectTypes> 
          <supportedProjectType>ejb</supportedProjectType> 
          <supportedProjectType>war</supportedProjectType> 
          <supportedProjectType>bundle</supportedProjectType> 
          <supportedProjectType>jar</supportedProjectType> 
         </supportedProjectTypes> 
         <instructions> 
          <!-- Read all OSGi configuration info from this optional file --> 
          <_include>-osgi.properties</_include> 
          <!-- By default, we don't export anything --> 
          <Export-Package></Export-Package> 
          <DynamicImport-Package>*</DynamicImport-Package> 
          <Import-Package>*;resolution:=optional</Import-Package> 
          <Embed-Dependency>*;scope=compile|runtime;inline=false</Embed-Dependency> 
          <Embed-Transitive>true</Embed-Transitive> 
          <Embed-Directory>WEB-INF/lib/</Embed-Directory> 
          <Embed-StripVersion>false</Embed-StripVersion> 
          <Embed-StripGroup>true</Embed-StripGroup> 
          <Bundle-ClassPath>.,WEB-INF/classes</Bundle-ClassPath> 
         </instructions> 
        </configuration> 
        <executions> 
         <execution> 
          <id>bundle-manifest</id> 
          <phase>process-classes</phase> 
          <goals> 
           <goal>manifest</goal> 
          </goals> 
         </execution> 
         <execution> 
          <id>bundle-install</id> 
          <phase>install</phase> 
          <goals> 
           <goal>install</goal> 
          </goals> 
         </execution> 
        </executions> 
       </plugin> 

       <plugin> <!-- Need to use this plugin to build war files --> 
        <artifactId>maven-war-plugin</artifactId> 
        <groupId>org.apache.maven.plugins</groupId> 
        <!-- Use version 2.1-beta-1, as it supports the new property failOnMissingWebXml --> 
        <version>2.2</version> 
        <configuration> 
         <archive> 
          <!-- add bundle plugin generated manifest to the war --> 
          <manifestFile> 
           ${project.build.outputDirectory}/META-INF/MANIFEST.MF 
          </manifestFile> 
          <!-- For some reason, adding Bundle-ClassPath in maven-bundle-plugin 
          confuses that plugin and it generates wrong Import-Package, etc. 
          So, we generate it here. 
          --> 
          <manifestEntries> 
           <Bundle-ClassPath>WEB-INF/classes/ 
           </Bundle-ClassPath> 
          </manifestEntries> 
         </archive> 
        <!-- We don't have a web.xml --> 
         <failOnMissingWebXml>false</failOnMissingWebXml> 
        </configuration> 
       </plugin> 
      </plugins> 
     </pluginManagement> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-dependency-plugin</artifactId> 
       <version>2.4</version> 
       <executions> 
        <execution> 
         <phase>validate</phase> 
         <goals> 
          <goal>copy</goal> 
         </goals> 
         <configuration> 
          <outputDirectory>${endorsed.dir}</outputDirectory> 
          <silent>true</silent> 
          <artifactItems> 
           <artifactItem> 
            <groupId>javax</groupId> 
            <artifactId>javaee-endorsed-api</artifactId> 
            <version>6.0</version> 
            <type>jar</type> 
           </artifactItem> 
          </artifactItems> 
         </configuration> 
        </execution> 
       </executions> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>2.4</version> 
       <configuration> 
        <source>1.7</source> 
        <target>1.7</target> 
        <compilerArguments> 
         <endorseddirs>${endorsed.dir}</endorseddirs> 
        </compilerArguments> 
       </configuration> 
      </plugin> 
      <plugin> 
       <!-- Enable this plugin for all modules --> 
       <groupId>org.apache.felix</groupId> 
       <artifactId>maven-bundle-plugin</artifactId> 
      </plugin> 
     </plugins> 
    </build> 
    <repositories> 
     <repository> 
      <id>glassfish-repo</id> 
      <name>The Glassfish repository</name> 
      <url>http://download.java.net/maven/glassfish/</url> 
     </repository> 
     <repository> 
      <id>prime-repo</id> 
      <name>PrimeFaces Maven Repository</name> 
      <url>http://repository.primefaces.org</url> 
      <layout>default</layout> 
     </repository> 
    </repositories> 
    <description>Module History Module</description> 
</project> 

我只得到錯誤信息。我在Glassfish中得到這個錯誤堆棧:

[#|2012-06-07T15:45:52.438+0300|SEVERE|glassfish3.1.2|javax.faces.event|_ThreadID=339;_ThreadName=Thread-2;|Received 'javax.el.MethodNotFoundException' when invoking action listener '#{LinuxController.save}' for component 'j_idt17'|#] 

[#|2012-06-07T15:45:52.439+0300|SEVERE|glassfish3.1.2|javax.faces.event|_ThreadID=339;_ThreadName=Thread-2;|javax.el.MethodNotFoundException: Method not found: [email protected]() 
    at com.sun.el.util.ReflectionUtil.getMethod(ReflectionUtil.java:160) 
    at com.sun.el.parser.AstValue.invoke(AstValue.java:251) 
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:302) 
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:153) 
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88) 
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:769) 
    at javax.faces.component.UICommand.broadcast(UICommand.java:300) 
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794) 
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259) 
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) 
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) 
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) 
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) 
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655) 
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161) 
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231) 
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317) 
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195) 
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849) 
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746) 
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045) 
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228) 
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137) 
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104) 
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90) 
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79) 
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54) 
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59) 
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71) 
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532) 
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513) 
    at java.lang.Thread.run(Thread.java:722) 
|#] 

[#|2012-06-07T15:45:52.439+0300|SEVERE|glassfish3.1.2|javax.enterprise.resource.webcontainer.jsf.context|_ThreadID=339;_ThreadName=Thread-2;|JSF1073: javax.faces.event.AbortProcessingException caught during processing of INVOKE_APPLICATION 5 : UIComponent-ClientId=j_idt12:j_idt17, Message=Method not found: [email protected]()|#] 

[#|2012-06-07T15:45:52.440+0300|SEVERE|glassfish3.1.2|javax.enterprise.resource.webcontainer.jsf.context|_ThreadID=339;_ThreadName=Thread-2;|Method not found: [email protected]() 
javax.faces.event.AbortProcessingException: Method not found: [email protected]() 
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:182) 
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88) 
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:769) 
    at javax.faces.component.UICommand.broadcast(UICommand.java:300) 
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794) 
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259) 
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) 
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) 
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) 
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) 
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655) 
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161) 
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231) 
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317) 
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195) 
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849) 
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746) 
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045) 
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228) 
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137) 
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104) 
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90) 
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79) 
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54) 
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59) 
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71) 
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532) 
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513) 
    at java.lang.Thread.run(Thread.java:722) 
Caused by: javax.el.MethodNotFoundException: Method not found: [email protected]() 
    at com.sun.el.util.ReflectionUtil.getMethod(ReflectionUtil.java:160) 
    at com.sun.el.parser.AstValue.invoke(AstValue.java:251) 
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:302) 
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:153) 
    ... 33 more 
|#] 

什麼可能會導致此問題?

UPDATE

我改變了託管bean的他的名字。事實證明,該框架正在搜索託管bean的錯誤名稱 - avax.el.MethodNotFoundException: Method not found: [email protected]()。應該是avax.el.MethodNotFoundException: Method not found: [email protected]()這是一個配置問題嗎?

回答

6

這是因爲ViewScoped在CDI中不是本機支持的。要麼將@Named("LinuxController")更改爲@ManagedBean(name="LinuxController")以使其成爲純JSF,要麼將ViewScoped更改爲其他類似Seam的ConversationalScope或SessionScope(或者您的情況下,我認爲RequestScope就好)

P.S.另外,請檢查您是否正確實施包動作事件,一次我花了兩個小時,發現我有java.awt.event.ActionEvent中而不是從JSF正確的:-)

編輯

那麼你究竟嘗試了什麼?你是否改變了bean的範圍,使它成爲ManagedBean而不是CDI bean,在頁面和bean中都改變了方法名稱?或者你可以創建另一個具有相同簽名的bean並嘗試重現錯誤。另外嘗試爲文本變量賦值,以查看方法getText是否正在工作。在嘗試縮小可能的選擇範圍之前,很難說哪裏會出現問題。

SOLUTION

好了,我希望我找到你的解決方案。您應該決定是否要使用CDI或純JSF。如果你想採用CDI方式,請保留註釋爲@Named的bean並使用其他範圍,然後使用ViewScoped。如果您選擇JSF方式,則將@Named更改爲@ManagedBean,您可以保留它ViewScoped。在這兩種情況下,您都應該刪除faces-config.xml中的管理bean記錄,因此之前的JSF 1.2需要使用,所以現在不推薦使用它(並且不能在配置中同時使用註釋和記錄,您必須選擇其中一個選項)。

您還需要改善您的導入。正如我以前寫的,你有一個java.awt.ActionEvent的進口,但你需要javax.faces.event.ActionEvent。另請注意,如果您使用CDI,然後從javax.enterprise.context.*包(請參閱ViewScope缺失)中導入所有範圍註釋(RequestScoped,SessionScoped,...),並且如果想要與ManagedBean一起使用JSF,則導入從javax.faces.bean.*開始的所有內容。你不能將它們混合在一起,因爲那樣你會得到一些奇怪的錯誤,比如你所擁有的錯誤。

Here是更改後的文件。

你也應該遵循Java convetion和使用CamelCase命名你的方法(即不gettext的()的getText())。這對於JSF表達式語言尤其重要,因爲您需要setter和getter來訪問頁面上的bean屬性,並且這些方法應該用CamelCase編寫,因此JSF EL可以解析它們。

+0

ViewScoped在CDI中不起作用?我認爲這是CDI與Spring的優點之一,因爲我確信Spring不允許使用ViewScoped功能。 –

+0

不工作。爲什麼Glassfish正在搜索'[email protected]​​c4.save()'?它應該是'com.DX_57.HM_57.LinuxController.save()' – user1285928

+0

任何想法或建議? – user1285928