我现在在学习JSF,我试着做了一个HelloWorld,最简单的那种
运行出错
如下:
type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception org.apache.jasper.JasperException: java.lang.NullPointerException
 org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:522)
 org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:356)
 org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
 org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
 com.sun.faces.context.ExternalContextImpl.dispatch(ExternalContextImpl.java:322)
 com.sun.faces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:142)
 com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:87)
 com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)
 com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:117)
 javax.faces.webapp.FacesServlet.service(FacesServlet.java:198)
root cause java.lang.NullPointerException
 org.apache.jsp.pages.index_jsp._jspInit(index_jsp.java:31)
 org.apache.jasper.runtime.HttpJspBase.init(HttpJspBase.java:52)
 org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:159)
 org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:329)
 org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
 org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
 com.sun.faces.context.ExternalContextImpl.dispatch(ExternalContextImpl.java:322)
 com.sun.faces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:142)
 com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:87)
 com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)
 com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:117)
 javax.faces.webapp.FacesServlet.service(FacesServlet.java:198) 
问题补充:这是我的index页面
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>第一个JSP</title>
</head>
<body>
<f:view>
<h:form>
<h3>请输入名字:</h3>
名称:<h:inputText value="#{user.name}"></h:inputText>
<h:commandButton value="确定" action="login"></h:commandButton>
 </h:form>
</f:view>
</body>
</html>
这是我的faces-config配置文件:
<faces-config>
     <navigation-rule>
         <from-view-id>/pages/index.jsp</from-view-id>
         <navigation-case>
             <from-outcome>login</from-outcome>
             <to-view-id>/pages/welcome.jsp</to-view-id>
         </navigation-case>
              </navigation-rule>
     <managed-bean>
         <managed-bean-name>user</managed-bean-name>
         <managed-bean-class>
             cn.javass.faces.UserBean
         </managed-bean-class>
         <managed-bean-scope>session</managed-bean-scope>
     </managed-bean>
</faces-config>

解决方案 »

  1.   

    <%@ page language="java" contentType="text/html; charset=gb2312"
    pageEncoding="gb2312"%>
    <%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
    <%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>第一个JSP</title>
    </head>
    <body>
    <f:view>
    <h:form>
    <h3>请输入名字:</h3> 
    名称: <h:inputText value="#{user.name}">
    </h:inputText>
    <h:commandButton value="确定" action="login">
    </h:commandButton>
    </h:form>
    </f:view>
    </body>
    </html>
      

  2.   

    另外,这种方式不那么流行了,现在2.0惯用xhtml格式。
      

  3.   

    <?xml version="1.0" encoding="gb2312" ?>
    <!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:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>第一个JSP</title>
    </head>
    <body>
    <f:view>
    <h:form>
    <h3>请输入名字:</h3> 
    名称: <h:inputText value="#{user.name}">
    </h:inputText>
    <h:commandButton value="确定" action="login">
    </h:commandButton>
    </h:form>
    </f:view>
    </body></html>
      

  4.   

    注意下web.xml里的配置。给你介绍本书:http://book.51cto.com/art/200812/100238.htm,比较新,还是中文版的,不过只有部分章节。
      

  5.   

    改成你的页面后错误成这样了:
    org.apache.jasper.JasperException: /pages/index.jsp(15,29) #{..} is not allowed in template text
    org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
    org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
    org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:102)
    org.apache.jasper.compiler.Validator$ValidateVisitor.visit(Validator.java:711)
    org.apache.jasper.compiler.Node$ELExpression.accept(Node.java:940)
    org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2343)
    org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2393)
    org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2399)
    org.apache.jasper.compiler.Node$Root.accept(Node.java:489)
    org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2343)
    org.apache.jasper.compiler.Validator.validate(Validator.java:1737)
    org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:165)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:314)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:294)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:281)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:566)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    com.sun.faces.context.ExternalContextImpl.dispatch(ExternalContextImpl.java:322)
    com.sun.faces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:142)
    com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:87)
    com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)
    com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:117)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:198)
      

  6.   

    上面那段代码是jsp格式的,下面那段是xhtml格式的。
    还有你定义和配置了user这个bean吗?
      

  7.   

    上面的两种方式我在我的机器上都试验通过,你看看环境吧,比如你的jsf的实现版本,以及web.xml。
      

  8.   

    我的web.xml文件:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <description>JSF Demo</description>
    <display-name>JSF Demo</display-name>
        <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>index.xhtml</welcome-file>
        </welcome-file-list>
        </web-app>
      

  9.   

    <?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>*.jsf</url-pattern>
    </servlet-mapping>
    <context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
    </context-param>
    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.html</welcome-file>
    </welcome-file-list> <context-param>
    <param-name>facelets.LIBRARIES</param-name>
    <param-value> /WEB-INF/facelets/mycustom.taglib.xml</param-value>
    </context-param></web-app>
      

  10.   

     <html>
     <head>
     <meta http-equiv="Refresh" content= "0; URL=index.jsf"/>
     <title>Start Web Application</title>
     </head>
     <body>
     <p>Please wait for the web application to start.</p>
     </body>
     </html>这是index.html。
    那个页面定义为index.xhtml。