运行Web应用程序 - 错误:包javax.faces.bean不存在

时间:2013-09-03 22:52:15

标签: java web-applications runtime-error

我刚接触网络应用程序。 我试过运行简单的Web应用程序。运行后

javac -d WEB-INF/classes WEB-INF/classes/bigjava/*.java 

我有输出:

nazar_art@nazar-desctop:~/Desktop/Big JAVA/bj4_code/ch24/time$ javac -d WEB-INF/classes WEB-INF/classes/bigjava/*.java
WEB-INF/classes/bigjava/TimeBean.java:6: error: package javax.faces.bean does not exist
import javax.faces.bean.ManagedBean;
                       ^
WEB-INF/classes/bigjava/TimeBean.java:7: error: package javax.faces.bean does not exist
import javax.faces.bean.SessionScoped;
                       ^
WEB-INF/classes/bigjava/TimeBean.java:9: error: cannot find symbol
@ManagedBean
 ^
  symbol: class ManagedBean
WEB-INF/classes/bigjava/TimeBean.java:10: error: cannot find symbol
@SessionScoped
 ^
  symbol: class SessionScoped
4 errors

代码:

package bigjava;

import java.text.DateFormat;
import java.util.Date;
import java.util.TimeZone;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class TimeBean
{
   private DateFormat timeFormatter;

   /**
      Initializes the formatter.
   */
   public TimeBean()
   {
      timeFormatter = DateFormat.getTimeInstance();
   }

   /**
      Read-only time property.
      @return the formatted time
   */
   public String getTime()
   {
      Date time = new Date();
      String timeString = timeFormatter.format(time);
      return timeString;
   }
}

所有类文件都放在WEB-INF/classes目录中。 index.html进入WEB-INF并包含:

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
   xmlns:h="http://java.sun.com/jsf/html">
   <h:head>
      <title>The time application</title>
   </h:head>
   <h:body>
      <h:form>
         <p>           
            The current time is #{timeBean.time}
         </p>
      </h:form>
   </h:body>
</html>

web.xml与classes处于同一级别,并包含:

<?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"
   version="2.5">
   <servlet>
      <servlet-name>Faces Servlet</servlet-name>
      <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
   </servlet>
   <servlet-mapping>
      <servlet-name>Faces Servlet</servlet-name>
      <url-pattern>/faces/*</url-pattern>
   </servlet-mapping>
   <welcome-file-list>
      <welcome-file>faces/index.xhtml</welcome-file>
   </welcome-file-list>
   <context-param>
      <param-name>javax.faces.PROJECT_STAGE</param-name>
      <param-value>Development</param-value>
   </context-param>
</web-app>

我可能会失去一些东西,但我无法弄明白到底是什么。

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

您必须将javax.faces.api jar文件添加到项目中。您可以找到此库here

下载此文件后,您必须将其导入项目并将其添加到工件中。

请参阅此链接Error “package javax.faces.bean does not exist”