2012-06-15 58 views
0

我試圖將JSF連接到數據庫。java.lang.LinkageError:加載器約束違規:解析接口方法時javax.servlet.jsp.JspApplicationContext.getExpressionFactory()

這是文件

的web.xml

 <?xml version="1.0" encoding="UTF-8"?> 
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
id="WebApp_ID" version="2.5"> 

    <display-name>JavaServerFaces</display-name> 

    <resource-ref> 
<description>MySQL Datasource example</description> 
<res-ref-name>jdbc/mkyongdb</res-ref-name> 
<res-type>javax.sql.DataSource</res-type> 
<res-auth>Container</res-auth> 
    </resource-ref> 


    <!-- Change to "Production" when you are ready to deploy --> 
    <context-param> 
    <param-name>javax.faces.PROJECT_STAGE</param-name> 
    <param-value>Development</param-value> 
    </context-param> 

    <!-- Welcome page --> 
<welcome-file-list> 
    <welcome-file>default.xhtml</welcome-file> 
</welcome-file-list> 

    <!-- JSF mapping --> 
<servlet> 
<servlet-name>Faces Servlet</servlet-name> 
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
<load-on-startup>1</load-on-startup> 
</servlet> 

<!-- Map these files with JSF --> 
<servlet-mapping> 
<servlet-name>Faces Servlet</servlet-name> 
<url-pattern>/faces/*</url-pattern> 
</servlet-mapping> 
<servlet-mapping> 
<servlet-name>Faces Servlet</servlet-name> 
<url-pattern>*.jsf</url-pattern> 
</servlet-mapping> 
<servlet-mapping> 
<servlet-name>Faces Servlet</servlet-name> 
<url-pattern>*.faces</url-pattern> 
</servlet-mapping> 
<servlet-mapping> 
<servlet-name>Faces Servlet</servlet-name> 
<url-pattern>*.xhtml</url-pattern> 
</servlet-mapping> 

的context.xml

 <Context> 

    <Resource name="jdbc/mkyongdb" auth="Container" type="javax.sql.DataSource" 
      maxActive="100" maxIdle="30" maxWait="10000" 
      username="root" password="root" driverClassName="com.mysql.jdbc.Driver" 
      url="jdbc:mysql://localhost:3306/mkyongdb"/> 

    </Context> 

Default.xhtml

 <?xml version="1.0" encoding="UTF-8"?> 
    <!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:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core" 
    > 
<h:head> 
    <h:outputStylesheet library="css" name="table-style.css" /> 
</h:head> 

<h:body> 

    <h1>JSF 2.0 + JDBC Example</h1> 

    <h:dataTable value="#{customer.getCustomerList()}" var="c" 
      styleClass="order-table" 
      headerClass="order-table-header" 
      rowClasses="order-table-odd-row,order-table-even-row" 
     > 

     <h:column> 
      <f:facet name="header"> 
       Customer ID 
      </f:facet> 
       #{c.customerID} 
     </h:column> 

     <h:column> 
      <f:facet name="header"> 
       Name 
      </f:facet> 
       #{c.name} 
     </h:column> 

     <h:column> 
      <f:facet name="header"> 
       Address 
      </f:facet> 
       #{c.address} 
     </h:column> 

     <h:column> 
      <f:facet name="header"> 
       Created Date 
      </f:facet> 
       #{c.created_date} 
     </h:column> 

    </h:dataTable> 

    </h:body> 

    </html> 

CustomerBean.java

 package com.mkyong; 

    import java.io.Serializable; 
    import java.sql.Connection; 
    import java.sql.PreparedStatement; 
    import java.sql.ResultSet; 
    import java.sql.SQLException; 
    import java.util.ArrayList; 
    import java.util.List; 

    import javax.annotation.Resource; 
    import javax.faces.bean.ManagedBean; 
    import javax.faces.bean.SessionScoped; 
    import javax.naming.Context; 
    import javax.naming.InitialContext; 
    import javax.naming.NamingException; 
    import javax.sql.DataSource; 

    import com.mkyong.customer.model.Customer; 

    @ManagedBean(name="customer") 
    @SessionScoped 
    public class CustomerBean implements Serializable{ 

//resource injection 
@Resource(name="jdbc/mkyongdb") 
private DataSource ds; 

//if resource inject is not support, you still can get it manually. 
/*public CustomerBean(){ 
    try { 
     Context ctx = new InitialContext(); 
     ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mkyongdb"); 
    } catch (NamingException e) { 
     e.printStackTrace(); 
    } 

}*/ 

//connect to DB and get customer list 
public List<Customer> getCustomerList() throws SQLException{ 

    if(ds==null) 
     throw new SQLException("Can't get data source"); 

    //get database connection 
    Connection con = ds.getConnection(); 

    if(con==null) 
     throw new SQLException("Can't get database connection"); 

    PreparedStatement ps 
     = con.prepareStatement(
      "select customer_id, name, address, created_date from customer"); 

    //get customer data from database 
    ResultSet result = ps.executeQuery(); 

    List<Customer> list = new ArrayList<Customer>(); 

    while(result.next()){ 
     Customer cust = new Customer(); 

     cust.setCustomerID(result.getLong("customer_id")); 
     cust.setName(result.getString("name")); 
     cust.setAddress(result.getString("address")); 
     cust.setCreated_date(result.getDate("created_date")); 

     //store all data into a List 
     list.add(cust); 
    } 

    return list; 
} 
    } 

Customer.java

 package com.mkyong.customer.model; 

    import java.util.Date; 

     public class Customer{ 

public long customerID; 
public String name; 
public String address; 
public Date created_date; 

public long getCustomerID() { 
    return customerID; 
} 
public void setCustomerID(long customerID) { 
    this.customerID = customerID; 
} 
public String getName() { 
    return name; 
} 
public void setName(String name) { 
    this.name = name; 
} 
public String getAddress() { 
    return address; 
} 
public void setAddress(String address) { 
    this.address = address; 
} 
public Date getCreated_date() { 
    return created_date; 
} 
public void setCreated_date(Date created_date) { 
    this.created_date = created_date; 
} 
    } 

的pom.xml

<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/maven-v4_0_0.xsd"> 

<modelVersion>4.0.0</modelVersion> 
<groupId>com.mkyong.common</groupId> 
<artifactId>JavaServerFaces</artifactId> 
    <packaging>war</packaging> 
    <version>1.0-SNAPSHOT</version> 
    <name>JavaServerFaces Maven Webapp</name> 
    <url>http://maven.apache.org</url> 

    <repositories> 
<repository> 
    <id>java.net.m2</id> 
    <name>java.net m2 repo</name> 
    <url>http://download.java.net/maven/2</url> 
</repository> 
</repositories> 

    <dependencies> 

<!-- MySQL database driver --> 
<dependency> 
    <groupId>mysql</groupId> 
    <artifactId>mysql-connector-java</artifactId> 
    <version>5.1.9</version> 
</dependency> 

<!-- Spring framework --> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring</artifactId> 
    <version>2.5.6</version> 
</dependency> 

<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-web</artifactId> 
    <version>2.5.6</version> 
</dependency> 



<dependency> 
    <groupId>com.sun.faces</groupId> 
    <artifactId>jsf-api</artifactId> 
    <version>2.0.0-b13</version> 
</dependency> 

<dependency> 
    <groupId>com.sun.faces</groupId> 
    <artifactId>jsf-impl</artifactId> 
    <version>2.0.0-b13</version> 
</dependency> 

<!-- EL 2.2 to support method parameter in EL --> 
    <dependency> 
    <groupId>org.glassfish.web</groupId> 
    <artifactId>el-impl</artifactId> 
    <version>2.2</version> 
</dependency> 


    <!-- http://repo1.maven.org/maven2/ --> 
    <dependency> 
    <groupId>javax.servlet</groupId> 
    <artifactId>jstl</artifactId> 
    <version>1.2</version> 
</dependency> 





    </dependencies> 

    <build> 
    <finalName>JavaServerFaces</finalName> 

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

如果我跑這Tomcat7我收到以下錯誤

   SEVERE: Exception sending context initialized event to listener instance     of class com.sun.faces.config.ConfigureListener 
     java.lang.LinkageError: loader constraint violation: when resolving interface method "javax.servlet.jsp.JspApplicationContext.getExpressionFactory()Ljavax/el/ExpressionFactory;" the class loader (instance of org/apache/catalina/loader/WebappClassLoader) of the current class, com/sun/faces/config/ConfigureListener, and the class loader (instance of org/apache/catalina/loader/StandardClassLoader) for resolved class, javax/servlet/jsp/JspApplicationContext, have different Class objects for the type javax/el/ExpressionFactory used in the signature 
at com.sun.faces.config.ConfigureListener.registerELResolverAndListenerWithJsp(ConfigureListener.java:636) 
at com.sun.faces.config.ConfigureListener.contextInitialized(ConfigureListener.java:226) 
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4701) 
at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5204) 
at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5199) 
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) 
at java.util.concurrent.FutureTask.run(FutureTask.java:138) 
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:619) 
    Jun 15, 2012 2:31:32 PM org.apache.catalina.core.StandardContext startInternal 
    SEVERE: Error listenerStart 
    Jun 15, 2012 2:31:32 PM org.apache.catalina.core.StandardContext startInternal 
SEVERE: Context [/JavaServerFaces] startup failed due to previous errors 
    Jun 15, 2012 2:31:32 PM org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc 
SEVERE: The web application [/JavaServerFaces] registered the JDBC driver  [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped.  To prevent a memory leak, the JDBC Driver has been forcibly unregistered. 
Jun 15, 2012 2:31:32 PM org.apache.coyote.AbstractProtocolHandler start 
INFO: Starting ProtocolHandler ["http-bio-8080"] 
Jun 15, 2012 2:31:32 PM org.apache.coyote.AbstractProtocolHandler start 
    INFO: Starting ProtocolHandler ["ajp-bio-8009"] 
Jun 15, 2012 2:31:32 PM org.apache.catalina.startup.Catalina start 
INFO: Server startup in 2341 ms 

任何人都可以幫我解決這個問題。

+0

這看起來像是加載表達式語言api(el-api)的類中的衝突。任何與您的web應用程序捆綁在一起的EL jars? –

+0

我已經解決了這個錯誤。 el-api.jar被發現了兩次。我有一個新的錯誤。我已經更新了這個問題。請檢查它。 – bharathi

+1

在WEB-INF/lib中是否有jsf-api和jsf-impl jar? –

回答

0

確保JSF api和實現jar可用於加載容器(tomcat)。將這些庫放在WEB-INF/lib中的tomcat lib文件夾中。

+0

我已經添加了這些jar文件。現在我得到新的錯誤相關的Bean文件。我已經更新了問題。對於更多的麻煩 – bharathi

+0

您是否能夠使用構造函數中的代碼獲取數據源? –

+0

不,我得到了javax.el.E​​LException:/default.xhtml:在java.lang.String類型中找不到屬性'customerID' – bharathi

相關問題