我得到空指针异常。不要明白出了什么问题。我使用了普通的JSF 2.2和bootstrap。我不太了解注释,所以我在faces-config.xml文件中添加了手动条目。有什么需要吗?
在控制台中收到以下错误。
Sep 12, 2015 10:14:40 PM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet FacesServlet threw exception
java.lang.NullPointerException
at com.sun.faces.context.flash.ELFlash.loggingGetPhaseMapForReading(ELFlash.java:793)
at com.sun.faces.context.flash.ELFlash.getPhaseMapForReading(ELFlash.java:826)
at com.sun.faces.context.flash.ELFlash.isEmpty(ELFlash.java:484)
at com.sun.faces.facelets.util.DevTools.writeVariables(DevTools.java:325)
at com.sun.faces.facelets.util.DevTools.writeVariables(DevTools.java:215)
at com.sun.faces.facelets.util.DevTools.debugHtml(DevTools.java:130)
at com.sun.faces.renderkit.RenderKitUtils.renderHtmlErrorPage(RenderKitUtils.java:1162)
at com.sun.faces.context.ExceptionHandlerImpl.throwIt(ExceptionHandlerImpl.java:276)
at com.sun.faces.context.ExceptionHandlerImpl.handle(ExceptionHandlerImpl.java:142)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:119)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:659)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:471)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:402)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329)
at com.sun.faces.context.ExternalContextImpl.dispatch(ExternalContextImpl.java:546)
at com.sun.faces.application.view.JspViewHandlingStrategy.executePageToBuildView(JspViewHandlingStrategy.java:364)
at com.sun.faces.application.view.JspViewHandlingStrategy.buildView(JspViewHandlingStrategy.java:154)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:100)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
faces-config.xml中
<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"
version="1.2">
<managed-bean>
<managed-bean-name>SignupBean</managed-bean-name>
<managed-bean-class>com.jsf.beans.SignupBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
</faces-config>
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>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<!-- Welcome page -->
<welcome-file-list>
<welcome-file>faces/views/index.xhtml</welcome-file>
</welcome-file-list>
<!-- JSF mapping -->
<servlet>
<servlet-name>FacesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<!-- Map these files with JSF -->
<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
</web-app>
的index.xhtml
<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>
<title>Registration</title>
<script type="text/javascript" src="/js/jquery-2.1.3.min.js"></script>
<script type="text/javacript" src="/js/jquery-ui.js"></script>
<link rel="stylesheet" href="/css/bootstrap.css"/>
<link rel="stylesheet" href="/css/datestyle.css"/>
<script type="text/javascript">
</script>
</head>
<body class="jumbotron">
<h:form class="form-horizontal">
<div class="container">
<div class="row form-group">
<h:outputLabel class="col-md-2"value="Name"/>
<div class="col-md-3">
<h:inputText id="name" value="#{SignupBean.name}" required="true"/>
<h:message for="name"/>
</div>
</div>
<div class="row form-group">
<h:outputLabel class="col-md-2"value="Password"/>
<div class="col-md-3">
<h:inputSecret id="password" value="#{SignupBean.password}" required="true"/>
<h:message for="password"/>
</div>
</div>
<div class="row form-group">
<h:outputLabel class="col-md-2"value="Date of Birth"/>
<div class="col-md-3">
<h:inputText id="dob" value="#{SignupBean.dob}" required="true"/>
<h:message for="dob"/>
</div>
</div>
<div class="row form-group">
<h:outputLabel class="col-md-2"value="Email"/>
<div class="col-md-3">
<h:inputText id="email" value="#{SignupBean.email}" required="true"/>
<h:message for="email"/>
</div>
</div>
<div class="row form-group">
<h:outputLabel class="col-md-2"value="Phone"/>
<div class="col-md-3">
<h:inputText id="phone" value="#{SignupBean.phone}" required="true"/>
<h:message for="phone"/>
</div>
</div>
<div class="row form-group">
<h:outputLabel class="col-md-2"value="Address"/>
<div class="col-md-3">
<h:inputText id="address" value="#{SignupBean.address}" required="true"/>
<h:message for="address"/>
</div>
</div>
</div>
</h:form>
</body>
</html>
DisplayBean.java
package com.jsf.beans;
public class DisplayBean {
private String name;
private String password;
private String dob;
private String email;
private String phone;
private String address;
public DisplayBean(String name,String password,String dob,String email,String phone,String address)
{
this.name=name;
this.password=password;
this.dob=dob;
this.email=email;
this.phone=phone;
this.address=address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
SignupBean.java
package com.jsf.beans;
import java.util.ArrayList;
public class SignupBean {
private String name;
private String password;
private String dob;
private String email;
private String phone;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public static final ArrayList<DisplayBean> details = new ArrayList<DisplayBean>();
public String setDetails(){
DisplayBean detail = new DisplayBean(name,password,dob,email,phone,address);
details.add(detail);
return "success";
}
}
罐子添加:
commons-beanutils-1.9.2.jar
commons-chain-1.2.jar
commons-collections-3.2.1.jar
commons-logging-1.2.jar
jsf-api-2.2.12.jar
jsf-impl-2.1.7.jar
jstl-1.2.jar
答案 0 :(得分:0)
在使用cdi托管bean时应该使用CDI注入(因为你可能正在学习,因此需要学习才能使用更新版本才能使用感应)。
@Named
@RequestScoped
public class Mybean{
public MyBean(){
}
}
这就是你制作豆子的方法。由于bean由容器管理,这意味着容器本身会实现这些容器。这也意味着你不能自己实现这些并且他们需要一个空的构造函数。
public MyBean(){
}