Error using Switch Case with PrimeFaces - jsf

I'm trying to use Switch Case for show multiple components in my xhtml, but it doesnt work. As result it shows all the components. I'm using PrimeFaces 6.1 , Eclipse Mars
I'm trying to use Switch Case for show multiple components in my xhtml, but it doesnt work. As result it shows all the components. I'm using PrimeFaces 6.1 , Eclipse Mars
My Beans
package vista;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
public class Prueba {
#ManagedBean
#ViewScoped
public class SwitchController {
private String value=null;
public String getValue() {
return value;
}
public void setValue(final String value) {
this.value = value;
}
}
}
My 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: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"
xmlns:c="http://java.sun.com/jstl/core"
xmlns:pe="http://primefaces.org/ui/extensions">
<h:head>
<title>prueba</title>
</h:head>
<body>
<h:form id="mainForm">
<p:growl id="growl" showDetail="true" />
<p:selectOneMenu id="caseSelection" value="#{switchController.value}">
<f:selectItem itemLabel="Default case" itemValue="default" />
<f:selectItem itemLabel="Case 1" itemValue="case1" />
<f:selectItem itemLabel="null" itemValue="#{null}" />
<f:selectItem itemLabel="Case 2" itemValue="case2" />
<p:ajax update="swichWrapper" process="#this" />
</p:selectOneMenu>
<p:separator />
<p:outputPanel id="swichWrapper">
<pe:switch id="switch" value="#{switchController.value}">
<pe:defaultCase>
Case: Default
</pe:defaultCase>
<pe:case value="case1">
Case: <p:commandButton id="case1button" actionListener="#{switchController.listener('case1')}" update=":mainForm:growl"
value="Call listener with 'case1'" />
</pe:case>
<pe:case value="case2">
Case: <p:commandButton id="case2button" actionListener="#{switchController.listener('case2')}" update=":mainForm:growl"
value="Call listener with 'case2'" />
</pe:case>
<pe:case value="#{null}">
Case: Null
</pe:case>
</pe:switch>
</p:outputPanel>
</h:form>
</body>
</html>
Result
enter image description here

Based on your comment your problem most probably is that you are missing Primefaces extensions library. Here is what you should add to your pom.xml
<dependency>
<groupId>org.primefaces.extensions</groupId>
<artifactId>primefaces-extensions</artifactId>
<version>6.1.0</version>
</dependency>

Related

p:remoteCommand run on page load instead of button's onclick event in javascript

I have to call a remote command through javascript. The remote command is working fine but it invokes on page load instead of the button's onclick event.
Following is my xhtml file code as well as the bean code.
<?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:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Crowd Flow</title>
</h:head>
<f:event listener="#{task.loadTaskByTaskId(param['task_id'])}"
type="preRenderView" />
<h:body bgcolor="white">
<h:outputScript>
function getCurrentLocation() {
testingCommand();
}
</h:outputScript>
<h:panelGroup id="header" layout="block">
<h3>Welcome to the task.</h3>
</h:panelGroup>
<h:panelGroup id="taskLayout" layout="block">
<h:outputText value="Category is #{task.taskCategory}"></h:outputText>
<h:outputText value="Task id is is #{task.id}"></h:outputText>
<c:if test="#{param['task_id']=='9'}">
<h:form style="width:400px;" id="newTagTaskForm">
<p:remoteCommand name="testingCommand"
actionListener="#{task.testingCommand()}" autoRun="false" />
Task Name : <h:outputText id="taskName"
value="#{task.newPlaceTagTask.name}"></h:outputText>
<br />
Task Description : <h:outputText id="taskDescription"
value="#{task.newPlaceTagTask.description}"></h:outputText>
<br />
Address : <h:outputText id="taskAddress"
value="#{task.newPlaceTagTask.location.address}"></h:outputText>
<br />
Image : <h:graphicImage width="50" height="50"
url="#{task.newPlaceTagTask.location.image}" />
<br />
<h:inputHidden value="#{task.userId}" id="taskPlaceTagUserId"
name="taskPlaceTagUserId" />
<h:inputHidden value="#{task.newPlaceTagTask.location.id}"
id="taskPlaceTagLocationId" name="taskPlaceTagLocationId" />
<h:commandButton id="testing" value="submit" type="submit" onclick="getCurrentLocation();return false;" ></h:commandButton>
</h:form>
</c:if>
</h:panelGroup>
</h:body>
</html>
Here is the bean function, bean is view scoped.
package com.project.crowdsource.beans;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
#ManagedBean(name="task")
#ViewScoped
public class TaskBean implements Serializable {
public TaskBean() {
//initiating user's data access object.
taskDao = new TaskDAO();
}
public String testingCommand() {
System.out.println("in action testing command");
return "";
}
}
Any help would be highly appreciated.
Thanks,

how to hide or show a textbox depending on radiobutton value in jsf

I have a comment box and a need to show and hide the comment box depending on radio button select above. I dont want to use jquery and i am not getting any idea as am new to jsf primeface
My code is bellow is:
<h:outputText value="Action :" class="alignment"/>
<p:selectOneRadio id="console"
value="#{examinationFormBean.action}"
required="true" label="Action">
<f:selectItem itemLabel="Ok" itemValue="ok" />
<f:selectItem itemLabel="Have Issued"
itemValue="haveissue" />
</p:selectOneRadio>
<h:outputLabel value="Comment:"></h:outputLabel>
<h:inputText id="compid"
value="#{examinationFormBean.comment}"/>
<p:commandButton value="Proceed to Zonal Electrical Inspector>>"
action="#{examinationFormBean.saveFormStatusByOfficeAssistent()}"
update="messageid"
process="data"
oncomplete="if (args && !args.validationFailed) PF('test').hide()" >
<f:ajax update="datatable"/>
</p:commandButton>
Please help me to sort it out. thanks in advance
You can achieve by using JSF EL.
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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>hide some component</title>
</h:head>
<h:body>
<h:form>
<p:selectOneRadio id="console"
value="#{examinationFormBean.radioVal}"
required="true" label="Action">
<f:selectItem itemLabel="Ok" itemValue="ok" />
<f:selectItem itemLabel="Have Issued"
itemValue="haveissue" />
<p:ajax process="console"
update="#form" />
</p:selectOneRadio>
<h:outputLabel value="Comment:"
rendered="#{examinationFormBean.radioVal eq 'haveissue'}">
</h:outputLabel>
<h:inputText id="compid"
value="#{examinationFormBean.comment}"
rendered="#{examinationFormBean.radioVal eq 'haveissue'}"/>
</h:form>
</h:body>
</html>
managedbean
#ViewScoped
#ManagedBean(name="examinationFormBean")
public class ExaminationFormBean implements Serializable{
private String comment;
private String radioVal;
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public String getRadioVal() {
return radioVal;
}
public void setRadioVal(String radioVal) {
this.radioVal = radioVal;
}
}

Primefaces: global message not showing in dialog on first access

i am trying to show a globalMessage in the main form as well as within a dialog. The message within the dialog only shows when the dialog is opened a second time.
I can't get it to work. Can anyone solve this problem? I'd really appreciate it!
I am using JSF Mojarra 2.2.1, Primefaces 4.0 and Apache Tomcat 7.0.
Here is my view (testView.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"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head></h:head>
<h:body>
<h:form id="testForm">
<p:messages globalOnly="true" showDetail="true" closable="true" />
<p:commandButton value="ClickMe" action="#{testBean.displayMessage()}"
update="testForm :testDialogForm" oncomplete="PF('testDialog').show()" />
</h:form>
<p:dialog id="testDialogId" widgetVar="testDialog"
dynamic="true" closable="true" closeOnEscape="true"
header="Dialog with message">
<h:form id="testDialogForm">
<p:messages globalOnly="true" showDetail="true" closable="true" />
<h:outputText value="This outputText should not be alone." />
</h:form>
</p:dialog>
</h:body>
Here is my bean (TestBean.java):
package de.simply.local.tbx.webbeans;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
#ManagedBean(name = "testBean")
#ViewScoped
public class TestBean {
public void displayMessage() {
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_INFO,
"This is the summary.", "These are the details."));
}
}

Viewscoped Managed Bean recreates on every request using ui:composition (Mojarra 2.2.4)

I'm trying to understand why a viewscoped view is recreating on every ajax call.
I have a test case, and it's working fine without composition templating.
I find questions that seem similar to my problem but the solutions don't work for me, like setting javax.faces.PARTIAL_STATE_SAVING to false or javax.faces.STATE_SAVING_METHOD to client.
Here's a sample:
page.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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets"
>
<h:body>
<h:form id="pageForm">
<p:commandButton
value="Choose an option..."
actionListener="#{pageController.handleOptionChoose}"
update="selectedOption"
/>
<br/>
<h:panelGrid columns="2" cellpadding="3">
<p:outputLabel for="selectedOption" value="Selected option: "/>
<p:outputLabel id="selectedOption" value="#{pageController.selectedOption}"/>
</h:panelGrid>
<br/><br/>
<p:commandButton
value="Edit option"
actionListener="#{pageController.doAnythingWithTheOption}"
update="editedOption"
/>
<br/>
<h:panelGrid columns="2" cellpadding="3">
<p:outputLabel for="editedOption" value="Edited option: "/>
<p:outputLabel id="editedOption" value="#{pageController.editedOption}"/>
</h:panelGrid>
</h:form>
<p:dialog
id="dialogOption_Dialog"
header="Choose an option"
widgetVar="dialogOption_Widget"
modal="true"
appendTo="#(body)"
>
<h:form id="dialogOption_Form">
<h:panelGrid columns="1">
<p:selectOneButton id="dialogOptionChoose" value="#{pageController.selectedOption}" >
<f:selectItem itemLabel="Option 1" itemValue="Option 1" />
<f:selectItem itemLabel="Option 2" itemValue="Option 2" />
<f:selectItem itemLabel="Option 3" itemValue="Option 3" />
</p:selectOneButton>
<p:spacer width="0" height="20" />
<p:commandButton
value="Done"
action="#{pageController.showOption}"
update=":pageForm:selectedOption"
oncomplete="dialogOption_Widget.hide();"
/>
</h:panelGrid>
</h:form>
</p:dialog>
</h:body>
</html>
content.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:h="http://java.sun.com/jsf/html"
xmlns:c="http://java.sun.com/jstl/core"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:mj="http://mojarra.dev.java.net/mojarra_ext"
xmlns:o="http://omnifaces.org/ui"
xmlns:of="http://omnifaces.org/functions">
<h:body>
<ui:composition template="/layout.xhtml">
<ui:define name="content">
<ui:include src="page.xhtml" />
</ui:define>
</ui:composition>
</h:body>
</html>
layout.xhtml
<!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"
xmlns:p="http://primefaces.org/ui"
xmlns:o="http://omnifaces.org/ui"
xmlns:of="http://omnifaces.org/functions">
<f:view contentType="text/html" transient="true">
<h:head>
<h:outputStylesheet name="theme.css" library="css" />
<f:facet name="first">
<f:metadata>
<meta http-equiv="X-UA-Compatible" content="IE=8" />
</f:metadata>
<title>Teste</title>
<link rel="icon" type="image/png" href="resources/css/icons/favicon.ico" />
</f:facet>
</h:head>
<h:body>
<p:layout fullPage="true" >
<p:layoutUnit id="top" position="north" size="50" style="border: 2px solid Black !important;">
<!-- <ui:insert name="menubar" /> -->
</p:layoutUnit>
<p:layoutUnit id="center" position="center" style="border: 2px solid Black !important;">
<ui:insert name="content" />
</p:layoutUnit>
<p:layoutUnit id="bottom" position="south" size="60" resizable="true" collapsible="true" style="border: 2px solid Black !important;">
<!-- <ui:insert name="footer" /> -->
</p:layoutUnit>
</p:layout>
</h:body>
</f:view>
</html>
PageController.java
package com.ericsantanna.grendel.mBeans;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import org.primefaces.context.RequestContext;
/**
* #author Eric Sant'Anna
*
*/
#ManagedBean
#ViewScoped
public class PageController {
private String selectedOption;
private String editedOption;
public PageController() {
System.out.println("RECREATING VIEW");
}
public void showOption() {
System.out.println("Selected option number: " + selectedOption.substring(7));
}
public void doAnythingWithTheOption() {
System.out.print("Trying to a substring in the selected option: ");
editedOption = selectedOption.substring(7);
System.out.println(editedOption);
}
public void handleOptionChoose() {
RequestContext.getCurrentInstance().execute("dialogOption_Widget.show();");
}
public String getSelectedOption() {
return selectedOption;
}
public void setSelectedOption(String selectedOption) {
this.selectedOption = selectedOption;
}
public String getEditedOption() {
return editedOption;
}
public void setEditedOption(String editedOption) {
this.editedOption = editedOption;
}
}
JBoss log (setup):
17:45:29,494 INFO [org.jboss.ejb.client] (MSC service thread 1-7) JBoss EJB Client version 1.0.5.Final
17:45:29,550 INFO [javax.enterprise.resource.webcontainer.jsf.config] (MSC service thread 1-8) Inicializando Mojarra 2.2.4 ( 20131003-1354 https://svn.java.net/svn/mojarra~svn/tags/2.2.4#12574) para o contexto '/JobSchedulerWeb'
17:45:30,344 INFO [org.hibernate.validator.util.Version] (MSC service thread 1-8) Hibernate Validator 4.2.0.Final
17:45:30,367 INFO [org.primefaces.webapp.PostConstructApplicationEventListener] (MSC service thread 1-8) Running on PrimeFaces 4.0
17:45:30,368 INFO [org.omnifaces.eventlistener.VersionLoggerEventListener] (MSC service thread 1-8) Using OmniFaces version 1.5
To reproduce this code, simply choose an option and click "Edit option" to do a substring in the backing bean, only to get a NullPointerException when using facelets templating. (Look at the console in each step)
Anyone can help?
This,
<f:view ... transient="true">
turns off JSF state saving for the current view. Essentially, this JSF view is stateless. As there's no view state, there's no means of a view scope and the logical consequence is that the view scoped bean instance can't be saved anywhere. It'll disappear in nowhere by end of the request and thus behave like a request scoped bean.
The observed symptoms are thus fully expected. If that wasn't your intent, then you need to remove the transient="true" attribute. It defaults to false.
This has completely nothing to do with templating. The <ui:composition> plays no significant role here. You'd have had exactly the same problem when merging the templates into a single template without <ui:composition>, <ui:define>, etc while keeping the same markup.
See also:
Why JSF saves the state of UI components on server?
javax.faces.application.ViewExpiredException: View could not be restored

JSF Locale Change Listener not persistent

In my WebApp I created a managed Bean that allows me to change the Locale from French to english and vice versa using Event Change Listener.
package beans;
import java.util.Locale;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
#ManagedBean
#SessionScoped
public class LocaleBean {
private Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
public Locale getLocale() {
return locale;
}
public String getLanguage() {
return locale.getLanguage();
}
public void setLanguage(String language) {
locale = new Locale(language);
FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
}
}
and in my template.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 lang="#{localeBean.language}"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.prime.com.tr/ui"
xmlns:f="http://java.sun.com/jsf/core">
<f:view contentType="text/html" locale="#{localeBean.locale}" id="mescoca">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><ui:insert name="title"></ui:insert></title>
<h:outputStylesheet name="css/jsfcrud.css"/>
<!--<f:loadBundle var="bundle" basename="/Bundle"/> -->
</h:head>
<h:body style="font-size: small; font-family: Ubuntu,verdana;">
<h:form>
<p:panel closable="false" style="float: right;height: 50px;font-size: smaller" >
<h:panelGrid columns="2" style="text-align: center">
<h:outputText value="#{bundle.Language}"/>
<h:selectOneMenu value="#{localeBean.language}" onchange="submit()">
<f:selectItem itemValue="fr" itemLabel="Français" />
<f:selectItem itemValue="en" itemLabel="English" />
<f:selectItem itemValue="fr_FR" itemLabel="France"/>
<f:selectItem itemValue="en_US" itemLabel="US" />
</h:selectOneMenu>
</h:panelGrid>
</p:panel>
The others pages:
<?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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<ui:composition template="/template.xhtml">
<ui:define name="title">
<h:outputText value="#{bundle.EditHistoryTitle}"></h:outputText>
</ui:define>
<ui:define name="body" >
<h:panelGroup id="messagePanel" layout="block">
<h:messages errorStyle="color: red" infoStyle="color: green" layout="table"/>
</h:panelGroup>
<h:form>.....
It is working, but the problem is that the language gets back to its first value once I navigate to another page. Otherwise, When I change the language, it only affects the current page and once I move to another page, the localization bundle gets its default value in faces-config.xml
What I need is to make the language persistent through the whole session.
Does anyone have a clue plz?
You need to wrap your master template in a
<f:view locale="#{LanguageBean.localeCode}">
See also:
Localization in JSF, how to remember selected locale per session instead of per request/view - contains complete bean/view code example
The problem is that when you are setting your Locale in the valueChangeListener you are setting it on the ViewRoot which lives and dies with the current view, and not the session.
You are storing your LocaleCode in the SessionScoped managed bean so you can set the Locale in the ViewRoot for every page and this should fix your problem.

Resources