提问人:user023835 提问时间:9/9/2021 最后编辑:Piotr P. Karwaszuser023835 更新时间:9/10/2021 访问量:352
如何从 xhtml 获取 bean 字符串变量并将其发送到列表方法?
How to reach a bean string variable from xhtml and send it to a list method?
问:
我正在尝试发送一个我从用户那里获得的 inputtext 变量,然后将其发送到我的 bean 页面中的方法,以便可以在我的查询中替换它。我将获得列表并将它们显示为表格。 这是我的豆子方法:
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;
}
如果我设置searchWord=“Ku”,那么我会得到正确的插入并看到以“Ku”开头的数据。 然后我尝试从我的 xhtml 页面访问它,这样我就可以从用户那里获取“Ku”。 这是我的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>
如何使用 searchWord 更新我的 searchList?
答:
0赞
Holger
9/10/2021
#1
为您的 searchWord 插入 getter 和 setter
public String getSearchWord() { return searchWord; }
public void setSearchWord(String val) { searchWord = val; }
并在您的 xhtml 中
<h:form id="id-form" >
<h:inputText value="#{products_controller.searchWord}">
<f:ajax event="change" render=":id-form"/>
</h:inputText>
通过更改输入字段的内容(当光标离开字段时),JSF 将发送一个 AJAX 请求,该请求设置 searchWord(以及表单中可能的其他输入字段)的值。
由于表单将使用另一个 searchWord 重新呈现。render=
评论