使用jsf会导致NullpointerException:
WARNING: StandardWrapperValve[jsp]: PWC1406: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at javax.faces.webapp.UIComponentClassicTagBase.setJspId(UIComponentClassicTagBase.java:1858)
at org.apache.jsp.jsf.list_jsp._jspx_meth_h_form_0(list_jsp.java:112)
at org.apache.jsp.jsf.list_jsp._jspService(list_jsp.java:86)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
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:860)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
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)
在我的glassfish-server和我的动态web项目的lib文件夹中,如下所示 罐子:
我的豆子:
@Stateless
@Named
public class StatelessBean {
public int getNumberOfMovies() {
return 42;
}
public int getNumberOfPersons() {
return 42;
}
}
我的jsf:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ 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>
<link href="../css/basic.css" type="text/css" rel="stylesheet">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Liste</title>
</head>
<body>
<h:form>
<h1>Stateless</h1>
<h:outputText value="Anzahl der Filme: #{StatelessBean.getNumberOfMovies()}" />
<h:outputText value="Anzahl der Personen: #{StatelessBean.getNumberOfPersons()}" />
</h:form>
</body>
</html>
这里提供的拳击手:stackoverflow没有帮助。
不知道如何解决这个问题。
答案 0 :(得分:4)
您应该像这样更改您的视图代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.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"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<link href="css/basic.css" type="text/css" rel="stylesheet">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Liste</title>
</h:head>
<h:body>
<h:form>
<h1>Stateless</h1>
<h:outputText value="Anzahl der Filme: #{statelessBean.getNumberOfMovies()}" />
<h:outputText value="Anzahl der Personen: #{statelessBean.getNumberOfPersons()}" />
</h:form>
</h:body>
</html>
这是JSF声明taglibs的方式。
您还需要更改web.xml
中的某些配置,并将文件重命名为yourfile.xhtml
:
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<!-- Use prefix mapping for Facelets pages, e.g. http://localhost:8080/webapp/yourfile.jspx -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jspx</url-pattern>
</servlet-mapping>
要在EL表达式中使用bean,您需要像这样更改它:
@ManagedBean
@RequestScoped
public class StatelessBean {
public int getNumberOfMovies() {
return 42;
}
public int getNumberOfPersons() {
return 42;
}
}
更多信息:
答案 1 :(得分:0)
尝试将调用更改为getNumberOfMovies。 JSF默默地将“get”和括号“()”添加到html中的值。我们的想法是你编写getter和setter,然后命名属性,JSF调用适当的属性。
<h:outputText value="Anzahl der Filme: #{statelessBean.numberOfMovies}" />
<h:outputText value="Anzahl der Personen: #{statelessBean.numberOfPersons}" />
但是,这会调用propertyNotFoundException。它也可能是你的空指针的原因,但我不确定。