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?
CodePudding user response:
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.