NullPointerException using JSF 2.3 websocket using JSF navigation - jsf

I have some simple navigation using primefaces menu. However I have jsf websockets on both pages. This causes a NullPointerException when navigating between pages. When navigating to Portfolio if there are websockets present in the Portfolio page the NullPointerException is returned when attempting to navigate to it and back to the Stocks page. If I remove all of the websockets from the Portfolio page, then the navigation kind of works - the first click causes the below exception but the second works. If I remove all websockets from both pages the navigation works without issue, but obviously I need the websockets for the page functionality.
EDIT: I've now made a minimal reproducible example and have changed the code below. Note that h:commandLink are included and they also cause the same issue when selecting page2.
java.lang.NullPointerException
at com.sun.faces.push.WebsocketFacesListener.processEvent(WebsocketFacesListener.java:118)
at javax.faces.event.SystemEvent.processListener(SystemEvent.java:147)
at javax.faces.event.ComponentSystemEvent.processListener(ComponentSystemEvent.java:134)
at com.sun.faces.application.ApplicationImpl.processListenersAccountingForAdds(ApplicationImpl.java:2340)
at com.sun.faces.application.ApplicationImpl.invokeViewListenersFor(ApplicationImpl.java:2158)
at com.sun.faces.application.ApplicationImpl.publishEvent(ApplicationImpl.java:337)
at com.sun.faces.application.ApplicationImpl.publishEvent(ApplicationImpl.java:292)
at javax.faces.application.ApplicationWrapper.publishEvent(ApplicationWrapper.java:748)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:113)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:223)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:671)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1580)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:338)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.glassfish.tyrus.servlet.TyrusServletFilter.doFilter(TyrusServletFilter.java:305)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:250)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:652)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:591)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:371)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:238)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:463)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:168)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:242)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:539)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:593)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:573)
at java.lang.Thread.run(Thread.java:748)
This is my menu page:
<?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:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<f:view>
<h:head>
</h:head>
<h:body>
<div class="column side">
<h2>Side</h2>
<h:form>
<h:commandLink value="Page1" actionListener="#{menuViewTest.selectPageOne}" />
<h:commandLink value="Page2" actionListener="#{menuViewTest.selectPageTwo}" />
</h:form>
</div>
<div class="column middle">
<h2>Middle</h2>
<ui:include src="#{menuViewTest.view}">
</ui:include>
</div>
</h:body>
</f:view>
</html>
Backing bean for MenuTest:
#Named("menuViewTest")
#SessionScoped
public class MenuViewTest implements Serializable {
private static final long serialVersionUID = -1015364935820045523L;
private String view;
public void selectPageOne() {
setView("page1.xhtml");
}
public void selectPageTwo() {
setView("page2.xhtml");
}
public String getView() {
return view;
}
public void setView(String view) {
this.view = view;
}
public String getSelectPageOne(){
return "page1.xhtml";
}
public String getSelectPageTwo(){
return "page2.xhtml";
}
}
Page1:
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<script type="text/javascript">
function notifyListener(message, channel, event) {
console.log("notifyListener message: " + message);
console.log("notifyListener channel: " + channel);
console.log("notifyListener event: " + event);
}
</script>
<h2>Page 1</h2>
<f:websocket channel="notify" onmessage="notifyListener" />
</ui:composition>
Page 2:
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<script type="text/javascript">
function notifyListener(message, channel, event) {
console.log("notifyListener message: " + message);
console.log("notifyListener channel: " + channel);
console.log("notifyListener event: " + event);
}
</script>
<h2>Page 2</h2>
<f:websocket channel="notify2" onmessage="notifyListener" />
</ui:composition>

The websocket exception is fixed by upgrading to the latest stable release of Glassfish (5.1.0) JSF version 2.3 with mojarra 2.3.9.

Related

Advanced fileUpload get the file null with Primefaces [duplicate]

This question already has answers here:
How to use PrimeFaces p:fileUpload? Listener method is never invoked or UploadedFile is null / throws an error / not usable
(11 answers)
Closed 5 years ago.
Environment:
Primefaces 6.1
JavaEE 7
Wildfly 10
JSF 2.2
when I click the button after having selected the file, the file var is null.
Test.xhtml
<!DOCTYPE html>
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<f:view contentType="text/html">
<h:head>
<title>Application</title>
</h:head>
<h:body>
<h:form id="frmTest" enctype="multipart/form-data">
<ui:include src="template/messages.xhtml" />
<p:fileUpload fileUploadListener="#{fileUploadView.handleFileUpload}" mode="advanced" dragDropSupport="false"
update="messages" sizeLimit="100000" fileLimit="3" allowTypes="/(\.|\/)(gif|jpe?g|png)$/" />
</h:form>
</h:body>
</f:view>
</html>
TestBean.java
#ViewScoped
#ManagedBean
public class TestBean implements Serializable {
...
public void handleFileUpload(FileUploadEvent event) {
UploadedFile file = event.getFile();//file is null
FacesUtils.addInfoMessage("Succesful" + file.getFileName() + " is uploaded.");
System.out.println("Succesful" + event.getFile().getFileName() + " is uploaded.");
}
...
You should remove or fill the update="" attribute from your fileUpload component. It "redirects" the files to a component after upload.

Javax ViewExpiredException thrown upon POST

I try to understand why I get a javax ViewExpiredException on my simple webapp - but I don't seem to understand what is causing the view to expire.
This is the register.jsf:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:b="http://bootsfaces.net/ui"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
template="pageTemplate.xhtml">
<ui:define name="content">
<b:panel title="Registrierung" look="primary">
<h:form>
<h:panelGrid>
<h:outputText value="Name:"/>
<b:inputText value="#{registerController.user.name}" placeholder="Robina Kuh"/>
<h:outputText value="E-Mail:"/>
<b:inputText value="#{registerController.user.email}" placeholder="robina.kuh#oc.com" size="32">
<f:facet name="prepend">
<h:outputText value="#" />
</f:facet>
</b:inputText>
<b:commandButton value="Registrieren" icon="envelope" action="#{registerController.registerUser}"/>
</h:panelGrid>
</h:form>
</b:panel>
</ui:define>
</ui:composition>
The template:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:b="http://bootsfaces.net/ui"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>Lunch</title>
</h:head>
<h:body style="padding: 60px;">
<ui:include src="topMenu.xhtml" />
<ui:insert name="content">
<b:container>
<b:jumbotron>
<h1>Da ist wohl etwas schiefgelaufen... Sorry!</h1>
</b:jumbotron>
</b:container>
</ui:insert>
</h:body>
</html>
This is the controller:
#Named("registerController")
#SessionScoped
public class RegisterController implements Serializable {
#Inject
private UserManager userManager;
private User user;
private Logger logger = Logger.getLogger(RegisterController.class);
public RegisterController() {
logger.debug("Created RegisterController");
user = new User();
if(user != null)
logger.debug("Name: " + user.getName()
+"\nEmail: " + user.getEmail());
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String registerUser() {
logger.debug("registerUser called"
+ "\n Name: " + user.getName()
+"\nEmail: " + user.getEmail());
userManager.addUser(user);
logger.debug("registerUser end");
return "benutzer.jsf";
}
}
It even is not entering the action method of my commandButton when I try to debug it (I am using the Bootsfaces Framework but I don't think this has anything to do with that Framework).
Setting the save state to client works but I would like to understand what is the problem? From what I am understanding of JSF this should work without pushing the state to the client side.
Am I missing something fundamental?
I deployed the webapp to a widlfly 9 server.
It was a bug in the used wildfly version - just used the new widlfly 10 final release and it is working as it should.

<f:view locale=""/> throws NullPointerException

I'm developing an web application using JSF and I need to make the web available in other languages. I haven't developed the app, I have to continue its development. In some views the following is used:
<f:view locale="#{nuevoProyectoBacking.locale}"/>
<f:loadBundle basename="com.company.resources.resdisenyo" var="rDisenyo"/>
So I've created a new JSF page and done the same:
<?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:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<ui:composition template="/WEB-INF/templates/template.xhtml">
<f:event type="preRenderView"
listener="#{nuevoProyectoBacking.forwardIfIsNotAdmin}" />
<f:view locale="#{nuevoProyectoBacking.locale}"/>
<f:loadBundle basename="com.appglass.resources.resdisenyo" var="rDisenyo"/>
<ui:define name="top">
<ui:decorate template="/WEB-INF/templates/menuAdministrador.xhtml"></ui:decorate>
</ui:define>
etc
It only works if I remove f:view locale="#{nuevoProyectoBacking.locale}"
nuevoProyectoBacking.java
#ManagedBean
#ViewScoped
public class NuevoProyectoBacking implements Serializable{
definitions
/** Creates a new instance of NuevoProyectoBacking */
public NuevoProyectoBacking()
{
many assignments
locale = LocaleUtil.getIdioma(getCurrentUser().getIdioma());
}
public String getLocale() {
return locale;
}
public void setLocale(String locale) {
this.locale = locale;
}
public Usuarios getCurrentUser()
{
return (Usuarios) getSessionMap().get("currentUser");
}
public Map<String, Object> getSessionMap() {
FacesContext context = FacesContext.getCurrentInstance();
Map<String, Object> sessionMap = context.getExternalContext().getSessionMap();
return sessionMap;
}
}
LocaleUtil
public class LocaleUtil {
public static String getIdioma(Integer idioma){
if(idioma == 0){
return "es_ES";
} else if (idioma == 1) {
return "en_US";
} else if (idioma == 2) {
return "it_IT";
} else if (idioma == 3) {
return "fr_FR";
} else if (idioma == 4) {
return "pt_PT";
} else if (idioma == 5) {
return "ru_RU";
} else if (idioma == 6) {
return "pl_PL";
}else {
return "es_ES";
}
}
}
Error
Warning: StandardWrapperValve[Faces Servlet]: Servlet.service() for servlet Faces Servlet threw exception
java.lang.NullPointerException
at com.sun.faces.mgbean.BeanManager$ScopeManager$ViewScopeHandler.getFromScope(BeanManager.java:566)
at com.sun.faces.mgbean.BeanManager$ScopeManager.getFromScope(BeanManager.java:477)
at com.sun.faces.mgbean.BeanManager.getBeanFromScope(BeanManager.java:240)
at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:255)
at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:117)
at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
at com.sun.el.parser.AstIdentifier.getValue(AstIdentifier.java:116)
at com.sun.el.parser.AstValue.getBase(AstValue.java:151)
at com.sun.el.parser.AstValue.getValue(AstValue.java:200)
at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:226)
at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
at com.sun.faces.facelets.tag.TagAttributeImpl.getObject(TagAttributeImpl.java:356)
at com.sun.faces.facelets.tag.TagAttributeImpl.getObject(TagAttributeImpl.java:276)
at com.sun.faces.facelets.tag.jsf.core.ViewHandler.apply(ViewHandler.java:211)
at com.sun.faces.facelets.compiler.NamespaceHandler.apply(NamespaceHandler.java:93)
at com.sun.faces.facelets.compiler.EncodingHandler.apply(EncodingHandler.java:87)
at com.sun.faces.facelets.impl.DefaultFacelet.apply(DefaultFacelet.java:161)
at com.sun.faces.application.view.ViewMetadataImpl.createMetadataView(ViewMetadataImpl.java:140)
at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:241)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:121)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:70)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
at java.lang.Thread.run(Thread.java:745)
I don't think the problem is in nuevoProyectoBacking as it is used by another JSF Page and it works well, language too. I have to remove that one and create a new one with a new look and feel.
I've fixed it but I don't know why xDDDD
This is my new JSF page
<?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:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<ui:composition template="/WEB-INF/templates/template.xhtml">
<f:event type="preRenderView"
listener="#{nuevoProyectoBacking.forwardIfIsNotAdmin}" />
<ui:define name="top">
<ui:decorate template="/WEB-INF/templates/menuAdministrador.xhtml"></ui:decorate>
</ui:define>
<ui:define name="content">
<ui:decorate template="/nuevoProyectoTemplate2.xhtml"></ui:decorate>
</ui:define>
</ui:composition>
</html>
I've written all the HTML code inside /nuevoProyectoTemplate2 and it works.

f:param value is returning null both with commandButton and also commandLink

I am doing a simple navigation example in jsf as i am a beginner. i am always getting null when accessing the f:param value in the managedBean using ManagedProperty
home.xhtml
<!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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/facelets">
<head>
<title>JSF Tutorial!</title>
</head>
<body>
<h3>Using JSF outcome</h3>
<h:form>
<h:commandButton action="#{navigation.show}" value="Page1">
<f:param name="pageId" value="1" />
</h:commandButton>
<h:commandLink action="#{navigation.show}" value="Page2">
<f:param name="pageId" value="2" />
</h:commandLink>
<h:commandLink action="#{navigation.show}" value="Home">
<f:param name="pageId" value="3" />
</h:commandLink>
</h:form>
Navigation.java
package com.jason.jsf;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
#ManagedBean(name = "navigation", eager = true)
#RequestScoped
public class Navigation {
#ManagedProperty(value = "#{param.pageId}")
private String pageId;
public String show() {
System.out.println("page id" + value);
if (pageId == null) {
return "home";
}
if (pageId.equals("1")) {
return "page1";
} else if (pageId.equals("2")) {
return "page2";
} else {
return "home";
}
}
public String getPageId() {
return pageId;
}
public void setPageId(String pageId) {
System.out.println("page id set" + pageId);
this.pageId = pageId;
}
}
How is this caused and how can I solve it? I am using jsf2.2 Mojarra 2.0.3.there are other sample page1.xhtml and page2.xhtml just for navigation with me
Thanks in advance
Look closer at the XML namespace prefix and the URI and compare with whatever is shown in a decent JSF book/tutorial/resource:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/facelets">
Yes, the XML namespace URI for the f: prefix is wrong. You declared it to be the one of Facelets tags which have usually ui: prefix. This basically causes those tags to not be properly interpreted at all. It's being misinterpreted as an <ui:param> which has an entirely different meaning than the real <f:param>.
Fix the taglib URI. It needs to be http://java.sun.com/jsf/core. Here's the complete set:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
See also:
Our JSF wiki page - contains a Hello World
Unrelated to the concrete problem, Mojarra 2.0.3 is not JSF 2.2. It's JSF 2.0. And a rather old implementation too, over 5 years already. You can get latest Mojarra 2.2 (currently 2.2.11) at http://javaserverfaces.java.net. After that, you can change the domain in taglib URIs from java.sun.com to xmlns.jcp.org:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">

JSF command button not working within included JSF page

I'm try to build application with Primefaces 4.0 and JSF 2.2.5. I need to load content dynamically in accordance with choosen menu item.
Here is my main page:
<?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:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>Hello, world!</title>
</h:head>
<h:body>
<p:layout fullPage="true">
<p:layoutUnit position="west" size="15%">
<h:form>
<p:commandButton value="List1" action="#{backingBean.setCurrentPage('included.xhtml')}"
update=":mypanel_id" process="#this"/><br/>
<p:commandButton value="List2"/>
</h:form>
</p:layoutUnit>
<p:layoutUnit position="center">
<p:panel id="mypanel_id">
<ui:include src="#{backingBean.page}"/>
</p:panel>
<p:messages autoUpdate="true" showDetail="true" showSummary="true"/>
</p:layoutUnit>
</p:layout>
</h:body>
</html>
And this is included page:
<?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:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Hello,world</title>
</h:head>
<h:body>
<h:outputText value="Included page"/>
<h:form>
<ui:repeat value="#{backingBean.items}" var="item">
<p:fieldset legend="item" toggleable="true">
<h:outputText value="#{item}"/>
<ui:param name="it" value="#{item}"/>
<p:commandButton value="Click" style="margin-left: 50px;"
actionListener="#{backingBean.actionListener}"/>
</p:fieldset>
</ui:repeat>
<p:commandButton value="Test" action="#{backingBean.actionListener}"/>
</h:form>
</h:body>
</html>
Command buttons not work. Not with action nor with actionListener. What i'm doing wrong? How to build page with conditionally rendered elements, such as command buttons and fieldsets?
P.S. Forget to say, that my bean have request scope.
Updated:
public class BackingBean {
private List<String> items;
private String currentItem;
private String page;
public BackingBean() {
items = new ArrayList<String>();
}
public List<String> getItems() {
if (items.size() == 0) {
this.fillAndUpdate();
}
return items;
}
public void setItems(List<String> items) {
this.items = items;
}
public void fillAndUpdate() {
for (int i = 0; i < 5; i++) {
items.add("Item " + String.valueOf(i));
}
}
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
public void setCurrentPage(String page) {
this.page = page;
}
public void actionListener() {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Test"));
}
}
UPDATED
Ok. I found the error (if someone interested). Because my bean has request scope, when i click the button in right panel my view was updated, but list of items was updated too and became empty (request scope). Now i keep selected menu in the session bean, and return that number when view rendered.
try to add ajax="false" in your command buttons

Resources