I'm trying to close a dialog with a cancel button, I receive the event but the dialog does not close.
After a lot of digging I realize the problem is where the dialog has an input field with the ticket description, but it works when I remove it and I leave only de buttons.
Does it make any sense?
Form base code
...
<p:commandButton icon="fa fa-bug" actionListener="#{ticketBean.viewTicket}"/>
...
TicketBean code
#ManagedBean
#ViewScoped
public class TicketBean {
...
public void viewTicket() {
logger.info("--------------ViewTicket");
Map<String, Object> options = new HashMap<>();
options.put("modal", true);
options.put("width", 640);
options.put("height", 250);
options.put("resizable", false);
options.put("contentWidth", "100%");
options.put("contentHeight", "100%");
options.put("closable", false);
RequestContext.getCurrentInstance().openDialog("maintenance/ticket", options, null);
}
public void cancel() {
RequestContext.getCurrentInstance().closeDialog(null);
}
...
ticket.xhtml dialog code DOES NOT WORK
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>#{msgs.ticket_create}</title>
</h:head>
<h:body>
<h:form id="frmTicket">
<p:inputTextarea rows="5" cols="70" value="#{ticketBean.ticket.descripcio}" >
</p:inputTextarea>
<br/>
<h:inputHidden value="#{ticketBean.ticket.path}" />
<p:commandButton value="#{msgs.save}" style="width:auto"
styleClass="GreenButton"
actionListener="#{ticketBean.save}"/>
<p:commandButton value="#{msgs.cancel}" style="width:auto"
styleClass="RedButton"
actionListener="#{ticketBean.cancel}"/>
</h:form>
</h:body>
</html>
ticket.xhtml dialog code DOES WORK
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>#{msgs.ticket_create}</title>
</h:head>
<h:body>
<h:form id="frmTicket">
<p:commandButton value="#{msgs.save}" style="width:auto"
styleClass="GreenButton"
actionListener="#{ticketBean.save}"/>
<p:commandButton value="#{msgs.cancel}" style="width:auto"
styleClass="RedButton"
actionListener="#{ticketBean.cancel}"/>
</h:form>
</h:body>
</html>
faces-config.xml
...
<application>
<action-listener>
org.primefaces.application.DialogActionListener
</action-listener>
<navigation-handler>
org.primefaces.application.DialogNavigationHandler
</navigation-handler>
<view-handler>
org.primefaces.application.DialogViewHandler
</view-handler>
</application>
...
web.xml
...
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
...
The solution is to change the code
<p:inputTextarea rows="5" cols="70" value="#{ticketBean.ticket.descripcio}" >
</p:inputTextarea>
for this one
<h:inputTextarea rows="5" cols="70" value="#{ticketBean.ticket.descripcio}" >
</h:inputTextarea>
The problems seems to be with the coponent p:inputTextarea
*Possible primefaces 6 bug
Related
I have two pages of jsf. dashboard.xhtml has a <p:poll /> tag that updates "form1" every second, while input.xhtml is just a basic crud page that updates display in dashboard.xhtml. I'm wondering because when the dashboard.xhtml is open on a different tab or a second window of browser, input.xhtml keeps losing its state and causes the view to expire ViewExpiredException. When I tried to put <o:enableRestorableView /> in template_2.xhtml, the ViewExpiredException is gone but my input values is resetting when I submit the form. The resetting is not occuring when dashboard.xhtml is not open. When it's open, the input resets randomly.
What I want to accomplish is when I input some data in the input.xhtml, I want it to display in dashboard.xhtml in realtime. Anybody knows how to do this without (ViewExpiredException)? The application runs in WebSphere 8.5(MyFaces 2.0), Primefaces 6.0, OmniFaces 1.8.3
dashboard.xhtml:
<ui:composition template="/WEB-INF/template_1.xhtml"
xmlns:p="http://primefaces.org/ui"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:o="http://omnifaces.org/ui">
<ui:define name="content">
<h:form id="form1">
<p:poll interval="1" listener="#{dashboardBacking.updateValues}" update="form1"
global="false" />
<!-- dashboard content -->
</h:form>
</ui:define>
</ui:composition>
input.xhtml:
<ui:composition template="/WEB-INF/template_2.xhtml"
xmlns:p="http://primefaces.org/ui"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:o="http://omnifaces.org/ui">
<ui:define name="content">
<h:form id="form2">
<p:panelGrid>
<p:row>
<p:column>
<p:inputText value="#{inputBacking.values.description}" />
</p:column>
</p:row>
<p:row>
<p:column>
<p:commandButton id="#{inputBacking.save}" update="form2"/>
</p:column>
</p:row>
</p:panelGrid>
</h:form>
</ui:define>
</ui:composition>
Backing bean (dashboard.xhtml):
#ManagedBean
#ViewScoped
public class DashboardBacking {
#EJB
private Dashboard dashboard;
private DashboardValues values;
#PostConstruct
public void postConstruct(){
values = new DashboardValues();
updateValues();
}
public void updateValues(){
DashboardValues newValues = dashboard.getValue();
if(!newValues.exactlyEqual(values)){
values = newValues;
}
}
public DashboardValues getValues(){
return values;
}
}
Backing bean (input.xhtml):
#ManagedBean
#ViewScoped
public class InputBacking {
#EJB
private DashboardDao dao;
private DashboardValues values;
public void save(){
dao.save(values)
}
//Getters and Setters
}
Dashboard singleton class:
#Singleton
#Startup
public class Dashboard {
#EJB
private DashboardDao dao;
private DashboardValue value;
#Schedule(second="*/5") //update dashboard value every 5 seconds
private void updater(){
value = dao.getLatest();
}
public DashvoardValue getValue(){
return value;
}
}
template_1.xhtml:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui" xmlns:o="http://omnifaces.org/ui"
xmlns:of="http://omnifaces.org/functions">
<f:view contentType="text/html">
<f:metadata>
<o:enableRestorableView />
</f:metadata>
<h:head>
</h:head>
<h:body>
<ui:insert name="content"/>
</h:body>
</f:view>
</html>
template_2.xhtml:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui" xmlns:o="http://omnifaces.org/ui"
xmlns:of="http://omnifaces.org/functions">
<f:view contentType="text/html">
<h:head>
</h:head>
<h:body>
<ui:insert name="content"/>
</h:body>
</f:view>
</html>
This question already has answers here:
commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated
(12 answers)
Closed 5 years ago.
Am a newbie to JSF. Am using JSF 2 and primefaces 4.0 in my application. As stated in the Title, the input value given in the xhtml page, does not set the value to the ManagedBean. I have tried all the possible combination.
growlMessage.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
<h:form>
<p:growl id="growl" showDetail="true" sticky="true" />
<p:panel id="panelID" header="Growl">
<h:panelGrid columns="2" cellpadding="5">
<h:outputLabel for="msg" value="Message:" />
<p:inputText id="msg" value="#{growlView.message}" required="true" />
</h:panelGrid>
<p:commandButton value="Save" actionListener="#{growlView.saveMessage}"/>
</p:panel>
</h:form>
</h:body>
</html>
`
GrowlView.java:
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
#ManagedBean
#ViewScoped
public class GrowlView implements Serializable{
private String message;
public GrowlView() {
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public void saveMessage(){
System.out.println("##### hello");
System.out.println("#####"+ getMessage());
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage("Successful", "Your message: "+message));
context.addMessage(null, new FacesMessage("Second message", "Additional Message details"));
}
}
Do you have a good reason to use JSF 2.0 instead of 2.2? You should use CDI instead of JSF managed beans, which is more or less deprecated. So, use
#Named
#ViewScoped
public class GrowlView implements Serializable
Be sure the ViewScoped annotation is from javax.faces.view. And the beginning of the xhtml should use the new namespace:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui">
Your commandButton should look like this (according to PrimeFaces showcase):
<p:commandButton value="Save" actionListener="#{growlView.saveMessage}" update="growl"/>
Have you tried setting a larger scope to your managed bean, for example #SessionScoped ?
(just for testing purposes). So you could exclude a possible scope problem.
try this following code: using the process and partialSubmit attributes:
<p:commandButton value="Save" actionListener="#{growlView.saveMessage}" update="growl" process="#form" partialSubmit="true"/>
hy,
change for
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
<h:form>
<p:growl id="growl" showDetail="true" sticky="true" autoUpdate="true"/>
<p:panel id="panelID" header="Growl">
<h:panelGrid columns="2" cellpadding="5">
<h:outputLabel for="msg" value="Message:" />
<p:inputText id="msg" value="#{pageView.message}" required="true" />
</h:panelGrid>
<p:commandButton value="Save" action="#{pageView.saveMessage}" update="growl"/>
</p:panel>
</h:form>
</h:body>
</html>
and replace
#ManagedBean
with
#ManagedBean(name="pageView")
I have an application that has a layout with left and center layout units (demoLayout.xhtml). On main page (main.xhtml) i have p:tree on left layout unit (demoTree.xhtml) and three different forms on center layout unit (first.xhtml, second.xhtml, third.xhtml). Center forms switches using tree node clicks. My default center form is first.xhtml and when i do not put p:commandButton on first.xhtml command buttons on second.xhtml and on third.xhtml actions has not being called. When i put p:commandButton on first.xhtml the other command buttons works, but i do not want to put p:commandButton on first.xhtml. How can i do?
demoLayout.xhtml
<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">
<f:view id="mainPanel" encoding="UTF-8" contentType="text/html">
<h:head>
<f:facet name="first">
<meta http-equiv="X-UA-Compatible" content="EmulateIE8" />
<meta content='text/html; charset=UTF-8' http-equiv="Content-Type" />
<title>#{title}</title>
</f:facet>
</h:head>
<h:body>
<p:layout fullPage="true">
<p:layoutUnit id="left" position="west" size="300" resizable="true" closable="true" collapsible="true" header="Quick Links" visible="true" minSize="200">
<div id="west">
<ui:insert name="west">
Default West Content
</ui:insert>
</div>
</p:layoutUnit>
<p:layoutUnit id="center" position="center">
<div id="centerDiv">
<ui:insert name="center">
Default Center Content
</ui:insert>
</div>
</p:layoutUnit>
</p:layout>
</h:body>
</f:view>
</html>
main.xhtml
<ui:composition template="demoLayout.xhtml"
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">
<ui:param name="title" value="demo" />
<ui:define name="west">
<ui:include src="demoTree.xhtml" />
</ui:define>
<ui:define name="center">
<ui:include src="#{demo3MBean.activePanel}" />
</ui:define>
</ui:composition>
demoTree.xhtml
<ui:composition 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">
<h:form id="treeForm">
<p:growl id="messages" showDetail="true" sticky="false" />
<p:tree value="#{demoTreeBean.root}" var="node" id="tree" animate="true" style="width:350px;height:720px" dynamic="true" cache="false"
selectionMode="single">
<p:treeNode type="First">
<h:outputText value="#{node}" id="lblNode1" />
</p:treeNode>
<p:treeNode type="Second">
<h:outputText value="#{node}" id="lblNode2" />
</p:treeNode>
<p:treeNode type="Third">
<h:outputText value="#{node}" id="lblNode3" />
</p:treeNode>
<p:ajax event="select" update=":rightForm" listener="#{demo3MBean.onNodeSelect}" />
</p:tree>
<p:blockUI block=":center" trigger="tree">
LOADING<br />
<p:graphicImage value="/images/ajax-loader.gif" />
</p:blockUI>
</h:form>
</ui:composition>
first.xhtml
<ui:composition 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">
<h:form id="rightForm">
<p:growl id="messages" showDetail="true" sticky="false" />
<br></br>
<p:fieldset id="resourceList" legend="1 nolu grup">
<h:outputText value="1 Nolu XHTML" />
<br />
<ui:remove>
<p:commandButton id="buton" value="Print Me 1" actionListener="#{demo3MBean.printMe1}" />
</ui:remove>
</p:fieldset>
</h:form>
</ui:composition>
second.xhtml
<ui:composition 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">
<h:form id="rightForm">
<p:growl id="messages" showDetail="true" sticky="false" />
<br></br>
<p:fieldset id="resourceList" legend="2 nolu grup">
<h:outputText value="2 Nolu XHTML" />
<br />
<p:commandButton id="buton" value="Print Me 2" actionListener="#{demo3MBean.printMe2}" />
</p:fieldset>
</h:form>
</ui:composition>
third.xhtml
<ui:composition 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">
<h:form id="rightForm">
<p:growl id="messages" showDetail="true" sticky="false" />
<br></br>
<p:fieldset id="resourceList" legend="3 nolu grup">
<h:outputText value="3 Nolu XHTML" />
<br />
<p:commandButton id="buton" value="Print Me 3" actionListener="#{demo3MBean.printMe3}" />
</p:fieldset>
</h:form>
</ui:composition>
Demo3MBean.java
#ManagedBean(name = "demo3MBean")
#ViewScoped
public class Demo3MBean extends TlosSWBaseBean implements Serializable {
private static final long serialVersionUID = -504537811128309503L;
private String activePanel = FIRST_PANEL;
public final static String FIRST_PANEL = "first.xhtml";
public final static String SECOND_PANEL = "second.xhtml";
public final static String THIRD_PANEL = "third.xhtml";
public void onNodeSelect(NodeSelectEvent event) {
String nodeType = event.getTreeNode().getType();
if (nodeType.equals("First")) {
activePanel = FIRST_PANEL;
} else if (nodeType.equals("Second")) {
activePanel = SECOND_PANEL;
} else if (nodeType.equals("Third")) {
activePanel = THIRD_PANEL;
}
}
public void printMe1(ActionEvent e) {
System.out.println("Me 1");
}
public void printMe2(ActionEvent e) {
System.out.println("Me 2");
}
public void printMe3(ActionEvent e) {
System.out.println("Me 3");
}
public String getActivePanel() {
return activePanel;
}
public void setActivePanel(String activePanel) {
this.activePanel = activePanel;
}
}
DemoTreeBean.java
#ManagedBean(name = "demoTreeBean")
public class DemoTreeBean {
private TreeNode root;
#SuppressWarnings("unused")
public DemoTreeBean() {
root = new DefaultTreeNode("Root", null);
TreeNode node0 = new DefaultTreeNode("First", "First Node", root);
TreeNode node1 = new DefaultTreeNode("Second", "Second Node", root);
TreeNode node2 = new DefaultTreeNode("Third", "Third Node", root);
}
public TreeNode getRoot() {
return root;
}
}
I also read these :
JSF : dynamically loaded page commandButton not working
Primefaces commandButton action attribute not being called
BalusC answer - h:commandLink / h:commandButton is not being invoked
This part from main.xhtml is the cause of your problem.
<ui:define name="center">
<ui:include src="#{demo3MBean.activePanel}" />
</ui:define>
changing value of src at runtime does not work.
Try it with a static value like
<ui:define name="center">
<ui:include src="third.xhtml" />
</ui:define>
and you will see that buttons work.
Changing value of src at runtime will not work.
<ui:define name="center">
<ui:include src="#{demo3MBean.activePanel}" />
</ui:define>
Alternative solution to conquer your problem is using a rendered flag for your xhtml pages.For example
<ui:define name="center">
<s:div rendered="#{demo3MBean.firstFlag}">
<ui:include src="first.xhtml" />
</s:div>
<s:div rendered="#{demo3MBean.secondFlag}">
<ui:include src="second.xhtml" />
</s:div>
<s:div rendered="#{demo3MBean.thirdFlag}">
<ui:include src="third.xhtml.xhtml" />
</s:div>
</ui:define>
Set all pages flag to FALSE intially. Make the specific flag value as TRUE only when the relevant tree node is clicked. Do not forget to set unique "id" of all components of the included pages.
Using the above solution, attributes like action and actionListener of components such as commandButtons, commandLinks etc will also work normally and perfectly.
Hope this will solve your problem.
And also do not forget to accept my answer if it helps. Have a nice Day!!
It is just impossible to include a different file by updating the form inside of the included file. As you have the form rightForm in each of your files, you are only updating the rightForm of the currently loaded file when a tree node is selected. So there will never be a different file loaded.
Besides it is a bad approach to define the same form in each file when it exists in everyone (similiar for the growl).
To fix this remove the h:form in first.xhtml, second.xhtml and third.xhtml and change the second ui:define in your main.xhtml to the following:
<ui:define name="center">
<h:form id="rightForm">
<ui:include src="#{demo3MBean.activePanel}" />
</h:form>
</ui:define>
And why are you defining three p:treeNodes in the p:tree? As you are initializing the tree in the DemoTreeBean there is no need to define the treenodes manually. The p:tree is just iterating through the nodes of the given value, as you can see in the showcase. Try the following code to reduce the effort when adding more tree nodes:
<p:tree value="#{demoTreeBean.root}" var="node" id="tree" animate="true"
style="width:350px;height:720px" dynamic="true" cache="false"
selectionMode="single">
<p:treeNode type="#{node.data}">
<h:outputText value="#{node}" id="lblNode1" />
</p:treeNode>
<p:ajax event="select" update=":rightForm" listener="#{demo3MBean.onNodeSelect}" />
</p:tree>
i just don't get it...
Why p:commandLink not working? The page is refreshing but with the same amount of data in table. I'm supposing that controller is okay. Take a look.\
View:
<?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"
xmlns:p="http://primefaces.org/ui">
<body>
<ui:composition template="./Template.xhtml">
<ui:define name="content" >
<f:view>
<h:form style="padding: 5px">
<p:dataTable id="dataTable2" var="item" value="#{warningsController.warns}">
<p:column rendered="#{loginController.admin}">
<f:facet name="header">
<h:outputText value="Administracja" />
</f:facet>
<h:form>
<p:commandLink id="Remove" value="Remove" action="#{warningsController.remove(item.id)}" ajax="false" />
</h:form>
</p:column>
</p:dataTable>
</h:form>
</f:view>
</ui:define>
</ui:composition>
</body>
</html>
and controller:
public String remove(long a){
//System.out.println(a);
pf.remove(pf.find(a));
return "Listsev.xhtml";
}
You have multiple h:forms cascaded/nested, that's invalid html. Unknown/wanted side effects can/may occur, maybe like you are experiencing right now. Get right if that inner h:form and try again.
In your remove method, pf is the list which you return by calling #{warningsController.warns}?
I have a primefaces (version 3.4.2) slider and an inputText for output value of the slider value.
The problem is that a change of the slider update the displayed value of the inputText, but the setter bind to the inputText is not called.
Here is my slider:
<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:a4j="http://richfaces.org/a4j">
<h:head>
<title>Zinsrechner</title>
</h:head>
<h:body>
<h:form>
<h:panelGrid columns="1" style="margin-bottom:10px">
<p:inputText id="x" value="#{zinsrechner.monatlicherBeitrag}" />
<p:slider minValue="0" maxValue="150" for="x" />
</h:panelGrid>
</h:form>
</h:body>
</html>
And this is my Setter, which is NOT called:
public void setMonatlicherBeitrag( Double beitrag ) {
monatlicherBeitrag = beitrag;
}
The Getter IS called:
public Double getMonatlicherBeitrag() {
return GuiParameter.getSpareinlageProMonat();
}
Adding a <p:ajax> inside your Slider will to the trick.
Example:
<p:slider minValue="0" maxValue="150" for="x">
<p:ajax event="slideEnd" process="x" />
</p:slider>
I was facing the same problem, but in my case the thing was that I was setting the <p:inputNumber .../> label as read only. I removed that attribute and everything worked.