Simplest JSF 2.0 application will not work - java

I've never had such a problem getting the simplest stuff to work in Java EE 6. I have an extremely basic project. I've tried debugging. Etc. I don't know what to do.
Here is my bean.
#Named(value = "contactsBean")
#SessionScoped
public class ContactsBean implements Serializable {
#EJB
ContactsFacade contactsEJB;
private List<Contacts> contacts = new ArrayList<Contacts>();
public ContactsBean() {
}
public String next() {
contacts = contactsEJB.findAll();
return "index";
}
/**
* #return the contacts
*/
public List<Contacts> getContacts() {
return contacts;
}
/**
* #param contacts the contacts to set
*/
public void setContacts(List<Contacts> contacts) {
this.contacts = contacts;
}
}
Ok. Pretty basic.
Here is my xhtml page.
<ui:define name="content">
<h:form>
<h:dataTable value="#{contactsBean.contacts}" var="contacts">
<h:column>
<f:facet name="header">
<h:outputText value="Name"/>
</f:facet>
<h:outputText value="#{contacts.name}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Street"/>
</f:facet>
<h:outputText value="#{contacts.street}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="City"/>
</f:facet>
<h:outputText value="#{contacts.city}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="State"/>
</f:facet>
<h:outputText value="#{contacts.state}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Zip"/>
</f:facet>
<h:outputText value="#{contacts.zip}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Country"/>
</f:facet>
<h:outputText value="#{contacts.country}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Sent?"/>
</f:facet>
<h:selectBooleanCheckbox value="#{contacts.sent}" />
</h:column>
</h:dataTable>
<h:commandButton value="next >" action="#{contactsBean.next}"/>
</h:form>
</ui:define>
Ok when I hit the next button my list should populate. At least it always did in Java EE 5 stuff. I'm not sure what I'm doing wrong. I was trying to do a simple paginator but even that wouldn't work. The model never gets updated.
What in the world is going on. I tried it without an EJB and it didn't work either.

I found out the reason.
There are to different packages with SessionScoped annotations.
javax.enterprise.context.SessionScoped and javax.faces.context.SessionScoped.
Since I'm using #Named(value = "contactsBean") injection I need to use javax.enterprise. #ManagedBean(name = "contactsBean") goes with javax.faces.context.

Related

How to reach a bean string variable from xhtml and send it to a list method?

I am trying to send a inputtext variable which i get from user and then send it to a method in my bean page so it can be replaced in my query. I will get list and display them as table.
This is my bean method:
public String searchWord;
public List<Product> searchList;
public List<Product> getSearchList() {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
Query query = session.createQuery("From Product where name LIKE '"+searchWord+"%'");
searchList = query.list();
return searchList;
}
if i set searchWord="Ku" then i get the correct insert and see the datas which starting with "Ku".
Then i tried to reach it from my xhtml page so i can get the "Ku" from user.
This is my xhtml
<?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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Products</title>
</h:head>
<h:body>
<h:form id="id-form" >
<h2><h:outputText value ="List of all products"></h:outputText></h2>
<h:dataTable style="border: 4px solid black;" value = "#{products_controller.searchList}" rows="#{products_controller.searchList.size()}" var = "item" border="1" headerClass="tableHeader" >
<h:column>
<f:facet name="header"> Product ID </f:facet>
<h:outputText value="#{item.p_id}" />
</h:column>
<h:column>
<f:facet name="header"> Product Name </f:facet>
<h:outputText value="#{item.p_name}" />
</h:column>
<h:column>
<f:facet name="header"> Product Class </f:facet>
<h:outputText value="#{item.p_class}" />
</h:column>
<h:column>
<f:facet name="header" > Product price </f:facet>
<h:outputText value="#{item.p_price}" />
</h:column>
<h:column>
<f:facet name="header"> Product Description </f:facet>
<h:outputText value="#{item.p_property}" />
</h:column>
<h:column>
<f:facet name="header"> Product Total </f:facet>
<h:outputText value="#{item.p_total}" />
</h:column>
</h:dataTable>
</h:form>
</h:body>
</html>
how can i use searchWord to update my searchList?
Insert a getter and setter for your searchWord
public String getSearchWord() { return searchWord; }
public void setSearchWord(String val) { searchWord = val; }
and in your xhtml
<h:form id="id-form" >
<h:inputText value="#{products_controller.searchWord}">
<f:ajax event="change" render=":id-form"/>
</h:inputText>
With changing the contents of the input field (when cursor leaves the field), JSF will send an AJAX request which sets the value of searchWord (and possible other input fields in the form).
Because of render= the form will be re-rendered with another searchWord.

Can't call controller function from jsf:action

I'm trying to call the function save in my controller SeminaireControl.java
package be.helha.aemt.control;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import be.helha.aemt.ejb.GestionSeminaireEJB;
import be.helha.aemt.entities.Seminaire;
#SessionScoped
#Named
public class SeminaireControl implements Serializable{
private static final long serialVersionUID = 1L;
#EJB
private GestionSeminaireEJB bean;
public List<Seminaire> getAll()
{
return bean.getAll();
}
public void save(Seminaire seminaire)
{
System.out.println("HELLO");
bean.save(seminaire);
}
}
with the <a></a> in my seminaires.xhtml
. . .
<h:dataTable class="seminaire" value = "#{seminaireControl.getAll()}" var = "seminaire">
<h:column>
<f:facet name="header">Id</f:facet>
<h:form>
#{seminaire.id_student}
</h:form>
</h:column>
<h:column>
<f:facet name="header">Nom</f:facet>
#{studentControl.getNameById(seminaire.id_student)}
</h:column>
<h:column>
<f:facet name="header">Séminaire 1</f:facet>
<h:inputText value="#{seminaire.seminaire1}"/>
</h:column>
<h:column>
<f:facet name="header">Séminaire 2</f:facet>
<h:inputText value="#{seminaire.seminaire2}"/>
</h:column>
<h:column>
<f:facet name="header">Séminaire 3</f:facet>
<h:inputText value="#{seminaire.seminaire3}"/>
</h:column>
<h:column>
<f:facet name="header">Séminaire 4</f:facet>
<h:inputText value="#{seminaire.seminaire4}"/>
</h:column>
<h:column>
<f:facet name="header">Save</f:facet>
<a jsf:action = "#{seminaireControl.save(seminaire)}">MODIFIER</a> // THIS LINE
</h:column>
<h:column>
<f:facet name="header">Total</f:facet>
0
</h:column>
</h:dataTable>
. . .
and for some reason, my code isn't executed (no changes in my database & no "HELLO" in my console)
I think you shold replace
<a jsf:action = "#{seminaireControl.save(seminaire)}">MODIFIER</a>
with
<h:commandLink value="MODIFIER" action="#{seminaireControl.save(seminaire)}">
You may also need take h:dataTable into h:form
<h:form>
<h:dataTable >
..............
..............
</h:dataTable>
<h:form>

Primefaces 4.0 datatable pagination and filter issue

I have datatable, code is below.
<h:form id="listaPoi">
<p:dataTable id="lokacije" var="poi" value="#{poiBacking.listaPoiLokacija}"
rowIndexVar="rowIndex" styleClass="nonsortable" paginator="true" rows="20"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="20,50,100" filteredValue="#{poiBacking.filteredPoi}"
emptyMessage="Nisu pronađeni POI za traženi upit">
<p:column filterBy="grad" filterMatchMode="contains">
<f:facet name="header">
<h:outputText value="Mjesto" />
</f:facet>
<h:outputText value="#{poi.grad}" />
</p:column>
<p:column filterBy="adresa" filterMatchMode="contains">
<f:facet name="header">
<h:outputText value="Adresa" />
</f:facet>
<h:outputText value="#{poi.adresa}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Geolat" />
</f:facet>
<h:outputText value="#{poi.geolat}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Geolon" />
</f:facet>
<h:outputText value="#{poi.geolon}" />
</p:column>
<p:column>
<f:facet name="header">
</f:facet>
<p:commandButton icon="ui-icon-pencil" value="Uredi lokaciju"
actionListener="#{poiBacking.getPois(poi.geolat,poi.geolon)}"
action="#{poiBacking.setLinkUpdate()}"
oncomplete="PF('poiDialog').show()" update=":unos,:poiDialogId,:toolbar" />
</p:column>
</p:dataTable>
</h:form>
Problem is when i click on second or any other page, datatable shows only first record. If i filter, results are the same. In backing bean list for filtered values contains all values but also shows only first.
Lists in PoiBacking:
private List<PoiLokacija> listaPoiLokacija;
private List<PoiLokacija> filteredPoi;
listaPoiLokacija is fill in #PostConstruct method init().
You didn't give us your code in PoiBacking, so I have no idea if there are something wrong in PoiBacking.
However, I tried your code and find every thing work just fine.
Check this:
Page(Deleted oncomplete="PF('poiDialog').show()" update=":unos,:poiDialogId,:toolbar" since I can't find them in your code) :
<h:form id="listaPoi">
<p:dataTable id="lokacije" var="poi" value="#{poiBacking.listaPoiLokacija}"
rowIndexVar="rowIndex" styleClass="nonsortable" paginator="true" rows="3"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="20,50,100" filteredValue="#{poiBacking.filteredPoi}"
emptyMessage="Nisu pronađeni POI za traženi upit">
<p:column filterBy="grad" filterMatchMode="contains">
<f:facet name="header">
<h:outputText value="Mjesto" />
</f:facet>
<h:outputText value="#{poi.grad}" />
</p:column>
<p:column filterBy="adresa" filterMatchMode="contains">
<f:facet name="header">
<h:outputText value="Adresa" />
</f:facet>
<h:outputText value="#{poi.adresa}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Geolat" />
</f:facet>
<h:outputText value="#{poi.geolat}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Geolon" />
</f:facet>
<h:outputText value="#{poi.geolon}" />
</p:column>
<p:column>
<f:facet name="header">
</f:facet>
<p:commandButton icon="ui-icon-pencil" value="Uredi lokaciju"
actionListener="#{poiBacking.getPois(poi.geolat,poi.geolon)}"
action="#{poiBacking.setLinkUpdate()}"
/>
</p:column>
</p:dataTable>
</h:form>
Backing Bean(Set every attributes in PoiLokacija as String since you didn't mention it):
private List<PoiLokacija> listaPoiLokacija=new ArrayList<>();
private List<PoiLokacija> filteredPoi=new ArrayList<>();
#PostConstruct
public void init(){
listaPoiLokacija.add(new PoiLokacija("grad1", "adresa1", "geolat1", "geolon1"));
listaPoiLokacija.add(new PoiLokacija("grad2", "adresa2", "geolat2", "geolon2"));
listaPoiLokacija.add(new PoiLokacija("grad3", "adresa3", "geolat3", "geolon3"));
listaPoiLokacija.add(new PoiLokacija("grad4", "adresa4", "geolat4", "geolon4"));
listaPoiLokacija.add(new PoiLokacija("grad5", "adresa5", "geolat5", "geolon5"));
listaPoiLokacija.add(new PoiLokacija("grad6", "adresa6", "geolat6", "geolon6"));
filteredPoi.addAll(listaPoiLokacija);
}
public void getPois(String geolat,String geolon){
System.out.println(geolat+","+geolon);
}
public void setLinkUpdate(){
System.out.println("setLinkUpdate");
}

Primefaces In cell editing data table event.getNewValue() sends the old value to the bean

I am trying to use the feature of prime faces which allows user to edit the cell data in the table itself.I have followed this link to achieve it:
Primefaces showcase
When I say edit a cell in the table, new values which are entered are not sent to the bean.It still shows the old values only.
My code JSF:
<h:form id="testform">
<p:growl id="messages" showDetail="true" />
<p:outputPanel id="testContainer" deferred="true">
<p:remoteCommand name="oncompleteCellEdit" update="cars messages" />
<p:dataTable id="cars" var="car" value="#{articlesbean.LMatpilotaccess1}" editable="true" editMode="cell" widgetVar="carsTable" update=":cars">
<f:facet name="header">
Matériel du pilotage et accessoires
</f:facet>
<p:ajax event="cellEdit" listener="#{articlesbean.onCellEdit}" oncomplete="oncompleteCellEdit()" update=":testform:messages" />
<p:column headerText="Serie" style="width:25%">
<p:cellEditor >
<f:facet name="output"><h:outputText value="#{car.serie}" /></f:facet>
<f:facet name="input"><p:inputText id="modelInput16" value="#{car.serie}" style="width:96%"/></f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Cap" style="width:25%">
<p:cellEditor >
<f:facet name="output"><h:outputText value="#{car.cap}" /></f:facet>
<f:facet name="input"><p:inputText id="modelInput15" value="#{car.cap}" style="width:96%"/></f:facet>
</p:cellEditor>
</p:column>
</p:dataTable>
</p:outputPanel>
</h:form>
My code bean
#ManagedBean(name="articlesbean")
#ViewScoped
public class ArticlesBean implements Serializable{
#Inject
private ArticlesDAO articleDAO;
private Matpilotaccess1 matpilotaccess1;
public void onCellEdit(CellEditEvent event) {
Object oldValue = event.getOldValue();
Object newValue = event.getNewValue();
System.out.println("/////"+(event.getNewValue());
System.out.println("/////"+(event.getOldValue());
FacesContext context = FacesContext.getCurrentInstance();
Matpilotaccess1 mpltiacc = context.getApplication().evaluateExpressionGet(context, "#{mpltiacc}", Matpilotaccess1.class);
articleDAO.Updatetable(mpltiacc);
}
/////////Getters and Setters
}
and as result of
System.out.println("/////"+(event.getNewValue());
System.out.println("/////"+(event.getOldValue());
i got in both case the old value!!!
Could anyone let me know how I can get the new values???I am using Jboss 7 and primefaces4 Thanks
You need to annotate your bean with #SessionScoped annotation.

Why i can't refresh my dataTable, after adding data?

When i drag my panel into my 'dataTable', all works fine(my listener is called, my object is added to my DataBase) but my 'dataTable' is not updated and i need to refresh my page to see it. My xhtml:
<h:form id="formDashboard">
<p:layoutUnit id="gridLeft" position="west" size="250" style="position:absolute;">
<p:fieldset style="padding:0;padding-top:5px;">
<f:facet name="legend" id="legendId">
<p:commandButton icon="ui-icon-plus" actionListener="#{myMemosController.prepareCreate}" value="Add Memo" onclick="addMemo.show()" update=":formAdd:commentAddInput, :formAdd:chooseAddPriority" style="width:30px;height:30px;"/>
</f:facet>
<p:dataGrid id="leftData" var="item" columns="1" value="#{myMemosController.items}" style="border:none;">
<p:panel id="pnl" style="background-color:#{item.priority}}" closable="true">
<p:ajax event="close" listener="#{myMemosController.destroy}"/>
<f:facet name="header">
<h:panelGroup>
<h:outputText value="#{item.name}" styleClass=""/>
<p:commandButton icon="ui-icon-pencil" actionListener="#{myMemosController.prepareEdit}" update=":formEdit:commentEditInput, :formEdit:chooseEditPriority" onclick="editMemo.show();" style="width:19px;height:19px;"/>
</h:panelGroup>
</f:facet>
<h:panelGrid columns="1" style="width:100%">
<h:outputText value="#{item.comments}" styleClass=""/>
</h:panelGrid>
<f:facet name="footer">
<h:outputText value="#{item.date} at #{item.time}" styleClass="footerStyle"/>
</f:facet>
</p:panel>
<p:draggable for="pnl" helper="clone" handle=".ui-panel-titlebar" stack=".ui-panel" zindex="100"/>
</p:dataGrid>
</p:fieldset>
</p:layoutUnit>
<p:layoutUnit position="center" style="position:absolute;">
<p:fieldset id="selectedMemos" legend="Selected Memos" style="margin-top:20px">
<p:outputPanel id="dropArea">
<p:dataTable id="centerData" value="#{dashboardController.items}" var="item">
<p:column style="text-align:center;">
<f:facet name="header">
<h:outputText value="Priority"/>
</f:facet>
<h:outputText value="#{item.priorityname}"/>
</p:column>
<p:column style="text-align:center;">
<f:facet name="header">
<h:outputText value="Name"/>
</f:facet>
<h:outputText value="#{item.name}"/>
</p:column>
<p:column style="text-align:center;">
<f:facet name="header">
<h:outputText value="Comment"/>
</f:facet>
<h:outputText value="#{item.comments}"/>
</p:column>
<p:column style="text-align:center;">
<f:facet name="header">
<h:outputText value="Time"/>
</f:facet>
<h:outputText value="#{item.time}"/>
</p:column>
<p:column style="text-align:center;">
<f:facet name="header">
<h:outputText value="Date"/>
</f:facet>
<h:outputText value="#{item.date}"/>
</p:column>
</p:dataTable>
</p:outputPanel>
</p:fieldset>
<p:droppable for="selectedMemos" tolerance="touch" activeStyleClass="ui-state-highlight" >
<p:ajax listener="#{myMemosController.onMemoDrop}" process="#this" update="dropArea, leftData"/>
</p:droppable>
</p:layoutUnit>
</h:form>
My method onMemoDrop:
public void onMemoDrop(DragDropEvent ddEvent)
{
String[] idTokens = ddEvent.getDragId().split(String.valueOf(UINamingContainer.getSeparatorChar(FacesContext.getCurrentInstance())));
int rowIndex = Integer.parseInt(idTokens[idTokens.length - 2]);
String name = idTokens[idTokens.length - 3];
MyMemos memo = null;
if (name.equals("leftData"))
{
items.setRowIndex(rowIndex);
memo = (MyMemos) items.getRowData();
dashboardController.create(1, memo.getName(), memo.getComments(), memo.getDate(), memo.getTime(), memo.getPriority(), memo.getPriorityname());
}
}
and the create method:
public String create(int id, String name, String comment, String date, String time, String priority, String priorityName)
{
try
{
System.out.println("I am in Create() method from DashboardController");
current = new Dashboard(id, name, comment, date, time, priority, priorityName);
getFacade().create(current);
JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/resources/Bundle").getString("DashboardCreated"));
return prepareCreate();
} catch (Exception e) {
JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/resources/Bundle").getString("PersistenceErrorOccured"));
return null;
}
}
May be a suggestion?
I think that this code is not working :
<p:ajax listener="#{myMemosController.onMemoDrop}" process="#this" update="dropArea, leftData"/>
, because this p:ajax cannot resolve the 'dropArea'. Ultimately, the component cannot be found and is not getting updated.
One solution to point to you datatable is set update as :
update=":#{p:component('centerData')}, :#{p:component('leftData')}"
Or maybe use widget vars
The problem was from my managed bean "DashboardController", when i removed #stateless and added an injection like: #ManagedBean(name = "dashboardController") and in MyMemosController i added: #ManagedProperty("value="#{dashboardController}""). Now my dataTable is correctly update.

Categories

Resources