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>
Related
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.
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.
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.
I have a PrimeFaces datatable. For each row in this table, I want to allow the user to update/delete the row entry (a user).
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.prime.com.tr/ui">
<h:head>
<link type="text/css" rel="stylesheet" href="themes/bluesky/skin.css" />
</h:head>
<h:body>
<center>
<h:form>
<p:panel id="viewUsersPanel" header="View Users">
<p:dataTable var="user" value="#{uController.users}"
emptyMessage="No Users Found.">
<p:column style="text-align: center;">
<f:facet name="header">
<h:outputText value="Name" />
</f:facet>
<h:outputText value="#{user.name}" />
</p:column>
<p:column style="text-align: center;">
<f:facet name="header">
<h:outputText value="Postal Address" />
</f:facet>
<h:outputText value="#{user.address}" />
</p:column>
<p:column style="text-align: center;">
<f:facet name="header">
<h:outputText value="Phone Number" />
</f:facet>
<h:outputText value="#{user.phone}" />
</p:column>
<p:column style="text-align: center;">
<f:facet name="header">
<h:outputText value="Email Address" />
</f:facet>
<h:outputText value="#{user.email}" />
</p:column>
<p:column style="text-align: center;">
<f:facet name="header">
<h:outputText value="DOB" />
</f:facet>
<h:outputText value="#{user.dob}">
<f:convertDateTime pattern="dd-MMM-yyyy" />
</h:outputText>
</p:column>
<p:column style="text-align: center;">
<f:facet name="header">
<h:outputText value="No. Memberships" />
</f:facet>
<h:outputText value="#{user.numberOfMemberships}" />
</p:column>
<p:column style="text-align: center;">
<f:facet name="header">
<h:outputText value="Actions" />
</f:facet>
<h:commandButton value="Update" action="#{uController.update}" />
<h:commandButton value="Delete" action="#{uController.delete}" />
</p:column>
</p:dataTable>
<h:panelGrid columns="2" cellpadding="2" id="footerPanelGrid"
border="0">
<h:commandButton action="#{uController.home}" value="Home Page" />
</h:panelGrid>
</p:panel>
</h:form>
</center>
</h:body>
</html>
However, neither of the buttons work. Instead they appear to simply refresh the view page. I have ran the app in debug and neither update or delete method is hit.
I suspect this may be due to using h:commandButton within a p:dataTable. However, I have also tried p:commandButton but to no avail.
For reference, here is a snippet of the UserController class:
#ManagedBean(name="uController")
public class UserController extends AbstractController {
private Collection<User> users;
...
public String update() {
System.out.println("Ready for update");
return "update-user";
}
public String delete() {
System.out.println("Ready for delete");
return "delete-user";
}
...
}
I have found the solution using a ViewScoped bean. The article that I came across for this:
Learning JSF2: Managed beans
I changed my h:commandButton to:
<h:commandButton value="Delete" action="#{userList.delete}">
<f:setPropertyActionListener target="#{userList.selectedUser}"
value="#{user}" />
I also re-designed my UserController class and it became UserList:
import java.util.Collection;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
#ManagedBean(name="userList")
#ViewScoped
public class UserList {
private Collection<User> users;
private User selectedUser;
#ManagedProperty(value="#{userService}")
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
#PostConstruct
public void populateList() {
users = userService.getUsers();
}
public void delete() {
// TODO remove from DB
users.remove(selectedUser);
}
public User getSelectedUser() {
return selectedUser;
}
public void setSelectedUser(User selectedUser) {
this.selectedUser = selectedUser;
}
public Collection<User> getUsers() {
return users;
}
}
Thanks for your responses Zenzen.
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.