MyTabs.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Tab view</title>
</h:head>
<h:form style="width:500px;" id="form">
<p:growl id="msgs" showDetail="true" />
<p:tabView dynamic="true">
<p:ajax event="tabChange" listener="#{MyTab.tabChange}" update="p1 p2 p3"/>
<p:tab title="C" id="c">
<h:panelGrid columns="2" cellpadding="10" id="p1">
<h:outputText value="#{MyTab.msg}"></h:outputText>
</h:panelGrid>
</p:tab>
<p:tab title="C++" id="cpp">
<h:panelGrid columns="2" cellpadding="10" id="p2">
<h:outputText value="#{MyTab.msg}"></h:outputText>
</h:panelGrid>
</p:tab>
<p:tab title="Java" id="java">
<h:panelGrid columns="2" cellpadding="10" id="p3">
<h:outputText value="#{MyTab.msg}"></h:outputText>
</h:panelGrid>
<p:tabView dynamic="false">
<f:ajax event="tabChange" listener="#{MyTab.tabChange2}" update="pg1,pg2"/>
<p:tab title="JSP" id="jsp">
<h:panelGrid id="pg1">
<h:outputText value="#{MyTab.javaProg}"></h:outputText>
</h:panelGrid>
</p:tab>
<p:tab title="JSF" id="jsf">
<h:panelGrid id="pg2">
<h:outputText value="#{MyTab.javaProg}"></h:outputText>
</h:panelGrid>
</p:tab>
</p:tabView>
</p:tab>
</p:tabView>
</h:form>
</html>
MyTab.java
package mypak;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.primefaces.event.TabChangeEvent;
@ManagedBean(name="MyTab")
public class MyTab {
private String msg="C";
private String javaProg="j2ee";
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getJavaProg() {
return javaProg;
}
public void tabChange(TabChangeEvent e){
String id=e.getTab().getId();
if(id.equals("c")){
msg="C is a good programming language.";
}else if(id.equals("cpp")){
msg="C++ is better !";
}else{
msg="Java is the best :)";
}
}
public void tabChange2(TabChangeEvent e){
System.out.println("nested");
String id=e.getTab().getId();
if(id.equals("jsp")){
javaProg="JSP is good option";
}
if(id.equals("jsf")){
javaProg="JSF is the better";
}
}
}
点击嵌套标签
时出现错误Sep 16, 2015 9:45:37 PM com.sun.faces.lifecycle.InvokeApplicationPhase execute
WARNING: /MyTabs.xhtml @31,81 listener="#{MyTab.tabChange2}": java.lang.NullPointerException
javax.el.ELException: /MyTabs.xhtml @31,81 listener="#{MyTab.tabChange2}": java.lang.NullPointerException
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:111)
at com.sun.faces.facelets.tag.jsf.core.AjaxBehaviorListenerImpl.processAjaxBehavior(AjaxHandler.java:462)
at org.primefaces.event.TabChangeEvent.processListener(TabChangeEvent.java:42)
at javax.faces.component.behavior.BehaviorBase.broadcast(BehaviorBase.java:106)
at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:809)
at org.primefaces.component.api.UITabPanel.broadcast(UITabPanel.java:1259)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
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:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1527)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1484)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at mypak.MyTab.tabChange2(MyTab.java:38)
at sun.reflect.GeneratedMethodAccessor48.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.el.parser.AstValue.invoke(AstValue.java:247)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:267)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
... 32 more
我使用的是JSF 2,Prime Faces 5.2,Tomcat 8。 这是怎么造成的,我该如何解决?