Hello everyone i am new at JSF-Hibernate. I have a table in my database called "Personels" and I am getting records from there with a function and showing at "ListAllPersonels.xhtml" page. I have a link next to each surname and when click on it the row that clicked should turn to editable area. I have boolean variable for holding value of each records. Everthing is work except editable area link.When click on it, nothing changes. The value of "editable" doesnt turn to "true" from "false". So my codes are here, where is problem? Thanks....
My ManagedBean - PersonelMB;
#ManagedBean
#SessionScoped
public class PersonelMB implements Serializable{
/**
* Creates a new instance of PersonelMB
*/
public PersonelMB() {
}
private int personelid;
private String personelname,personelsurname;
public int getPersonelid() {
return personelid;
}
public void setPersonelid(int personelid) {
this.personelid = personelid;
}
public String getPersonelname() {
return personelname;
}
public void setPersonelname(String personelname) {
this.personelname = personelname;
}
public String getPersonelsurname() {
return personelsurname;
}
public void setPersonelsurname(String personelsurname) {
this.personelsurname = personelsurname;
}
private NewHibernateUtil hu;
private Session s;
private Personels personel;
private List<Personels> plist;
public List<Personels> getAllPersonelsFromDatabase(){
plist=new ArrayList<>();
s=hu.getSessionFactory().openSession();
s.beginTransaction();
Query qu=s.createQuery("from Personels");
plist=qu.list();
s.getTransaction().commit();
s.close();
return plist;
}
public String addRecord(){
Personels p=new Personels();
p.setPersonelName(personelname);
p.setPersonelSurname(personelsurname);
s=hu.getSessionFactory().openSession();
s.beginTransaction();
s.save(p);
s.getTransaction().commit();
s.close();
p=null;
personelname="";
personelsurname="";
return "success";
}
public String editRecord(Personels p){
p.setEditable(true);
return null;
}
public String saveAlteration(Personels pr){
for(Personels pers:mylist)
pers.setEditable(false);
s=hu.getSessionFactory().openSession();
s.beginTransaction();
s.update(pr);
s.getTransaction().commit();
s.close();
return null;
}
public String deleteRecord(Personels p){
s=hu.getSessionFactory().openSession();
s.beginTransaction();
s.delete(p);
s.getTransaction().commit();
s.close();
return "deleted";
}
}
My Java Class - Personels.java
public class Personels implements java.io.Serializable {
private Integer personelId;
private String personelName;
private String personelSurname;
public Personels() {
}
public Personels(String personelName, String personelSurname) {
this.personelName = personelName;
this.personelSurname = personelSurname;
}
public Integer getPersonelId() {
return this.personelId;
}
public void setPersonelId(Integer personelÄ°d) {
this.personelId = personelÄ°d;
}
public String getPersonelName() {
return this.personelName;
}
public void setPersonelName(String personelName) {
this.personelName = personelName;
}
public String getPersonelSurname() {
return this.personelSurname;
}
public void setPersonelSurname(String personelSurname) {
this.personelSurname = personelSurname;
}
boolean editable;
public boolean isEditable() {
return editable;
}
public void setEditable(boolean editable) {
this.editable = editable;
}
}
My index.xhtml
<h:form>
<h:panelGrid columns="2">
<f:facet name="Personel Register Form"/>
<h:outputLabel value="NAME: "/>
<h:inputText value="#{personelMB.personelname}"/>
<h:outputLabel value="SURNAME: "/>
<h:inputText value="#{personelMB.personelsurname}"/>
<h:commandButton value="Reset" type="reset"/>
<h:commandButton value="Save" type="submit" action="#{personelMB.addRecord()}"/>
</h:panelGrid>
<h:commandLink value="List All Personels" action="listallpersonels.xhtml"/>
</h:form>
My listallpersonels.xhtml;
<h:form>
<h:dataTable value="#{personelMB.getAllPersonelsFromDatabase()}" var="p" >
<h:column>
<f:facet name="header">NAME</f:facet>
<h:inputText value="#{p.personelName}" rendered="#{p.editable}" size="15"/>
<h:outputText value="#{p.personelName}" rendered="#{not p.editable}"/>
</h:column>
<h:column>
<f:facet name="header">SURNAME</f:facet>
<h:outputText value="#{p.personelSurname}" rendered="#{not p.editable}"/>
<h:inputText value="#{p.personelSurname}" rendered="#{p.editable}" size="15" />
</h:column>
<h:column>
<f:facet name="header">DELETE</f:facet>
<h:commandLink value="Delete" action="#{personelMB.deleteRecord(p)}"/>
</h:column>
<h:column>
<f:facet name="header">EDIT</f:facet>
<h:commandLink value="Edit" action="#{personelMB.editRecord(p)}"
rendered="#{not p.editable}"/>
<h:commandButton value="Save Alteration" action="#{personelMB.saveAlteration(p)}"
rendered="#{p.editable}" />
</h:column>
</h:dataTable>
</h:form>
You should take a look at primefaces, it had some features. And you should find some responses to your questions.
http://www.primefaces.org/showcase/ui/data/datatable/edit.xhtml
And then try something like this
<p:dataTable value="#{personelMB.getAllPersonelsFromDatabase()}" var="p" editable="true" editMode="cell" widgetVar="cellP">
Related
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<ui:composition>
<h:form>
<h:panelGrid columns="2">
<h:outputText value="New categorie"></h:outputText>
<h:inputText value="${categorieBean.categorie.name}"></h:inputText>
<h:commandButton value="Add categorie" action="#{categorieBean.dodajKategoriju}">
<f:ajax render="#form" execute="#form"></f:ajax>
</h:commandButton>
</h:panelGrid>
<h:dataTable value="${categorieBean.categories}" var="categor">
<h:column>
<f:facet name="header">Name</f:facet>
<h:outputText value="${categor.name}"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">DELETE</f:facet>
<h:commandButton value="Delete-${categor.id}" action="#{categorieBean.obrisiKategoriju()}"
style="width: 64px;background-color: green;color: white;border: none;height: 27px;border-radius:
5px;">
<f:ajax render="#form" execute="#form"></f:ajax>
<f:param value="${categor.id}" name="id"></f:param>
</h:commandButton>
</h:column>
</h:dataTable>
</h:form>
</ui:composition>
</html>
#ManagedBean(name="categorieBean")
#SessionScoped
public class CategorieBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 7281388938441323040L;
private List<Categorie> categories=new ArrayList<Categorie>();
private Categorie categorie=new Categorie();
public CategorieBean() {
// TODO Auto-generated constructor stub
}
public List<Categorie> getCategories() {
categories=CategorieDAO.readAll();
return categories;
}
public void setCategories(List<Categorie> categories) {
this.categories = categories;
}
public Categorie getCategorie() {
return categorie;
}
public void setCategorie(Categorie categorie) {
this.categorie = categorie;
}
public String obrisiKategoriju() {
System.out.println("hocu da obrisem kategoriju");
HttpServletRequest req=(HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
System.out.println(req.getParameter("id"));
Map<String,String> mapa=FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
for(Entry<String,String>pom:mapa.entrySet()) {
System.out.println(pom.getKey()+" "+pom.getValue());
}
if(mapa.containsKey("id")) {
System.out.println("Usao sam");
int id=Integer.parseInt(mapa.get("id"));
CategorieDAO.delete(id);
}
return "";
}
public String dodajKategoriju() {
CategorieDAO.insert(categorie);
return "";
}
}
I do not send id param to Bean in method obrisiKategoriju() when I print value of requestparammap I get only j_idt6 j_idt6
j_idt6:j_idt9
j_idt6:j_idt11:0:j_idt17 Delete-1
javax.faces.ViewState 9066186310981453487:-2255221033905007338 someone have idea? I have another Bean but it work perfect, with these it does not, I tried everything
I would like to have a 'Show All' button for a primefaces datatable, however, I'm experiencing some trouble. Here's some test code that demostrates the problem:
Test.xhtml:
...
<h:form>
<p:selectBooleanCheckbox value="#{testBean.showAll}" itemLabel="Show All">
<p:ajax update="#form"/>
</p:selectBooleanCheckbox>
<p:panel id="test">
<p:dataTable id="values" var="value" value="#{testBean.fullList}" filteredValue="#{testBean.filteredList}" paginatorAlwaysVisible="false"
paginator="#{!testBean.showAll}" rows="#{!testBean.showAll ? 2 : null }" widgetVar="valuesTable"
emptyMessage="No records found.">
<p:column id="enum" sortBy="#{value.toString()}" filterBy="#{value.toString()}" filterMatchMode="contains">
<f:facet name="header">
<h:outputText value="Enum" />
</f:facet>
<h:outputText value ="#{value.toString()}"/>
</p:column>
<p:column id="name" sortBy="#{value.name}" filterBy="#{value.name}" filterMatchMode="contains">
<f:facet name="header">
<h:outputText value="Name" />
</f:facet>
<h:outputText value="#{value.name}" />
</p:column>
</p:dataTable>
</p:panel>
</h:form>
...
And here's TestBean.java:
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
#Named(value = "testBean")
#SessionScoped
public class TestBean implements Serializable {
public static enum Value {
ONE, TWO, THREE;
public String getName() {
return name().toLowerCase();
}
#Override
public String toString() {
return this.name();
}
}
/**
* Creates a new instance of TestBean
*/
public TestBean() {
}
private boolean showAll = false;
private List<Value> fullList = Arrays.asList(Value.values());
private List<Value> filteredList;
public boolean isShowAll() {
return showAll;
}
public void setShowAll(boolean showAll) {
this.showAll = showAll;
}
public List<Value> getFullList() {
return fullList;
}
public void setFullList(List<Value> fullList) {
this.fullList = fullList;
}
public List<Value> getFilteredList() {
return filteredList;
}
public void setFilteredList(List<Value> filteredList) {
this.filteredList = filteredList;
}
}
If I don't change tabs, the page works as expected: toggling the 'Show All' button updates the table to show all 3 values, or only two. However, if show all is not checked (only 2 rows are showing), and I click to the 2nd page of the table to view the third record, and then click 'Show All', the table does not update properly. It removes the paginator from the top of the table (as expected), but still only shows the 3rd record. And if I then uncheck show all and navigate back to the first page of the datatable, it is now broken too.
I've tried changing the ajax update statement to the id of the table, but that didn't change the results.
Am I doing something wrong? Thanks in advance.
You have to programmatically set the values you need directly on the DataTable component (I used explicit first and rows bean properties just for more readability):
<h:form>
<p:selectBooleanCheckbox value="#{testBean.showAll}" itemLabel="Show All">
<p:ajax update="#form" />
</p:selectBooleanCheckbox>
<p:panel id="test">
<p:dataTable id="values" var="value" value="#{testBean.fullList}"
filteredValue="#{testBean.filteredList}" paginatorAlwaysVisible="false"
paginator="#{!testBean.showAll}" first="#{testBean.first}" rows="#{testBean.rows}"
widgetVar="valuesTable" emptyMessage="No records found.">
<p:column id="enum" sortBy="#{value.toString()}" filterBy="#{value.toString()}"
filterMatchMode="contains">
<f:facet name="header">
<h:outputText value="Enum" />
</f:facet>
<h:outputText value="#{value.toString()}" />
</p:column>
<p:column id="name" sortBy="#{value.name}" filterBy="#{value.name}"
filterMatchMode="contains">
<f:facet name="header">
<h:outputText value="Name" />
</f:facet>
<h:outputText value="#{value.name}" />
</p:column>
</p:dataTable>
</p:panel>
</h:form>
and
#Named
#ViewScoped
public class TestBean implements Serializable
{
private static final long serialVersionUID = 1L;
public static enum Value
{
ONE, TWO, THREE;
public String getName()
{
return name().toLowerCase();
}
#Override
public String toString()
{
return name();
}
}
private boolean showAll = false;
private int first = 0;
private int rows = 2;
private List<Value> fullList = Arrays.asList(Value.values());
private List<Value> filteredList;
public boolean isShowAll()
{
return showAll;
}
public void setShowAll(boolean showAll)
{
this.showAll = showAll;
first = 0;
rows = showAll ? 0 : 2;
filteredList = null;
// get the FacesContext instance
FacesContext context = FacesContext.getCurrentInstance();
// get the current component (p:selectBooleanCheckbox)
UIComponent component = UIComponent.getCurrentComponent(context);
// find DataTable within the same NamingContainer
DataTable table = (DataTable) component.findComponent("values");
// reset first row index
table.setFirst(first);
// reset last row index
table.setRows(rows);
// reset filterd value
table.setFilteredValue(null);
}
// all other getters/setters
}
Tested on Wildfly 10.0.0.Final with JSF 2.3.0-m06 (Mojarra) and PrimeFaces 6.0.2
I had the same problem, was resolved by this steps:
Adding styleClass="all-paginator" to datatable
<p:dataTable id="values" var="value" value="#{testBean.fullList}"
filteredValue="#{testBean.filteredList}" styleClass="all-paginator"..>
Add a getter of number of all records in the controller (TestBean.java) like:
public int getRecordNumber() {
if(CollectionUtils.isEmpty(fullList)){
return 0;
}
return fullList.size();
}
Add the property of the recordNumber at the end of the datatable attribute "rowsPerPageTemplate"
<p:dataTable id="values" var="value" value="#{testBean.fullList}" filteredValue="#{testBean.filteredList}"
styleClass="all-paginator" rowsPerPageTemplate="20,30,40,50,#{testBean.recordNumber}"..>
In my application (RichFaces 4.1) I have an extendedDataTable, in my backing bean I need to track the selected rows. I achieve this with the following code:
JSF:
<rich:extendedDataTable id="freie"
selectionMode="multipleKeyboardFree"
selection="#{myBean.tableSelection}"
...
<a4j:ajax execute="#this" event="selectionchange"
listener="#{myBean.tableSelection}"
render="indicatorPanel" />
Java:
UIExtendedDataTable dataTable= (UIExtendedDataTable) event.getComponent();
Object originalKey= dataTable.getRowKey();
_tableSelectedEntries.clear();
for (Object selectionKey: _tableSelection) {
dataTable.setRowKey(selectionKey);
if (dataTable.isRowAvailable()) {
_tableSelectedEntries.add((Entry) dataTable.getRowData());
}
}
dataTable.setRowKey(originalKey);
This works fine, as long as the table is not filtered. I use the standard RichFaces way to filter the table:
<rich:column sortBy="#{mitarbeiter.vorname}"
filterValue="#{mitarbeiterFilterBean.firstNameFilter}"
filterExpression="#{fn:containsIgnoreCase(mitarbeiter.vorname, mitarbeiterFilterBean.firstNameFilter)}">
When the table is filtered and I select for instance the first row, I get the rowKey for the first row of the unfiltered table in the backing bean. How can I get the rowData of the selected row when my table is filtered?
I think my code works the same way as in the showcase.
I could solve my problem by making my filter bean SessionScoped. I also don't bind the currently selected rows to my backing bean anymore. I get the selected rows using:
public void tableSelection (AjaxBehaviorEvent event) {
UIExtendedDataTable dataTable= (UIExtendedDataTable) event.getComponent();
for (Object selectionKey: dataTable.getSelection()) {
It could also be achieved using rowKeyVar to get the correct row index.
Maybe you have overlooked something because I tried it and it works.
I copied the source for selectableTable and added the filter method from filterTable
Example usage: To get the selected item/items data just use a get method for selected items list
Source code (xhtml):
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:rich="http://richfaces.org/rich"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:fn="http://java.sun.com/jsp/jstl/functions"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Richfaces Welcome Page</title>
</h:head>
<h:body>
<h:panelGrid columns="2">
<h:form>
<fieldset style="margin-bottom: 10px;">
<legend>
<h:outputText value="Selection Mode " />
</legend>
<h:selectOneRadio value="#{exTableSelect.selectionMode}">
<f:selectItem itemLabel="Single" itemValue="single" />
<f:selectItem itemLabel="Multiple" itemValue="multiple" />
<f:selectItem itemLabel="Multiple Keyboard-free" itemValue="multipleKeyboardFree" />
<a4j:ajax render="table, res" />
</h:selectOneRadio>
</fieldset>
<rich:extendedDataTable value="#{exTableSelect.inventoryItems}" var="car"
selection="#{exTableSelect.selection}" id="table" style="height:300px; width:500px;"
selectionMode="#{exTableSelect.selectionMode}">
<a4j:ajax execute="#form" event="selectionchange" listener="#{exTableSelect.selectionListener}"
render=":res" />
<f:facet name="header">
<h:outputText value="Cars marketplace" />
</f:facet>
<rich:column filterValue="#{exTableSelect.vendorFilter}"
filterExpression="#{fn:containsIgnoreCase(car.vendor, exTableSelect.vendorFilter)}">
<f:facet name="header">
<h:panelGroup>
<h:outputText value="Vendor " />
<h:inputText value="#{exTableSelect.vendorFilter}">
<a4j:ajax render="table" execute="#this" event="change" />
</h:inputText>
</h:panelGroup>
</f:facet>
<h:outputText value="#{car.vendor}" />
</rich:column>
</rich:extendedDataTable>
</h:form>
<a4j:outputPanel id="res">
<rich:panel header="Selected Rows:" rendered="#{not empty exTableSelect.selectionItems}">
<rich:list type="unordered" value="#{exTableSelect.selectionItems}" var="sel">
<h:outputText value="#{sel.vendor} - #{sel.model} - #{sel.price}" />
</rich:list>
</rich:panel>
</a4j:outputPanel>
</h:panelGrid>
</h:body>
</html>
Managed Bean:
public class ExTableSelect {
private String selectionMode = "multiple";
private Collection<Object> selection;
private List<InventoryItem> inventoryItems;
private List<InventoryItem> selectionItems = new ArrayList<InventoryItem>();
private String vendorFilter;
public void selectionListener(AjaxBehaviorEvent event) {
UIExtendedDataTable dataTable = (UIExtendedDataTable) event.getComponent();
Object originalKey = dataTable.getRowKey();
selectionItems.clear();
for (Object selectionKey : selection) {
dataTable.setRowKey(selectionKey);
if (dataTable.isRowAvailable()) {
selectionItems.add((InventoryItem) dataTable.getRowData());
}
}
dataTable.setRowKey(originalKey);
}
public Filter<?> getFilterVendor() {
return new Filter<InventoryItem>() {
public boolean accept(InventoryItem t) {
String vendor = getVendorFilter();
if (vendor == null || vendor.length() == 0 || vendor.equals(t.getVendor())) {
return true;
}
return false;
}
};
}
#PostConstruct
public void addInventory(){
InventoryItem i = new InventoryItem();
i.setVendor("A");
InventoryItem i2 = new InventoryItem();
i2.setVendor("AB");
InventoryItem i3 = new InventoryItem();
i3.setVendor("AC");
InventoryItem i4= new InventoryItem();
i4.setVendor("E");
InventoryItem i5 = new InventoryItem();
i5.setVendor("F");
InventoryItem i6 = new InventoryItem();
i6.setVendor("G");
InventoryItem i7 = new InventoryItem();
i7.setVendor("H");
InventoryItem i8 = new InventoryItem();
i8.setVendor("I");
InventoryItem i9 = new InventoryItem();
i9.setVendor("J");
inventoryItems= new ArrayList<InventoryItem>();
inventoryItems.add(i);
inventoryItems.add(i2);
inventoryItems.add(i3);
inventoryItems.add(i4);
inventoryItems.add(i5);
inventoryItems.add(i6);
inventoryItems.add(i7);
inventoryItems.add(i8);
inventoryItems.add(i9);
}
public Collection<Object> getSelection() {
return selection;
}
public void setSelection(Collection<Object> selection) {
this.selection = selection;
}
public List<InventoryItem> getInventoryItems() {
return inventoryItems;
}
public void setInventoryItems(List<InventoryItem> inventoryItems) {
this.inventoryItems = inventoryItems;
}
public InventoryItem getSelectionItem() {
if (selectionItems == null || selectionItems.isEmpty()) {
return null;
}
return selectionItems.get(0);
}
public List<InventoryItem> getSelectionItems() {
return selectionItems;
}
public void setSelectionItems(List<InventoryItem> selectionItems) {
this.selectionItems = selectionItems;
}
public String getSelectionMode() {
return selectionMode;
}
public void setSelectionMode(String selectionMode) {
this.selectionMode = selectionMode;
}
public void setVendorFilter(String vendorFilter) {
this.vendorFilter = vendorFilter;
}
public String getVendorFilter() {
return vendorFilter;
}
}
In the following scenario, where I have several dataTables that I build iterating over a ui:repeat tag, I have experienced that row listener returns null for selected elements that are not in the first table.
In order to understand the model object, I have several menus, and each of them contains several areas.
Any help will be much appreciated.
xhtml below:
<ui:repeat value="#{someBean.menus}" var="menu">
<p:dataTable var="area" value="#{menu.areas}"
rowKey="#{area.id}" selection="#{menu.area}" selectionMode="single">
<p:ajax event="rowSelect" listener="#{someBean.rowSelected}" />
<f:facet name="header">#{menu.name}</f:facet>
<p:column>
<f:facet name="header"></f:facet>
<h:outputText value="#{area.id}" />
</p:column>
<p:column>
<f:facet name="header">Area name</f:facet>
<h:outputText value="#{area.name}" />
</p:column>
</p:dataTable>
</ui:repeat>
ListDataModel java implementation:
public class Areas extends ListDataModel<Area> implements SelectableDataModel<Area>, Serializable {
private static final long serialVersionUID = -9102592194300556930L;
public Areas() {
}
public Areas(List<Area> data) {
super(data);
}
#Override
public Area getRowData(String rowKey) {
#SuppressWarnings("unchecked")
List<Area> areas = (List<Area>)getWrappedData();
for (Area area : areas) {
if (String.valueOf(area.getId()).equals(rowKey)) {
return area;
}
}
return null;
}
#Override
public Object getRowKey(Area area) {
return area.getId();
}
}
public class Menus extends ListDataModel<Menu> implements SelectableDataModel<Menu>, Serializable {
private static final long serialVersionUID = -4079772686830676202L;
public Menus() {
}
public Menus(List<Menu> data) {
super(data);
}
#Override
public Menu getRowData(String rowKey) {
#SuppressWarnings("unchecked")
List<Menu> menus = (List<Menu>)getWrappedData();
for (Menu menu : menus) {
if (String.valueOf(menu.getId()).equals(rowKey)) {
return menu;
}
}
return null;
}
#Override
public Object getRowKey(Menu menu) {
return menu.getId();
}
}
...in the bean someBean
public void rowSelected(SelectEvent event) {
Area selectedArea = (Area)event.getObject(); //This, is null for other tables except the first
System.out.println("SELECTED AREA:" + selectedArea);
//...
}
I am glad to show you the solution: replace ui:repeat with another p:dataTable !!!
<p:dataTable value="#{someBean.menus}" var="menu">
<p:column>
<p:dataTable var="area" value="#{menu.areas}"
rowKey="#{area.id}" selection="#{menu.area}" selectionMode="single">
<p:ajax event="rowSelect" listener="#{someBean.rowSelected}" />
<f:facet name="header">#{menu.name}</f:facet>
<p:column>
<f:facet name="header"></f:facet>
<h:outputText value="#{area.id}" />
</p:column>
<p:column>
<f:facet name="header">Area name</f:facet>
<h:outputText value="#{area.name}" />
</p:column>
</p:dataTable>
</column>
</p:dataTable>
My result tables/images will be displayed in the same page. But the submit button is not invoked. I have two drop downs on which the values of the other two drop downs are dependent. If I am writting immediate="true" the method is invoked but only two values are set with whom I have associated processValueChange action the other selected values are not updated in the bean.
The jsp page is as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%#taglib uri="http://www.ibm.com/jsf/html_extended" prefix="hx"%>
<%#taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%#taglib uri="http://java.sun.com/jsf/html" prefix="h"%><%#taglib
uri="http://www.ibm.com/jsf/BrowserFramework" prefix="odc"%>
<html>
<head>
<script language="javascript" src="Validation.js"></script>
<link rel="stylesheet" href="../css/style.css" type="text/css">
<title>SLA DASHBOARD</title>
</head>
<f:view>
<body>
<hx:scriptCollector id="scriptCollector1">
<div id="mDiv" class="pStyle">
<h:form styleClass="form" id="LoginForm" ><br>
<br>
<div id="Header" class="hStyle">
<h:outputText value= "WELCOME TO SLA DASHBOARD" />
</div>
<br><br>
<div id="fDiv" >
<fieldset class="fStyle">
<legend style="text-align: left;padding: 6px; font-weight:bold; font-size: 14">PLEASE MAKE YOUR SELECTION</legend><br>
<h:outputText value="BU"/>
<h:selectOneMenu id="slaBU" value="#{LoginForm.slaPeriod}"
onchange="this.form.submit();" valueChangeListener="#{LoginForm.processBUChange}">
<f:selectItems value="#{LoginForm.buList}" />
</h:selectOneMenu>
<h:outputText>Application</h:outputText>
<h:selectOneMenu id="slaApplication" style="width:160px" value="#{LoginForm.slaApp}" required="true">
<f:selectItems value="#{LoginForm.appList}" />
</h:selectOneMenu>
<h:outputText>Period</h:outputText>
<h:selectOneMenu id="slaPeriod" value="#{LoginForm.slaPeriod}" onchange="this.form.submit()"
valueChangeListener="#{LoginForm.processPeriodChange}" >
<f:selectItems value="#{LoginForm.periodList}" />
</h:selectOneMenu>
<h:selectOneMenu id="slaPeriod1" style="width:100px"
value="#{LoginForm.slaPeriod1}" required="true">
<f:selectItems value="#{LoginForm.periodList1}" />
</h:selectOneMenu>
<h:selectOneMenu id="slaPeriod2" value="#{LoginForm.slaPeriod2}" required="true">
<f:selectItems value="#{LoginForm.periodList2}" />
</h:selectOneMenu>
<h:outputText value="SLA Group" />
<h:selectOneMenu id="slaGroup" value="#{LoginForm.slaGroup}" required="true">
<f:selectItems value="#{LoginForm.groupList}" />
</h:selectOneMenu>
<h:outputText>View</h:outputText>
<h:selectOneMenu id="slaView" value="#{LoginForm.slaView}" required="true">
<f:selectItems value="#{LoginForm.viewList}" />
</h:selectOneMenu>
<h:commandButton id="submitButton" value="Submit"
type="submit"
style="width:60px;" action="#{LoginForm.processSubmit}"
/>
<br>
<br>
</fieldset>
</div><br><br><br>
<div id="tDiv" >
<h:dataTable id="bTable" value="#{LoginForm.BT}" var="BillingTable"
style="font-weight: bold; text-align: center;" bgcolor="#D4D7FE"
border="4" cellpadding="1" width="60%"
rendered="#{LoginForm.btDisplay}">
<f:facet name="header">
<h:outputText value="Billing Report" style="font-weight: bold"/>
</f:facet>
<h:column>
<f:facet name="header">
<h:outputText value="Buisness Unit" />
</f:facet>
<h:outputText value="#{BillingTable.buName}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Completion Date" />
</f:facet>
<h:outputText value="#{BillingTable.completionDate}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Target Date" />
</f:facet>
<h:outputText value="#{BillingTable.targetDate}"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Billing Cycle" />
</f:facet>
<h:outputText value="#{BillingTable.billingCyle}"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Status" />
</f:facet>
<h:outputText value="#{BillingTable.status}"></h:outputText>
</h:column>
</h:dataTable>
<br>
<h:dataTable id="uTable" value="#{LoginForm.UT}" var="UptimeTable"
style="text-align: center" bgcolor="#D4D7FE"
border="4" cellpadding="1" width="60%" rendered="#{LoginForm.utDisplay}">
<f:facet name="header">
<h:outputText value="Uptime Report" style="font-style: normal; font-weight: bold"/>
</f:facet>
<h:column>
<f:facet name="header">
<h:outputText value="Application Name" style="width:60px" />
</f:facet>
<h:outputText value="#{UptimeTable.applicationName}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Application Class" />
</f:facet>
<h:outputText value="#{UptimeTable.applicationClass}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Availability-Actual" />
</f:facet>
<h:outputText value="#{UptimeTable.actual}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Availability-Target" />
</f:facet>
<h:outputText value="#{UptimeTable.target}"></h:outputText>
</h:column>
</h:dataTable>
<br>
<h:dataTable id="tTable" value="#{LoginForm.TT}" var="TATTable"
style="font-weight: bold; text-align: center" bgcolor="#D4D7FE"
border="4" cellpadding="1" width="60%"
rendered="#{LoginForm.ttDisplay}">
<f:facet name="header">
<h:outputText value="Turn Around Time Report" />
</f:facet>
<h:column>
<f:facet name="header">
<h:outputText value="Application Name" />
</f:facet>
<h:outputText value="#{TATTable.applicationName}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Application Class" />
</f:facet>
<h:outputText value="#{TATTable.applicationClass}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="TAT Average" />
</f:facet>
<h:outputText value="#{TATTable.tatAverage}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="TAT Target" />
</f:facet>
<h:outputText value="#{TATTable.tatTarget}"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Request Count" />
</f:facet>
<h:outputText value="#{TATTable.reqCount}"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Request Type" />
</f:facet>
<h:outputText value="#{TATTable.reqType}"></h:outputText>
</h:column>
</h:dataTable>
</div>
<br>
<h:graphicImage styleClass="graphicImage" id="reportImage" alt="Sorry records not found !!"
value="#{LoginForm.imageLocation}" width="700" height="250" rendered="#{LoginForm.viewImage}">
</h:graphicImage><br>
<h3><h:outputText value="Sorry records not found !!" rendered="#{LoginForm.notFoundMsg}"/></h3>
</h:form>
</div>
</hx:scriptCollector></body>
</f:view>
</html>
My backing bean Login Form is as follows:
package sla.dashboard.form;
import sla.dashboard.Search.SearchManager;
import sla.dashboard.drop_down_components.*;
import sla.dashboard.datatables.*;
import java.util.*;
import javax.faces.context.FacesContext;
import javax.faces.event.*;
import javax.faces.model.*;
public class LoginForm implements ValueChangeListener,ActionListener
{
private String slaBU=null,slaPeriod1=null,slaPeriod2=null,slaGroup=null,
periodType=null,periodSelected=null,slaView=null,period=null,
imageResult=null,imageLocation=null;
private int slaPeriod,slaApp;
Boolean btDisplay,utDisplay,ttDisplay,viewImage,notFoundMsg;
//Table Lists
List <BillingTable> BT;
List <UptimeTable> UT;
List <TATTable> TT;
DropDownComponents ddC;
List<SelectItem> buList;
List<SelectItem> periodList;
List<SelectItem> periodList1;
List<SelectItem> periodList2;
List<SelectItem> groupList;
List<SelectItem> appList;
List<SelectItem> viewList;
ArrayList<String> result= new ArrayList<String>();
public LoginForm()
{
ddC=new DropDownComponents();
buList=ddC.getBuList();
this.reset();
BT=new ArrayList<BillingTable>();
UT=new ArrayList<UptimeTable>();
TT=new ArrayList<TATTable>();
System.out.println("\n Back Bean Object Instantiated");
}
public int getSlaApp() {
return slaApp;
}
public Boolean getBtDisplay() {
return btDisplay;
}
public void setBtDisplay(Boolean btDisplay) {
this.btDisplay = btDisplay;
}
public Boolean getUtDisplay() {
return utDisplay;
}
public void setUtDisplay(Boolean utDisplay) {
this.utDisplay = utDisplay;
}
public Boolean getTtDisplay() {
return ttDisplay;
}
public void setTtDisplay(Boolean ttDisplay) {
this.ttDisplay = ttDisplay;
}
public void setSlaApp(int slaApp) {
this.slaApp = slaApp;
}
public int getSlaPeriod() {
return slaPeriod;
}
public void setSlaPeriod(int slaPeriod) {
this.slaPeriod = slaPeriod;
}
public String getPeriod() {
return period;
}
public void setPeriod(String period)
{
this.period = period;
}
public List<SelectItem> getViewList() {
return viewList;
}
public void setViewList(List<SelectItem> viewList) {
this.viewList = viewList;
}
public List<SelectItem> getPeriodList() {
return periodList;
}
public void setPeriodList(List<SelectItem> periodList) {
this.periodList = periodList;
}
public List<SelectItem> getBuList()
{
return buList;
}
public String getPeriodType() {
System.out.println("Inside getPeriodType " + periodType);
return periodType;
}
public void setPeriodType(String periodType) {
System.out.println("Inside setPeriodType "+ periodType);
this.periodType = periodType;
}
public void setBuList(List<SelectItem> buList) {
this.buList = buList;
}
public String getSlaBU() {
return slaBU;
}
public void setSlaBU(String slaBU)
{
this.slaBU = slaBU;
}
public String getSlaPeriod1()
{
return slaPeriod1;
}
public void setSlaPeriod1(String slaPeriod1) {
this.slaPeriod1 = slaPeriod1;
}
public String getSlaPeriod2() {
return slaPeriod2;
}
public void setSlaPeriod2(String slaPeriod2) {
this.slaPeriod2 = slaPeriod2;
}
public String getPeriodSelected() {
return periodSelected;
}
public void setPeriodSelected(String periodSelected) {
this.periodSelected = periodSelected;
}
public List<SelectItem> getPeriodList1() {
return periodList1;
}
public void setPeriodList1(List<SelectItem> periodList1) {
this.periodList1 = periodList1;
}
public List<SelectItem> getPeriodList2() {
return periodList2;
}
public void setPeriodList2(List<SelectItem> periodList2) {
this.periodList2 = periodList2;
}
public String getSlaGroup() {
return slaGroup;
}
public void setSlaGroup(String slaGroup) {
this.slaGroup = slaGroup;
}
public List<SelectItem> getGroupList() {
return groupList;
}
public void setGroupList(List<SelectItem> groupList) {
this.groupList = groupList;
}
public List<SelectItem> getAppList() {
return appList;
}
public void setAppList(List<SelectItem> appList) {
this.appList = appList;
}
public String getSlaView() {
return slaView;
}
public void setSlaView(String slaView) {
this.slaView = slaView;
}
//ValueChangeListener
public void processValueChange(ValueChangeEvent arg0) throws AbortProcessingException
{}
public void processPeriodChange(ValueChangeEvent arg0) throws AbortProcessingException
{
this.setSlaPeriod(Integer.parseInt(arg0.getNewValue().toString()));
this.setPeriodList1(ddC.getPeriodList1(Integer.parseInt(arg0.getNewValue().toString())));
System.out.println("Period Changed Value : "+this.getSlaPeriod());
FacesContext.getCurrentInstance().renderResponse();
}
public void processBUChange(ValueChangeEvent arg0) throws AbortProcessingException
{
this.setSlaBU(arg0.getNewValue().toString());
this.setAppList(ddC.getAppList(Integer.parseInt(arg0.getNewValue().toString())));
System.out.println("\n BU Slected : " + this.getSlaBU());
FacesContext.getCurrentInstance().renderResponse();
}
public void processAction(ActionEvent arg0) throws AbortProcessingException
{}
public List<BillingTable> getBT() {
return BT;
}
public void setBT(List<BillingTable> bt) {
BT = bt;
}
public List<UptimeTable> getUT() {
return UT;
}
public void setUT(List<UptimeTable> ut) {
UT = ut;
}
public List<TATTable> getTT() {
return TT;
}
public void setTT(List<TATTable> tt) {
TT = tt;
}
public void setImageLocation(String imageLocation) {
this.imageLocation = imageLocation;
}
public String getImageLocation() {
return imageLocation;
}
String getLabel(String index,List<SelectItem> lst)
{
return(lst.get(Integer.parseInt(index)).getLabel());
}
public Boolean getViewImage() {
return viewImage;
}
public void setViewImage(Boolean viewImage) {
this.viewImage = viewImage;
}
public void setImageResult(String imageResult) {
this.imageResult = imageResult;
}
public String getImageResult() {
return imageResult;
}
public Boolean getNotFoundMsg() {
return notFoundMsg;
}
public void setNotFoundMsg(Boolean notFoundMsg) {
this.notFoundMsg = notFoundMsg;
}
public void reset()
{
slaPeriod=0;
slaApp=0;
slaBU="0";
slaPeriod1="0";
slaPeriod2="0";
slaGroup="0";
slaView="0";
periodList=ddC.getPeriodList();
periodList1=ddC.getPeriodList1(slaPeriod);
periodList2=ddC.getPeriodList2();
groupList=ddC.getSlaGroupList();
appList=ddC.getAppList(slaApp);
viewList=ddC.getViewList();
this.setViewImage(false);
this.setUtDisplay(false);
this.setTtDisplay(false);
this.setBtDisplay(false);
this.setNotFoundMsg(false);
}
public String processSubmit()
{
System.out.println("Inside Process Submit ");
System.out.println(this.getSlaBU()+this.getSlaApp()+this.getSlaPeriod()+this.getSlaPeriod1()+
this.getSlaPeriod2()+this.getSlaGroup()+this.getSlaView());
this.setViewImage(false);
this.setUtDisplay(false);
this.setTtDisplay(false);
this.setBtDisplay(false);
this.setNotFoundMsg(false);
/*slaPeriod=1;
slaApp=0;
slaBU="1";
slaPeriod1="1";
slaPeriod2="2";
slaGroup="2";
slaView="1";*/
if(this.slaGroup=="1" && slaView=="1")
{
BT=new SearchManager().searchBillingTable
(slaBU,periodList.get(slaPeriod).getLabel(),
this.getSlaPeriod1(),getLabel(slaPeriod2,
periodList2),getLabel(slaGroup,groupList));
if(BT.size()!=0)
this.setBtDisplay(true);
else
this.setNotFoundMsg(true);
}
else
{
if(this.slaGroup=="2" && slaView=="1")
{
UT=new SearchManager().searchUptimeTable
(slaBU,periodList.get(slaPeriod).getLabel(),
this.getSlaPeriod1(),getLabel(slaPeriod2,
periodList2),getLabel(slaGroup,groupList));
if(UT.size()!=0)
this.setUtDisplay(true);
else
this.setNotFoundMsg(true);
}
else
{
if(this.slaGroup=="3" && slaView=="1")
{
TT=new SearchManager().searchTATTable(slaBU,periodList.get(slaPeriod).getLabel(),
this.getSlaPeriod1(),getLabel(slaPeriod2,
periodList2),getLabel(slaGroup,groupList));
if(TT.size()!=0)
this.setTtDisplay(true);
else
this.setNotFoundMsg(true);
}
else
{
if(slaView=="2")
{
imageResult=new SearchManager().searchImage(slaBU,
periodList.get(slaPeriod).getLabel(), slaPeriod1,
slaPeriod2,getLabel(slaGroup,groupList),slaView);
this.setImageLocation(imageResult);
this.setViewImage(true);
}
else
{
this.setNotFoundMsg(true);
}
}
}
}
return("Success");
}
}
How to submit the form properly?
When a form is not being submitted, sometimes it may happen from a mistaken usage of JSF component.
I see that you are using
<h:selectOneMenu id="slaView" value="#{LoginForm.slaView}" required="true">
<f:selectItems value="#{LoginForm.viewList}" />
</h:selectOneMenu>
which comes from:
viewList=ddC.getViewList();
and the selected item goes into a String value.
can you please write the code of getViewList ?
If the items are declared of int,String - than the problem is that slaView should be of type int.
You're calling FacesContext.getCurrentInstance().renderResponse(); inside a valueChangeListener method whenever a dropdown value changes. This will cause the remaining phases of the JSF lifecycle being skipped until the render response phase. Since the valueChangeListener method is invoked during validations phase, the update model values and invoke action phases are skipped. And guess what, the submit button is to be invoked during invoke action phase, but this is been skipped!
Anyway, creating dynamic dependent dropdown menus wherein the data is fully to be retrieved from the server side without any help of advanced JavaScript and Ajax is a real pain. You've to take a lot of things into account with regard to skipping validation of other fields and retaining submitted values of other fields. So also in JSF. Long story short, here's an article which describes how to do it properly: Populate child menu's.