我是jsf的新手,当我检查我的代码时,我打开浏览器时总会收到此异常:
org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path [/SampleApplication] threw exception [/login.xhtml @11,69 value="#{nameWrapper.name}": Target Unreachable, identifier 'nameWrapper' resolved to null] with root cause
javax.el.PropertyNotFoundException: Target Unreachable, identifier 'nameWrapper' resolved to null
at org.apache.el.parser.AstValue.getTarget(AstValue.java:98)
at org.apache.el.parser.AstValue.getType(AstValue.java:82)
at org.apache.el.ValueExpressionImpl.getType(ValueExpressionImpl.java:172)
at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:98)
at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:95)
at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1046)
at javax.faces.component.UIInput.validate(UIInput.java:976)
at javax.faces.component.UIInput.executeValidate(UIInput.java:1249)
at javax.faces.component.UIInput.processValidators(UIInput.java:712)
at javax.faces.component.UIForm.processValidators(UIForm.java:253)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1261)
at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1195)
at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
这是我的代码:
<h:form>
Name:
<h:inputText id="text1" value="#{nameWrapper.name}" />
<br />
Address:
<h:inputText id="text2" value="#{nameWrapper.address}" />
<br />
Zip:
<h:inputText id="text3" value="#{nameWrapper.zip}" />
<br />
<h:commandButton id="submit" value="Submit" action="PageTwo" />
</h:form>
和
Your password is <h:outputText id="outtext" value="#{nameWrapper.outtext}" />!
和
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class NameWrapper {
private String name;
private String address;
private String zip;
@SuppressWarnings("unused")
private String outtext;
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 String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public String getOuttext() {
return name + zip + address;
}
public void setOuttext(String outtext) {
this.outtext = outtext;
}
}
代码来自一个教程,我没有弄清楚我做错了什么,我跟着关于这个例外的所有答案,我仍然没有成功解决它,谢谢!
答案 0 :(得分:0)
您必须使用JSF标准标记(h:head,h:body),我在下面给出了一个示例,并且还在ManagedBean中实现了Serializable。
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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Test</title>
</h:head>
<h:body>
<h:form>
Name:
<h:inputText id="text1" value="#{nameWrapper.name}" />
<br />
Address:
<h:inputText id="text2" value="#{nameWrapper.address}" />
<br />
Zip:
<h:inputText id="text3" value="#{nameWrapper.zip}" />
<br />
<h:commandButton id="submit" value="Submit" action="PageTwo" />
</h:form>
</h:body>
</html>
ManagedBean
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name = "nameWrapper")
@SessionScoped
public class NameWrapper implements Serializable{
private String name;
private String address;
private String zip;
@SuppressWarnings("unused")
private String outtext;
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 String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public String getOuttext() {
return name + zip + address;
}
public void setOuttext(String outtext) {
this.outtext = outtext;
}
}
PageTwo的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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>PageTwo</title>
</h:head>
<h:body>
PageTwo
</h:body>
</html>