action / ActionListener within p:commandButton / p:commandLink not fired - xhtml

I've been read this article : commandButton/commandLink/ajax action/listener method not invoked or input value not updated
but still i cant understand why my problems weren't solved.
i'm afraid that my understanding for the english explanation were not fully correctly. so i'll lay down my code snippets here.
fyi, i'm still got medium level of understanding english. my english capability is just so-so.
i'm using Primefaces 3.0.RC1, EL library 2.2, JSF 2.1, JSTL 1.1, GlassFish 3.x, coded with Netbeans 7.0.1
now here we come the codes:
template.xhtml
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<h:outputStylesheet name="default.css" library="css"/>
<h:outputStylesheet name="cssLayout.css" library="css"/>
<h:outputStylesheet name="style.css" library="css"/>
<title>MFI Core Banking System</title>
</h:head>
<h:body>
<f:view>
<div id="top">
<ui:include src="header.xhtml"/>
</div>
<div id="menuBar">
<ui:include src="menubar.xhtml"/>
</div>
<div id="content" class="center_content">
<ui:insert name="content">Content</ui:insert>
</div>
<div id="bottom">
<ui:include src="footer.xhtml"/>
</div>
</f:view>
</h:body>
header.xhtml and footer.xhtml just had UI:Composition declaration and <div/> and <a href/> tag only.
menubar.xhtml as below :
<ui:composition 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">
<f:view>
<h:form>
<p:menubar >
<p:menuitem value="Home" action="/index.xhtml?faces-redirect=true" icon="ui-icon ui-icon-home"/>
<p:submenu label="CIF" icon="ui-icon ui-icon-bookmark">
<p:menuitem value="Manajemen CIF" action="/modul/cif/cifManagement?faces-redirect=true"/>
<p:submenu label="CIF Grouping ">
<p:menuitem value="Search" action="/modul/cif/SearchGroupNew.xhtml?faces-redirect=true"/>
<p:menuitem value="Create" action="/modul/cif/CreateGroupNew.xhtml?faces-redirect=true"/>
</p:submenu>
<p:menuitem value="Analyze" url="#"/>
<p:menuitem value="Directorate " action="/modul/userManagement/Struktural.Directorate.xhtml?faces-redirect=true"/>
</p:submenu>
</p:menubar>
</h:form>
</f:view>
</ui:composition>
this is my example page (Directorate) :
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
template="./../../template.xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<ui:define name="top">
Directorate
</ui:define>
<ui:define name="menuBar">
menuBar
</ui:define>
<ui:define name="content">
<f:view contentType="text/html">
<h:form prependId="false">
<p:panel toggleable="true" closable="true" header="Direktorat">
<p:fieldset legend="Tambah Direktorat"
toggleable="TRUE"
collapsed="#{directorateBean.fieldsetCollapse}"
toggleSpeed="500"
>
<h:panelGrid columns="3">
<h:outputLabel for="directorateCode" value="Kode Direktorat*"/>
<p:inputText id="directorateCode" value="#{directorateBean.directorate.directoratecode}" required="true" label="Kode Direktorat" maxlength="4"/>
<p:message for="directorateCode"/>
<h:outputLabel for="directorateName" value="Nama Direktorat*"/>
<p:inputText id="directorateName" value="#{directorateBean.directorate.directoratename}" required="true" label="Nama Direktorat" maxlength="40"/>
<p:message for="directorateName"/>
<h:outputLabel for="directorateStatus" value="Status Direktorat"/>
<p:selectOneRadio id="directorateStatus" value="#{directorateBean.directorate.directoratestatus}">
<f:selectItem itemValue="true" itemLabel="Aktif"/>
<f:selectItem itemValue="false" itemLabel="Tidak Aktif"/>
</p:selectOneRadio>
<p:spacer/>
<p:commandButton id="btnSubmitDirectorate" value="Submit" update="#form" action="#{directorateBean.add()}"/>
<p:commandButton value="Reset" type="reset"/>
</h:panelGrid>
</p:fieldset>
<p:spacer/><p:spacer/>
<p:dataTable
id="tableDirectorate"
emptyMessage="Tidak Ada Direktorat"
value="#{directorateBean.directorateList}"
var="dir"
paginator="true"
paginatorPosition="bottom"
rows="10"
rowsPerPageTemplate="10,20,50"
>
<f:facet name="header">Daftar Direktorat</f:facet>
<p:column sortBy="#{dir.directoratecode}" filterBy="#{dir.directoratecode}" filterMatchMode="contains" >
<f:facet name="header"><h:outputText value="Kode Direktorat"/></f:facet>
<h:outputText value="#{dir.directoratecode}"/>
<h:commandLink value="#{dir.directoratecode}" actionListener="#{directorateBean.test}"/>
</p:column>
<p:column sortBy="#{dir.directoratename}" filterBy="#{dir.directoratename}" filterMatchMode="contains" >
<f:facet name="header"><h:outputText value="Nama Direktorat"/></f:facet>
<h:outputText value="#{dir.directoratename}"/>
</p:column>
<p:column style="width: 100px;">
<f:facet name="header"><h:outputText value="Direktorat Status"/></f:facet>
<p:commandButton icon="ui-icon ui-icon-check" title="Active" rendered="#{dir.directoratestatus == true}"/>
<p:commandButton icon="ui-icon ui-icon-closethick" title="Inactive" rendered="#{dir.directoratestatus == false}"/>
</p:column>
<p:column style="width: 100px;">
<p:commandButton icon="ui-icon ui-icon-search" title="View #{dir.directoratename} Details"/>
<p:commandButton icon="ui-icon ui-icon-pencil" title="Edit #{dir.directoratename}" action="#{directorateBean.edit(dir)}" update="panelDetail" oncomplete="updateDlg.show();"/>
<p:commandButton icon="ui-icon ui-icon-trash" title="Delete #{dir.directoratename}"/>
</p:column>
</p:dataTable>
</p:panel>
<p:dialog header="Corfirmation" widgetVar="confirmDialog" resizable="false" id="confirm" showEffect="fade" modal="true" >
<h:outputText value="Confirm Delete ?"/>
<h:panelGrid columns="2">
<p:commandButton value="Cancel" update="#form" />
<p:commandButton value="Delete" update="#form"/>
</h:panelGrid>
</p:dialog>
<p:dialog header="Directorate Detail" widgetVar="updateDlg" resizable="false" id="updateDialog" showEffect="fade" modal="true" >
<h:panelGrid id="panelDetail" columns="3" cellpadding="2">
<h:outputLabel for="editDirectorateCode" value="Kode Direktorat*"/>
<p:inputText id="editDirectorateCode" value="#{directorateBean.selectedDirectorate.directoratecode}" required="true" label="Kode Direktorat" maxlength="4"/>
<p:message for="editDirectorateCode"/>
<h:outputLabel for="editDirectorateName" value="Nama Direktorat*"/>
<p:inputText id="editDirectorateName" value="#{directorateBean.selectedDirectorate.directoratename}" required="true" label="Nama Direktorat" maxlength="40"/>
<p:message for="editDirectorateName"/>
<h:outputLabel for="editDirectorateStatus" value="Status Direktorat"/>
<p:selectOneRadio id="editDirectorateStatus" value="#{directorateBean.selectedDirectorate.directoratestatus}">
<f:selectItem itemValue="true" itemLabel="Aktif"/>
<f:selectItem itemValue="false" itemLabel="Tidak Aktif"/>
</p:selectOneRadio>
<p:spacer/>
<p:commandButton id="btnUpdateDirectorate" value="Simpan" update="#form" action="#{directorateBean.update()}"/>
<p:commandButton value="Reset" type="reset"/>
</h:panelGrid>
</p:dialog>
</h:form>
</f:view>
</ui:define>
<ui:define name="bottom">
bottom
</ui:define>
</ui:composition>
and here is the managed bean :
#ManagedBean(name = "directorateBean")
#ViewScoped
public class structuralDirectorateManagedBean implements Serializable {
private DirectorateDAO directorateService;
/* Initialize variable */
private List<Directorate> directorateList;
private Directorate directorate = new Directorate();
private Directorate selectedDirectorate = new Directorate();
private static final String Creator;
private boolean fieldsetCollapse = true;
private boolean DirectorateStatus = false;
/* init */
#PostConstruct
public void init() throws SQLException {
if (directorateService == null) {
directorateService = FactoryDAO.getDirectorateDAO();
}
directorateList = directorateService.findAll();
directorate.setDirectoratestatus(DirectorateStatus);
directorate.setCreator(Creator);
}
/* Basic Method */
public void add() throws SQLException {
System.out.println("Directorate Code : " + directorate.getDirectoratecode());
directorateService.create(directorate); // insert record to datatabse
directorateList.add(directorate); // insert object to List<Object>
directorate = new Directorate(); // Reset Form
directorate.setDirectoratestatus(DirectorateStatus); // set default
directorate.setCreator(Creator); // set default
}
public void test(){
System.out.println("TEST!!");
}
public void edit(Directorate directorate) {
System.out.println("Directorate code to edit : " + directorate.getDirectoratecode());
this.selectedDirectorate = directorate;
}
public void delete(Directorate directorate) throws SQLException {
directorateService.remove(directorate); // delete record within Database
directorateList.remove(directorate); // delete record within List
}
public void update(ActionEvent event) throws SQLException {
directorateService.edit(selectedDirectorate); // update record to database
selectedDirectorate = new Directorate(); // reset form
}
/** Getter Setter */
public List<Directorate> getDirectorateList() throws SQLException {
return directorateList;
}
public Directorate getDirectorate() {
return directorate;
}
public boolean isFieldsetCollapse() {
return fieldsetCollapse;
}
public void setFieldsetCollapse(boolean fieldsetCollapse) {
this.fieldsetCollapse = fieldsetCollapse;
}
public Directorate getSelectedDirectorate() {
return selectedDirectorate;
}
public void setSelectedDirectorate(Directorate selectedDirectorate) {
this.selectedDirectorate = selectedDirectorate;
}
}
everything runs smooth, displaying record from database, p:selectOneMenu runs well.
this button were runs well too :
<p:commandButton id="btnSubmitDirectorate" value="Submit" update="#form" action="#{directorateBean.add()}"/>
but, this button and commandLink were not working, the Action and ActionListener both not fired :
<p:commandButton icon="ui-icon ui-icon-pencil" title="Edit #{dir.directoratename}" action="#{directorateBean.edit(dir)}" update="panelDetail" oncomplete="updateDlg.show();"/>
<p:commandLink value="#{dir.directoratecode}" actionListener="#{directorateBean.test}"/>
i'm sorry if i'm asking too much, maybe i'm the dumb here.. XD
but i just cannot get over it and just begin to frustated..
thanks for your responds.. :)
have a nice day..

You have one big <h:form> in the "Directorate" view with a lot of different sections. When you invoke an action in one of the sections, then the data of all other sections will also be submitted (and validated!). It look like that you didn't notice those "Value is required" validation messages at first sight because those sections are collapsed/hidden by default.
You need to remove the "God" <h:form> and give the fieldset, the datatable and the two dialogs each its own <h:form>. So you should have 4 separate forms, each with its own responsibility. They can however without problems be bound to the one and same bean.
By the way, the <f:view> does not belong in the include files nor the template clients. There should be only one in the master template.

Related

Why does the lookup of my managed bean fail?

Consider the following code:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Crear Personas</title>
</h:head>
<h:body>
<f:view>
<h:form>
<h1><h:outputText value="Create/Edit"/></h1>
<h:panelGrid columns="2">
<h:outputLabel value="Perid:" for="perid" />
<h:inputText id="perid" value="#{lineaControl.persona.perid}" title="Perid" required="true" requiredMessage="The Perid field is required."/>
<h:outputLabel value="Pernombre:" for="pernombre" />
<h:inputText id="pernombre" value="#{lineaControl.persona.pernombre}" title="Pernombre" required="true" requiredMessage="The Pernombre field is required."/>
<h:outputLabel value="Perapellido:" for="perapellido" />
<h:inputText id="perapellido" value="#{lineaControl.persona.perapellido}" title="Perapellido" required="true" requiredMessage="The Perapellido field is required."/>
<h:outputLabel value="Pertelefonofijo:" for="pertelefonofijo" />
<h:inputText id="pertelefonofijo" value="#{lineaControl.persona.pertelefonofijo}" title="Pertelefonofijo" required="true" requiredMessage="The Pertelefonofijo field is required."/>
<h:outputLabel value="Perfechanacimiento:" for="perfechanacimiento" />
<h:inputText id="perfechanacimiento" value="#{lineaControl.persona.perfechanacimiento}" title="Perfechanacimiento" required="true" requiredMessage="The Perfechanacimiento field is required.">
<f:convertDateTime pattern="MM/dd/yyyy" />
</h:inputText>
<h:outputLabel value="Percedula:" for="percedula" />
<h:inputText id="percedula" value="#{lineaControl.persona.percedula}" title="Percedula" required="true" requiredMessage="The Percedula field is required."/>
</h:panelGrid>
<h:commandButton value="Crear" action="#{personaControl.crearPersona()}" ></h:commandButton>
</h:form>
</f:view>
</h:body>
</html>
This one is the view:
#Named(value = "personaControl")
#SessionScoped
public class PersonaControl implements Serializable {
/**
* Creates a new instance of PersonaControl
*/
public PersonaControl() {
persona = new Persona();
}
#EJB
private Persona persona;
PersonaFacade personaFacade;
public Persona getPersona() {
return persona;
}
public void setPersona(Persona persona) {
this.persona = persona;
}
public List<Persona> listarPersonas(){
return personaFacade.findAll();
}
public void crearPersona(){
personaFacade.create(persona);
persona = new Persona();
}
public void eliminarPersona(Persona newPersona){
persona = newPersona;
personaFacade.remove(persona);
persona = new Persona();
}
public String editarPersona(Persona newPersona){
persona = newPersona;
return "/Equipo/Editar";
}
public String editarPersona(){
personaFacade.edit(persona);
persona = new Persona();
return "/Equipo/Editar";
}
}
This one is the controller:
Entity and facade were generated using the NetBeans IDE, so when I try to create the new user, I get this error output:
Exception attempting to inject Remote ejb-ref name=controlador.PersonaControl/persona,Remote 3.x interface =entidades.Persona,ejb-link=null,lookup=,mappedName=,jndi-name=entidades.Persona,refType=Session into class controlador.PersonaControl: Lookup failed for 'java:comp/env/controlador.PersonaControl/persona' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming}
I cannot understand why I am getting this error output. I can't create any user.

p:selectCheckboxMenu Dynamic CSS

I have populated multi select drop down list , i have a scenario like the color of the text in the dropdown should differ based on some condtions . i have populated the values in <f:selectItems>. I have attached the screen shot of the drop down by adding css to rendered html file. How could i add dynamic css for the label
xhtml :
<p:selectCheckboxMenu id="stackListDropDown" filter="true"
style="vertical-align:middle" filterMatchMode="contains"
value="#{stackListForPOCBean.selectedStackListFromDB}"
styleClass="help-inline selectOneMenuDefault">
<!-- <f:selectItem noSelectionOption="false" /> -->
<!-- <p:ajax event="change" process="#this"
listener="#{stackListForPOCBean.subjectSelectionChanged(stack)}" /> -->
<f:selectItems value="#{stackListForPOCBean.stackListFromDB}"
var="stack" itemLabel="#{stack.stackId} - #{stack.stackDesc}"
itemValue="#{stack.stackId}">
</f:selectItems>
<p:ajax update="stackLineGroup" process="#this "
listener="#{stackListForPOCBean.validateMultiSelectStackForPOC()}" />
<p:ajax event="toggleSelect" update="#this stackLineGroup"
listener="#{stackListForPOCBean.validateMultiSelectStackForPOC()}" />
</p:selectCheckboxMenu>
Expected Result using primefaces
I achieved it by the following code,changed to selectManyMenu and added checkbox true
<p:selectManyMenu id="stackListDropDown"
styleClass="help-inline selectOneMenuManyDefault"
value="#{stackListForPOCBean.stackListSelected}" var="t"
filter="true" filterMatchMode="contains" showCheckbox="true">
<f:selectItems value="#{stackListForPOCBean.stackListFromDB}"
var="stack" itemLabel="#{stack.stackId} - #{stack.stackDesc}"
itemValue="#{stack}" />
<p:column>
<h:outputText value="#{t.stackId} - #{t.stackDesc}"
styleClass="#{t.stackColorStyle}" />
</p:column>
<p:ajax update="stackLineGroup" process="#this "
listener="#{stackListForPOCBean.validateMultiSelectStackForPOC()}" />
</p:selectManyMenu>

positionning a command button on the right

I'm using jsf2.2 primefaces 6.0 and i already implemented a graphical solution to view, download and print pictures through galleria component of primefaces.
The issue is that I need help positioning the print commandButton on the right for best ergonomic result.
Here's the wanted result:
Here the XHTML code:
<p:dialog header="Documents numérisés" widgetVar="diag" modal="true"
dynamic="true" showEffect="fade" hideEffect="fade" resizable="true"
position="center" id="diagImages" onShow="download()">
<p:outputPanel id="gal" style="text-align:center;">
<p:galleria value="#{demandeBean.demandeSelectionnee.images}"
panelWidth="500" panelHeight="313" showCaption="false"
autoPlay="false" var="image">
<p:graphicImage id="image"
value="http://localhost:18080/openCars/images/#{image}"
width="500" height="313" />
</p:galleria>
</p:outputPanel>
<p:commandButton value="Print" type="button" icon="ui-icon-print"
style="display:block;margin-bottom: 20px">
<p:printer target="image" />
</p:commandButton>
</p:dialog>
Well, this may not be a perfect solution but it worked for me.
<p:panelGrid>
<p:commandButton value="Print" type="button" icon="ui-icon-print"
style="float:right;">
<p:printer target="image" />
</p:commandButton>
<p:panelGrid>

Same space between 2 inputText boxes using JSF and CSS

I want to create a JSF form where users can write their name and surname and validate them using JSF validators. The problem is that I would like to set the same space between the 2 inputText boxes in the form, in both cases, when the typed data is correct according to the validator, and when it is not correct. So if between the 2 inputText boxes there are X pixels if the user has not typed anything, when typed name is not correct, the error showing message should be displayed between the 2 inputText boxes and the pixels between them should be also X px. I suppose this is a CSS issue, but I am unable to get it, so any help would be appreciated.
Here is my code
<h:form>
<p:panel id="panelregister">
<h:panelGrid columns="1">
<!--Name-->
<p:inputText id="name" label="name" size="32" maxlength="9"
value="#{mManagedBean.name}">
<f:validator validatorId="packagevalidators.NameValidator"/>
</p:inputText>
<p:watermark for="name" value="Name (*)" />
<p:message for="name"/>
<!--Surname-->
<p:inputText id="surname" label="surname" required="true" size="32" maxlength="9"
value="#{mManagedBean.surname}"
requiredMessage="Must enter a surname">
<f:validator validatorId="packagevalidators.SurnameValidator"/>
</p:inputText>
<p:watermark for="surname" value="Surname (*)" />
<p:message for="surname" />
<p:commandButton value="Submit" action="#{mManagedBean.save}" ajax="false" />
</h:panelGrid>
</p:panel>
</h:form>
You are using h:panelGrid which renders as HTML <table>.
p:message will be rendered as div and by default that div won't have any content so it won't take any space you provide.
One way to do this is to use a h:panelGroup around it and give pre defined height of p:message
Example:
<h:panelGroup layout="block" style="height:27px">
<p:message for="name" />
</h:panelGroup>

Scrollbar for outputLabel

I want to implment a scrollbar for a outputLabel but if i use scrollbar component it put every list elment in a new row. The whole output should be disyplayed in one row and the list should be scrollable. Can i fix this problem with a CSS attribute?
i use primefaces 4.0 and jsf 2.1
here is my code
<p:commandButton id="newMessage" update=":contentForm"
actionListener="#{taskboxBean.newMessage}" value="New Message" />
<p:commandButton id="trash"
update=":contentForm, :postForm:tabViewPosts:trashTable, :postForm:tabViewPosts:inboxTable, :postForm:tabViewPosts:sentTable"
actionListener="#{taskboxBean.deleteSelectedTaskbox}" icon="ui-icon-trash" title="Trash" />
<p:scrollPanel>
<ui:repeat var="task_to_user" value="#{taskboxBean.selectedTaskbox.TASKBOX_TO_USERS}">
<p:outputLabel
value="#{task_to_user.USER.EMAIL} #{task_to_user.USER.FIRST_NAME} #{task_to_user.USER.LAST_NAME} #{task_to_user.USER.FIRST_NAME}" />
</ui:repeat>
</p:scrollPanel>
<h:outputText value="#{taskboxBean.selectedTaskbox.TASKTYPE.NAME} " />
<h:outputText value="#{taskboxBean.selectedTaskbox.CREATE_TIMESTAMP} " />

Resources