Click on Growl with link - jsf

I'm using jsf and Primefaces and I need some help.
I built in my project a IdleMonitor. It will trigger after the timeout a growl and I want to build in a clickable link. But my problem is that I can't add html code into the bean and didn't find anything about actions after clicking on the growl.
public void onActive() {
String link = "http://abcd.efg.com";
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "Deine Session ist abgelaufen", link));
}
xhtml:
<h:form>
<prime:growl id="messages" showDetail="true" sticky="true" redisplay="false" escape="false"/>
<prime:idleMonitor timeout="5000">
<prime:ajax event="idle" listener="#{idleMonitorView.onActive}" update="messages" />
<prime:ajax event="active" listener="#{idleMonitorView.onActive}" update="messages"/>
</prime:idleMonitor>
</h:form>

Related

p:steps example not functioning

I'm trying use the STEPS component - Primefaces. But in the documentation the tutorial is very poor.
Can someone write an example using steps with property rendered or something like that, how Can I show and hide a panel using STEPS component.
I tried like this but does not work
My xhtml
<p:steps id="testSteps">
<p:menuitem value="Personal" update="testSteps" actionListener="#{BeanTest.shown2()}"/>
<p:menuitem value="Seat Selection" update="testSteps"/>
</p:steps>
<form id="formShowField1" >
<p:panel rendered="#{BeanTest.showfield1}">
<p:outputLabel value="FORM 1"/>
</p:panel>
</form>
<form id="formShowField2">
<p:panel rendered="#{BeanTest.showfield2}">
<p:outputLabel value="FORM 2" />
</p:panel>
</form>
My bean
public void shown1(){
showfield1 = true;
updateEntirePage("formShowField1");
}
public void shown2(){
showfield1 = false;
updateEntirePage("formShowField1");
showfield2 = true;
updateEntirePage("formShowField2");
}
As stated by comments, there are multiple issues with your XHTML code.
1) Use <h:form> instead of <form>
2) The p:steps component is readonly by default. Set readonly="false" in order to have interactive menu items. In this mode, it needs to be placed somwhere inside a h:form to - I get a javax.faces.FacesException: MenuItem must be inside a form element else.
3) Your menuItems update the p:steps component only. Your other panels won't ever show up this way as they are not updated. You should update an element containing them too. Don't know what updateEntirePage is though, especially when invoked twice.
4) Bean names like Java variables typically start with lower case character.
Try it like this:
<h:form>
<p:steps id="testSteps" readonly="false">
<p:menuitem value="Personal" update="#form" actionListener="#{beanTest.shown2()}"/>
<p:menuitem value="Seat Selection" update="#form"/>
</p:steps>
<p:panel rendered="#{neanTest.showfield1}">
<p:outputLabel value="FORM 1"/>
</p:panel>
<p:panel rendered="#{beanTest.showfield2}">
<p:outputLabel value="FORM 2" />
</p:panel>
</h:form>
And in your bean:
public void shown2(){
showfield1 = false;
showfield2 = true;
}

Empty page after h:commandButton submit

I have an JSF 2.2 page which contain forms and a <h:commandButton> to call the method, after the submit button, the method called successfully and all instructions works fine
Problem:
After the button submit, I got an empty page and the URL in the browser is still unchanged
What I want is that after the submit, the page stays the same and shows a growl message.
managedBean.java
#ManagedBean(name="consignmentShipBean")
#ViewScoped
public class ConsignmentShipBean implements Serializable{
public void send(){
//some instructions to do
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage("Successful", "__") );
}
}
page.xhtml
<p:growl id="growl" globalOnly="true" autoUpdate="true"/>
<h:form>
...
<h:commandButton value="Send" actionListener="#{consignmentShipBean.send}" />
</h:form>
UPDATE
the result after the button submit:
You have to perform an Ajax request, not an usual request. Use a standard button or a PrimeFaces one for it:
<h:commandButton value="Send" actionListener="#{consignmentShipBean.send}" >
<f:ajax update="growl"/>
</h:commandButton>
Or
<p:commandButton value="Send"
actionListener="#{consignmentShipBean.send}" update="growl" />

How to show messages in p:dialog?

I need your help in showing an error message in the dialog. By clicking on the commandButton, no message is shown in the dialog.
Even though I tried to show the message in a dialog, but nothing is shown without any error.
So how can I produce messages in a dialog and not in the main form
Here is the JSF page code:
<h:form id="Requests">
<p:messages id="messages" showDetail="true" autoUpdate="true" closable="true"/>
<p:dialog id="c1" header="C1" widgetVar="c1">
<p:message id="messagePDFSTAR"
for=":Requests:DownloadPDFSTAR"
showDetail="true" />
<p:commandButton id="DownloadPDFSTAR"
value="Download"
ajax="false"
actionListener="#{hrd.PDFSTAR}"
update=":Requests:messagePDFSTAR" >
<p:fileDownload value="#{hrd.fileSTAR}" />
</p:commandButton>
</p:dialog>
</h:form>
Here is the java bean code:
public void PDFSTAR() {
try {
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Ref is Null", "Ref is Null");
RequestContext.getCurrentInstance().showMessageInDialog(message);
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "Fatal!", "System Error"));
} catch (Exception e) {
e.printStackTrace();
}
}
It's because you have ajax=false at the commandButton, so, the attribute update::Requests:messagePDFSTAR isn't going to work.
You can't combine ajax=false and update="...", update attribute is only for ajax actions.
I understand you need the ajax=false because the p:fileDownload works with that, but maybe you can try another way to display the message you want.
I deal with that situation once and use a workaround, in p:dialog you can use a p:messages and autoUpdate=true.
<h:form id="Requests">
<p:messages id="messages" showDetail="true" autoUpdate="true" closable="true"/>
<p:dialog id="c1" header="C1" widgetVar="c1">
<p:messages id="messagesPDFSTAR"
autoUpdate="true"
showDetail="true" />
<p:commandButton id="DownloadPDFSTAR"
value="Download"
ajax="false"
actionListener="#{hrd.PDFSTAR}" >
<p:fileDownload value="#{hrd.fileSTAR}" />
</p:commandButton>
</p:dialog>
</h:form>
I hope that will help you.
Edit:
RequestContext.getCurrentInstance().showMessageInDialog(message);
It shows the message in a new dialog, but i think it's not what you want. You want to show the message in the same dialog of the commandButton and the fileDownLoad.
Ajax and update not working correctly
To update components as p:growl or p:messages after a download, or even at the start just use a p:remoteCommand and the PrimeFaces.monitorDownload(start, stop); as in the example provided here PrimeFaces File Download example
here is how I'd do it:
<script type="text/javascript">
function start() {
PF('statusDialog').show();
//maybe some other blah blah..
updateMyMessages();
}
function stop() {
PF('statusDialog').hide();
//maybe some other blah blah..
updateMyMessages();
}
</script>
<div class="card">
<p:dialog modal="true" widgetVar="statusDialog" header="Status" draggable="false" closable="false"
resizable="false">
<i class="pi pi-spinner pi-spin" style="font-size:3rem"></i>
</p:dialog>
<h:form>
<p:messages id="messagesPDFSTAR"/>
<p:remoteCommand name="updateMyMessages" update="messagesPDFSTAR" action="#{hrd.PDFSTAR}"/>
<p:commandButton value="Download" ajax="false" onclick="PrimeFaces.monitorDownload(start, stop);"
icon="pi pi-arrow-down" styleClass="p-mr-2">
<p:fileDownload value="#{fileDownloadView.file}"/>
</p:commandButton>
</div>
Please note that I called the remote command twice so you get two updates to your messages, also this will execute your action #{hrd.PDFSTAR} twice, once on download start and the other on download finish, so modify as you see fit.
You can always upgrade to PrimeFaces 10+ and use ajax="true" (the default) on the download command button.

Primefaces growl message from dialog external file

I have jsf page with TabView... Together 3 tabs. Tabs calls dialogs with some forms for creating elements. Dialogs are placed in external .xhtml page.
First tab is on "main" page (code placed below) and growl message called from validation bean shows well on the page. Second tab is calling growl message but from button placed on form from external file with dialogs... No errors occurs but the growl message didn't show at all.
My "main" view:
<ui:composition>
<p:growl id="growl" showDetail="true" life="4000" />
<h:form id="systemSettingsForm">
<p:tabView id="ticketSettingsTab" rendered="true" widgetVar="tabViewVar">
<p:tab id="tab0">
...
</p:tab>
<p:tab id="tab1">
<ui:include src="/WEB-INF/tags/dialogs.xhtml" />
<p:commandButton value="Add" oncomplete="PF('addDialog').show()" update=":systemSettingsForm:ticketSettingsTab:addingDlg" />
...
</p:tab>
</p:tabView>
</h:form>
</ui:composition>
</h:body>
Dialogs view looks like (nothing special as I think...):
<p:dialog id="addPositionDlg" header="Add position" widgetVar="addDialog">
<h:panelGrid id="addingDlg" columns="3" >
<p:outputLabel value="Position name :"/>
<p:selectOneMenu id="selectType" value="#{positionsTableView.positionType}" >
<f:selectItem itemLabel="Select one option" itemValue="" />
<f:selectItems value="#{positionsTableView.types}" />
</p:selectOneMenu>
<p:outputLabel style="width:1px;"/>
<p:outputLabel value="Prefix :" />
<p:inputText id="addPrefix" value="#{positionsTableView.newPosition.prefix}" />
<pe:tooltip for="addPrefix" showEvent="focus" hideEvent="blur">...</pe:tooltip>
//few simillar inputs, not even validated
<center>
<p:commandButton value="Add" actionListener="#{positionsTableView.addByPrefix}" ajax="true"
update=":systemSettingsForm:ticketSettingsTab:mappingTable" oncomplete="PF('addDialog').hide();"/>
</center>
</h:panelGrid>
</p:dialog>
Growl message is added in actionListener invoked by command button in dialog form (#{positionsTableView.addByPrefix}) and this bean is in the same package as previous one (this working) and message is added in the same manner, which is:
public static void addMessage(String title, String message) {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage(title, message));
}
Both beans calling messages are session-scoped.
I have searched here and tried to google solutions but none was successful...
Please help.
MY SOLUTION:
At command button which invokes the message to be displayed on growl element I have updated whole tabView menu and it works.Maybe it's not the best solution, but updating growl id causes an error (it was not visible from dialog file).

How to keep FacesMessage displayed

I display messages in my application
However, the messages are shown brifely and I would like to keep them on screen until they are closed manually.
How can i achieve this ?
EDITED:
I invoke the run method where messages are made.
I invoke the component pbAjax which then updates the growl element
I use
<p:growl id="growl" />
<p:commandButton value="Run" actionListener="#{myBean.run}" id="btnSubmitCreation" onclick="PF('pbAjax').start();"
<p:progressBar widgetVar="pbAjax" ajax="true" value="#{progressBean.progress}" styleClass="animated" >
<p:ajax event="complete" listener="#{controleBean.onComplete}" update="growl" oncomplete="startButton2.enable()"/>
</p:progressBar>
Java code:
public void run() {
...
...
List<String> messages = expResult.getMessages();
for (String message:messages){
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,message,message));
}
}
Just add sticky="true" in growl tag.

Resources