render a primefaces message if url params come - jsf-2

I have the next url: http://host:port/page.xhtml?response=something
I want render a p:message if response==something
I have the next code:
<h:panelGroup rendered="#{param.response == 'something'}">
<h:outputText value="message" />
</h:panelGroup>
this render a <div>message</div>
I want render a <p:messages /> or <p:growl /> and its value "message" if response==something
please help me to code this, and something else:
Do I need a bean managed to send value to p:messages or p:growl?
Do I need to call p:messages or p:growl from a <p:commandButton />?
I can't use <h:body onload="callFunction()"> 'cause h:body is on
template xhtml <ui:insert name="content"></ui:insert> and my view
is on <ui:composition ... <ui:define name="content" > in other
xhtml, and h:body is common to several views

You'd really need to use FacesContext#addMessage() to add a message to the context. On an initial request, your best bet is a
<f:metadata>
<f:viewParam name="response" value="#{bean.response}"/>
<f:event type="preRenderView" listener="#{bean.init}" />
</f:metadata>
with
private String response;
public void init() {
if (response != null) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
FacesMessage.SEVERITY_INFO, response, null));
}
}
You can by the way also use a separate <p:messages globalOnly="true"> or <p:growl globalOnly="true"> to display only faces messages with a null client ID.
See also:
What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?

Related

Updating data operation in JSF

I'm much newbie to the JSF. I am creating a simple crud app. I have done adding, and deleting but there is some problem with updating data..... when edit is clicked, bean is populated with values of whom i want to update. but it is not shown in the next page, in which it is to be edited......
this is the link which leads to editPage
<h:column>
<f:facet name="header">Edit</f:facet>
<h:form>
<h:commandLink value="Edit" action="#{personManipulate.editPerson(person)}"/>
</h:form>
</h:column>
this is the code which assign person data to person entity
public String editPerson(PersonEntity person){
this.person=person;
return "success2";
}
This is the code which updates per save person
public String savePerson(){
if(person.getPersonId() > 0){
personDao.updatePerson(person);
return "success";
}else{
personDao.addPerson(person);
return "success";
}
}
}
This is the page where values should be shown and Updated
<h:form>
<h:panelGrid columns="2">
<f:facet name="header">Person</f:facet>
<h:outputLabel for="firstName" value="First name" />
<h:inputText id="firstName" value="#{personManipulate.person.firstName}"
label="First name" />
<h:outputLabel for="lastName" value="Last name" />
<h:inputText id="lastName" value="#{personManipulate.person.lastName}"
label="Last name" />
<h:outputLabel for="address" value="Address" />
<h:inputText id="address" value="#{personManipulate.person.address}"
label="Address" />
<h:outputLabel for="phone" value="Contact Number" />
<h:inputText id="phone" value="#{personManipulate.person.phone}" />
<h:commandButton action="#{personManipulate.savePerson}" value="Submit" />
<h:button outcome="ShowPersons.xhtml" value="Cancel" />
</h:panelGrid>
</h:form>
This is the navigation rule
<navigation-rule>
<from-view-id>JSF/personManipulate.xhtml</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>JSF/ShowPersons.xhtml</to-view-id>
<redirect />
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>JSF/ShowPersons.xhtml</from-view-id>
<navigation-case>
<from-outcome>success2</from-outcome>
<to-view-id>JSF/personManipulate.xhtml</to-view-id>
<redirect />
</navigation-case>
</navigation-rule>
On Debugging everything is fine i.e. data is assigned to person object but not shown on update page
Well, you're trying to access the #{personManipulate} bean, which still has not been created when you're in the people list. Supposing it is a #ViewScoped bean as it should, that should be created when you address to JSF/personManipulate.xhtml specifically. So I would encourage you to do the People List -> Edit Person transition using a GET request, instead:
showPersons.xhtml
<h:column>
<f:facet name="header">Edit</f:facet>
<h:form>
<h:link value="Edit" outcome="personManipulate.xhtml">
<f:param name="idPerson" value="#{person.id}" />
</h:link>
</h:form>
</h:column>
That will direct you to an url similar as: personManipulate.xhtml?idPerson=1. Then, load the person before the #{personManipulate} bean makes the rendering task:
personManipulate.xhtml
<f:metadata>
<f:viewParam name="idPerson" value="#{personManipulate.idPerson}" />
<f:event listener="#{personManipulate.loadData}" type="preRenderView" />
</f:metadata>
PersonManipulate.java
public void loadData(ComponentSystemEvent event){
person = service.getPersonById(idPerson);
}
That way you keep both view beans unbound and you use urls for navigation. If you aren't interested in showing the person id in the url, then you can use the Flash scope to pass it from bean to bean. Have a look at the linked posts.
See also:
View parameter when navigating to another page
Understand Flash Scope in JSF2

Updating composite overlayPanel

Based on this subject, I would like to update a <p:overlayPanel> from a datatable to a composite component
View
<h:form id="myForm">
<p:dataTable id="myTable" ...>
<p:column headerText="1">
<p:commandLink id="link"
actionListener="#{bean.update}"
update=":toto" value="Val" />
<p:overlayPanel id="panel" for="link">
<my:view id="toto">
</p:overlayPanel>
</p:column>
</p:dataTable>
</h:form>
Composite
<composite:implementation>
<span id="#{cc.clientId}">
<p:dataTable id="toto" ...>
</p:dataTable>
</span>
</composite:implementation>
However I always get an error message saying that :toto component id can not be reached.
So, how can I access the dataTable placed in the composite ?
EDIT : Actually I changed my mind concerning the utilisation of p:overlayPanel for a p:datatable because of the creation of dynamic ids. I replaced this by a p:dialog and it works great.

JSF2 Extract URL Parameter

I'm using primefaces menubar and, depending on the selected menuitem I would like to pass a diferent parameter in the URL (GET)
<ui:composition>
<h:form rendered="#{facesContext.externalContext.userPrincipal != null}">
<p:menubar>
<p:menuitem value="#{msg['menu.home']}" url="/index.xhtml" />
<p:submenu label="#{msg['menu.maintenance']}" >
<p:menuitem id="client" value="#{msg['menu.clients']}" action="/person/newPerson?faces-redirect=true" ajax="false" includeViewParams="true">
**<f:param name="type" value="client"/>**
</p:menuitem>
<p:menuitem id="supplier" value="#{msg['menu.suppliers']}" action="/person/newPerson?faces-redirect=true" ajax="false" >
**<f:param name="type" value="supplier"/>**
</p:menuitem>
</p:submenu>
This way when the menuitem "client" is selected I would like to use the GET parameter "type=client" in the newPerson.xhtml like so:
<h:outputText value="Parameter: #{param['type']}" />
However, no parameter is being passed in the URL. Can someone tell me what's wrong in my code?
Instead of action attribute try URL
<p:menuitem id="client" value="#{msg['menu.clients']}" url="/person/newPerson?faces-redirect=true&type=client" ajax="false" includeViewParams="true">
</p:menuitem>

display growl when page is loaded primefaces jsf2

I have this commandButton :
<p:commandButton value="Enregistrer" action="#{reglementClientMB.ajouter}"
process="#this #form" update="#form" >
<f:setPropertyActionListener target="#{reglementClientMB.est_ajouter}" value="false" />
</p:commandButton>
the action method return to another page , I want to display one p:growl when the next page is loaded
I tested to put that is the constructor of its managed bean but the growl is displayed below data in the page
FacesContext.getCurrentInstance().addMessage(
null,
new FacesMessage(FacesMessage.SEVERITY_INFO, ""
+ "Confirmation", "Réglement crée avec sucée"));
RequestContext.getCurrentInstance().update("messages");
how can I achieve this
thank you in advance
The bean's constructor may be too late for the job if the <p:growl> is rendered before the bean is been constructed for the first time. E.g.
<p:growl />
...
<h:outputText value="#{bean.something}" />
It would only work if the bean is constructed before the <p:growl> is rendered.
<h:outputText value="#{bean.something}" />
...
<p:growl />
In order to solve your concrete problem, you'd need to do the job in a pre render view listener instead.
<f:event type="preRenderView" listener="#{bean.init}" />
With:
public void init() {
// Add the desired message here.
}

h:selectManyCheckbox with POJO problem

I tried some ways to achieve this simple thing :
Show a collection of POJOs in form of checkboxes
When i click on one of the checkboxes, a method should be executed
The method will be able to access the pojo/checkbox that was clicked
I tried to implement it this way :
<h:selectManyCheckbox id="groupUsers" layout="pageDirection" value="#{timetableBean.selectedUsers}">
<f:ajax listener="#{timetableBean.processUserEvents}" render="#this" />
<f:selectItems value="#{timetableBean.group.users}"
var="user" itemLabel="#{user.userId} - #{user.name}" itemValue="#{user}">
<f:attribute name="user" value="#{user}" />
</f:selectItems>
</h:selectManyCheckbox>
And my method is :
public void processUserEvents(AjaxBehaviorEvent e) {
User user = (User) e.getComponent().getAttributes().get("user");
...
}
The user fetched from e.getComponent().getAttributes().get("user"); is sadly null
In what way can i accomplish this ?
Thank you !

Resources