This question already has answers here:
commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated
(12 answers)
Closed 1 year ago.
I have a project and I decided to make it look better. I learned how to use bootstrap and then tried on some of my pages. Although doing the same things on xhtml pages, some of my pages' buttons does not work correctly. Here is an example from my code
my add function which is being used from interface:
#Override
public void add(Object object) {
Session session = factory.openSession();
session.beginTransaction();
session.save(object);
session.getTransaction().commit();
session.close();
}
and i use it like this in my registerpage:
<?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">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
template="/template/site.xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<ui:define name="content">
<h:form>
<style>
.error{color:red}
</style>
<div class="form-group">
<label for="inputEmail4">Product name</label>
<h:inputText class="form-control" value="#{product_bean.p_name}"
id="name"
required="true"
requiredMessage="Product name can't be empty!"
>
<f:validator validatorId="only_letter_validator"/>
</h:inputText>
<h:message for="name" styleClass="error"/> <br></br>
</div>
<h:commandButton value="Save" action="#{products_controller.add(product_bean)}">
<f:actionListener binding="#{products_controller.setFactory()}"/>
</h:commandButton>
</h:form>
</ui:define>
</ui:composition>
my command button is working fine.
here is my delete method:
#Override
public void delete(int id) {
Session session = factory.openSession();
session.beginTransaction();
session.createQuery("delete from Product where id=" + id).executeUpdate();
session.getTransaction().commit();
session.close();
}
and this is how I use it:
<?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">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
template="/template/site.xhtml">
<ui:define name="content">
<h2><h:outputText value ="Product List"></h:outputText></h2>
<h:dataTable styleClass="table table-striped" value = "#{products_controller.productsList}" rows="#{products_controller.productsList.size()}" var = "item" border="1" headerClass="tableHeader" >
<h:column>
<f:facet name="header"> ProductID </f:facet>
<h:outputText value="#{item.p_id}" />
</h:column>
<h:column>
<f:facet name="header"> Productname </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" > Productprice </f:facet>
<h:outputText value="#{item.p_price}" />
</h:column>
<h:column>
<f:facet name="header"> ProductPP </f:facet>
<h:outputText value="#{item.p_property}" />
</h:column>
<h:column>
<f:facet name="header"> ProductTotal </f:facet>
<h:outputText value="#{item.p_total}" />
</h:column>
</h:dataTable>
<br></br><br></br>
<div>
Ürün ID Numarası <h:inputText value="#{deleteID}"
id="id"
required="true"
requiredMessage="Can not be empty!"
validator="#{inputValidators.OnlyLetter}"/>
<h:message for="id" styleClass="error"/>
<h:commandButton value="Delete" action="#{products_controller.delete(deleteID)}">
<f:actionListener binding="#{products_controller.setFactory()}"/>
</h:commandButton>
</div>
</ui:define>
</ui:composition>
actually I am thinking they are same and it was working before I started to use bootstrap. one is working but other is not. what is different or what should I change? I traced it but could not figure out anything.
i found the solution and saw there are many people strugling with this problem. so i am sharing.
if your commandButtons are not in a <h:form> BUTTONHERE <h:form> they don't work. You can take whole table or just buttons between form parts, they will work.
Related
This question already has answers here:
commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated
(12 answers)
Closed 5 years ago.
I know there are similiar questions here but none of the solutions worked for me. Before I used primeface datatable every thing was working perfectly but I needed paginator of primefaces so I switched to primeface's datatable instead of default jsf datatable. The problem is I have a column named action where somebuttons will be placed. For instance one of the button is "Buy". When I click this button the program will take from product database and add it to cart database. This is working perfectly with jsf datatable. But in primefaces datatable it's either not doing anything or gives null point exception. This is my xhtml file. It gives null point exception if I click buy button for the first item in the datatable if I click for anything else it's not doing anything.
<?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:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Shopping</title>
<h:outputStylesheet library="css" name="Product-table.css" />
</h:head>
<body>
<h:outputLabel value="Welcome #{loginBean.name}"></h:outputLabel>
<br></br>
<br></br>
<div style="width: 100%;">
<div style="width: 75%; float: left;">
<h2>Products</h2>
<h:form>
<p:dataTable var="product" value="#{databaseBean.products}"
filteredValue="#{databaseBean.filteredProducts}"
widgetVar="productWidget" paginator="true" rows="10"
paginatorTemplate="{CurrentPageReport} {FirstPageLink}
{PreviousPageLink} {PageLinks} {NextPageLink}
{LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="5,10,15,20,25">
<f:facet name="header">
<p:outputPanel>
<h:outputText value="Search:" />
<p:inputText id="globalFilter"
onkeyup="PF('productWidget').filter()" style="width:150px"
placeholder="Search text" />
</p:outputPanel>
</f:facet>
<p:column headerText="Id" filterBy="#{product.id}" filterMatchMode="contains">
<h:outputText value="#{product.id}" />
</p:column>
<p:column headerText="Name" filterBy="#{product.name}" filterMatchMode="contains" >
<h:outputText value="#{product.name}" />
</p:column>
<p:column headerText="Price" filterBy="#{product.price}" filterMatchMode="contains">
<h:outputText value="#{product.price}" />
</p:column>
<p:column headerText="Quantity" filterBy="#{product.quantity}" filterMatchMode="contains">
<h:outputText value="#{product.quantity}" />
</p:column>
<p:column headerText="Category" filterBy="#{product.category}" filterMatchMode="contains" >
<h:outputText value="#{product.category}" />
</p:column>
<p:column>
<f:facet name="header">Action</f:facet>
<h:form>
<h:commandButton type="submit" value="#{product.id}"
name="buyLink" action="#{databaseBean.addtoCart}">
<f:param name="productId" value="#{product.id}" />
<f:param name="userId" value="#{loginBean.name}" />
</h:commandButton>
</h:form>
</p:column>
</p:dataTable>
</h:form>
</div>
<div style="width: 25%; float: right;">
<h2>Cart</h2>
<p:dataTable value="#{databaseBean.cart}" var="product"
styleClass="product-table" headerClass="product-table-header"
rowClasses="product-table-odd-row,product-table-even-row">
<p:column>
<f:facet name="header">Product ID</f:facet>
#{product.productId}
</p:column>
<p:column>
<f:facet name="header">Quantity</f:facet>
#{product.quantity}
</p:column>
</p:dataTable>
</div>
</div>
<h:form>
<h:commandButton action="#{loginBean.logout}" value="logout"></h:commandButton>
</h:form>
This is the method that the button should call. As I said it's working perfectly in default jsf datatable so I do not think anything is wrong with the java code but the problem is in xhtml file.
public void addtoCart(){
//userId = getUserID();
ManageCart MC = new ManageCart();
FacesContext fc = FacesContext.getCurrentInstance();
Map<String,String> params =
fc.getExternalContext().getRequestParameterMap();
String productIdString = params.get("productId");
int productId = Integer.parseInt(productIdString);
MC.addToChart(userId, productId, 1);
cart = mc.listCart(userId);
products = mp.listProducts();
}
I would really appreciate any help.
Thank you.
I found the solution. I needed to delete tag <h:form></h:form> from the code block of action column and it works.
This question already has answers here:
commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated
(12 answers)
Closed 6 years ago.
I am making an application using jsf and I have a problem when sending data from one of the rows of the datatable to a bean and then redirect to another page. Just to state, I'm using bootsfaces. Here are the codes:
JSF page:
<?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">
<ui:composition template="/templates/layout.xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:b="http://bootsfaces.net/ui"
xmlns:f="http://java.sun.com/jsf/core">
<ui:define name="title">Home</ui:define>
<ui:define name="content">
<b:dataTable value="#{LeiloesMB.lista()}" var="l">
<b:column>
<f:facet name="header">
<h:outputText value="Leilao"/>
</f:facet>
<h:outputText value="#{l.leilao.id}"/>
</b:column>
<b:column>
<f:facet name="header">
<h:outputText value="Encerramento"/>
</f:facet>
<h:outputText value="#{l.leilao.encerramento}"/>
</b:column>
<b:column>
<f:facet name="header">
<h:outputText value="Item"/>
</f:facet>
<h:outputText value="#{l.item.nome}"/>
</b:column>
<b:column>
<f:facet name="header">
<h:outputText value="Descrição"/>
</f:facet>
<h:outputText value="#{l.item.descricao}"/>
</b:column>
<b:column>
<f:facet name="header">
<h:outputText value="Maior Lance"/>
</f:facet>
<h:outputText value="#{l.valor}"/>
</b:column>
<b:column>
<f:facet name="header">
<h:outputLabel value="ID"/>
</f:facet>
<h:outputLabel value="#{l.usuario.id}"/>
</b:column>
<b:column>
<f:facet name="header">
<h:outputLabel value="Nome"/>
</f:facet>
<h:outputLabel value="#{l.usuario.nome}"/>
</b:column>
<b:column rendered="#{not empty Usuario.user.nome}">
<f:facet name="header">
<h:outputLabel value="Realizar Lance"/>
</f:facet>
<h:form>
<h:commandButton value="Faça seu lance" action="#{LanceMB.lance(Usuario.user, l)}"/>
</h:form>
</b:column>
</b:dataTable>
</ui:define>
</ui:composition>
LanceMB code:
#SessionScoped
#ManagedBean(name="LanceMB")
public class NovoLance {
private Usuario user = new Usuario();
private LancesLeilao ll = new LancesLeilao();
private double novo_valor;
public String lance(Usuario usuario, LancesLeilao lal){
this.user = usuario;
this.ll = lal;
return "novolance";
}
//getters.. setters
}
Usuario code:
#ManagedBean(name="Usuario")
#SessionScoped
public class UserSession {
private String email, senha;
private Usuario user = new Usuario();
//getters and setters..
}
Any question leave a comment.
A <h:commandButton /> is used to push the values of individual fields in a form to the managed bean defined. I see that you have enclosed just the button within the form but not the values themselves.
Wrap all the values within the form including the button and give it a try. It should work if everything else has been defined correctly.
Hope this helps :)
This question already has an answer here:
How and when should I load the model from database for JSF dataTable
(1 answer)
Closed 6 years ago.
I am new to jsf. I write a simple application to retrive data from the database. I am calling a method in xhtml form but it is giving me the error as follow
Expression must be a value expression but is a method
expression
Here is my code:
<?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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
>
<h:head>
<h:outputStylesheet library="css" name="table-style.css" />
</h:head>
<h:body>
<h1>JSF 2.0 + JDBC Example</h1>
<h:dataTable value="#{member.getMemberList()}" var="mem"
styleClass="order-table"
headerClass="order-table-header"
rowClasses="order-table-odd-row,order-table-even-row"
>
<h:column>
<f:facet name="header">
Customer ID
</f:facet>
#{mem.FirstName}
</h:column>
<h:column>
<f:facet name="header">
Name
</f:facet>
#{mem.LastName}
</h:column>
<h:column>
<f:facet name="header">
Email
</f:facet>
#{mem.Email}
</h:column>
<h:column>
<f:facet name="header">
Password
</f:facet>
#{mem.Password}
</h:column>
</h:dataTable>
</h:body>
</html>
Error at this line
<h:dataTable value="#{member.getMemberList()}" var="mem"
styleClass="order-table"
headerClass="order-table-header"
rowClasses="order-table-odd-row,order-table-even-row"
>
can any on help me to solve this error. I shall be thankful ...... :)
In managed bean member check if property memberList has both getter and setter. Then replace datatable code with:
<h:dataTable value="#{member.memberList}" var="mem"
styleClass="order-table"
headerClass="order-table-header"
rowClasses="order-table-odd-row,order-table-even-row"
>
Hope it helps.
Here is a Facelets file which is used to get first and last name and after that will go to the another Facelets file:
<?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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head><title>User Form</title></h:head>
<h:body>
<h:form>
<h:panelGrid columns="2">
<p:graphicImage value="resources/images/image.jpg" width="50%"/>
<p:panelGrid columns="2" width="50%">
<f:facet name="header">
User Form
</f:facet>
<h:outputLabel for="firstname" value="Firstname: *"/>
<p:inputText id="firstname" value="" label="Firstname"/>
<h:outputLabel for="surname" value="Surname: *"/>
<p:inputText id="surname" value="" required="true" label="Surname"/>
<f:facet name="footer">
<p:commandButton type="button" value="Save" icon="ui-icon-check" style="margin:0" action="construction"/>
</f:facet>
</p:panelGrid>
</h:panelGrid>
</h:form>
</h:body>
</html>
I expected to go to the construction.xhtml when I press the "Save" button, but nothing happens. How is this caused and how can I solve it?
when you are using commandButton you must do something like this:
<p:commandButton value="Save" icon="ui-icon-check" style="margin:0" action="#{someBean.navigate}"/>
it means that you must use managed bean
and in your case you must use button instead of commandButton
I have a jsf datatable in one of my screen where I have take the date input. Hence I want to replace the input text with rich:calendar. Can someone tell me what all changes have to be done for this?
here is my current code:
<?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"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">
<h:head>
<title> Tracker</title>
</h:head>
<h:body>
<h3>list</h3>
<h:form>
<h:dataTable value="#{userMB.entriesRecords}"
var="entryRecord">
<h:column>
<f:facet name="header">Date</f:facet>
<h:inputText value="#{entryRecord.entry.date}"
rendered="#{entryRecord.editable}">
<f:convertDateTime pattern="dd/MM/yyyy" />
</h:inputText>
<h:outputText value="#{entryRecord.entry.date}"
rendered="#{not entryRecord.editable}">
<f:convertDateTime pattern="dd/MM/yyyy" />
</h:outputText>
</h:column>
</h:dataTable>
<h:commandButton value="Save All Changes" action="#{userMB.saveAll}"></h:commandButton>
</h:form>
</h:body>
</html>
Replace h:inputText with
<rich:calendar enableManualInput="true"
id="entryDate"
datePattern="dd/MM/yyyy"
value="#{entryRecord.entry.date}"
label="Entry Date">
<f:convertDateTime pattern="dd/MM/yyyy" />
</rich:calendar>
It was tested with rich:dataTable and rich:column in RichFaces 4.3.