我有一个奇怪的问题,我确定它与h:form放置标签有关。我有一个表格,在表格上方我有一个搜索表单,我可以在其中放置一些标签,例如姓名或姓氏,之后该表格将被刷新。它奏效了!但由于某些原因,它只是停止工作,我不知道为什么。现在为了检查搜索结果,我必须刷新页面或将表中的分页从 10 更改为 15,之后将出现结果。下面是一些代码:
.xhtml:
<h:form>
<div class="row">
<div class="panel-heading text-center">
<div class="bootstrap-filestyle input-group inn">
<div class="search-criteria" style="width: 500px">
<h:inputText value="#{clientBean.tags}" styleClass="form-control"
type="text">
<f:passThroughAttribute name="placeholder"
value="Imię, nazwisko, adres..." />
</h:inputText>
</div>
<p:commandButton type="submit" style="float:left"
styleClass="btn btn-primary" value="Szukaj"
actionListener="#{clientBean.getAllClients()}">
<i class="icon-search icon-white"></i>
</p:commandButton>
</div>
</div>
</div>
<h:form>
<p:dataTable id="clientsTable" style="white-space: nowrap"
var="client" value="#{clientBean.getAllClients()}" paginator="true"
rows="15"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="10,15">
<p:column headerText="Imię">
<h:outputText value="#{client.name}" />
</p:column>
<p:column headerText="Nazwisko">
<h:outputText value="#{client.lastName}" />
</p:column>
<p:column headerText="Numer telefonu">
<h:outputText value="#{client.phoneNumber}" />
</p:column>
<p:column headerText="Adres">
<h:outputText value="#{client.address}" />
</p:column>
<p:column>
<a href="klienci/#{client.ID}"
class="btn btn-success edit resized-font"><span
class="glyphicon glyphicon-pencil"></span> Edytuj</a>
<a href="klienci/#{client.ID}"
class="btn btn-danger delete resized-font"><span
class="glyphicon glyphicon-trash"></span> Usuń</a>
<a href="klienci/#{client.ID}" class="btn btn-primary resized-font"><span
class="glyphicon glyphicon-book"></span> Informacje</a>
</p:column>
</p:dataTable>
</h:form>
</h:form>
来自客户端Bean的重要代码:
private String tags;
public Set<Client> getAllClients() {
if (tags == null) {
Set<Client> clients = new HashSet<Client>(clientDao.findAll());
return clients;
}
return getClients();
}
public Set<Client> getClients() {
Set<Client> mergedClientSet = new HashSet<>();
String[] tags = getTags().split(" ");
for(int i=0; i<tags.length; i++){
mergedClientSet.addAll(searchService.getClientWithParameters(tags[i]));
}
return mergedClientSet;
}
public String getTags() {
return tags;
}
public void setTags(String tags) {
this.tags = tags;
}
首先,你违反了W3C XHTML规范,你正在嵌套表单,你不能这样做。
而你要实现的目标相当简单。
这是你怎么做的:
JSF Facelet:
<h:form prependId="true" id="main-form">
<div class="row">
<div class="panel-heading text-center">
<div class="bootstrap-filestyle input-group inn">
<div class="search-criteria"
style="width: 500px">
<h:inputText value="#{clientBean.tags}"
styleClass="form-control">
<f:passThroughAttribute name="placeholder"
value="Imię, nazwisko, adres..." />
</h:inputText>
</div>
<p:commandButton style="float:left"
styleClass="btn btn-primary" value="Szukaj"
actionListener="#{clientBean.doTagsSearch}"
update=":main-form:clients-table">
<i class="icon-search icon-white"></i>
</p:commandButton>
</div>
</div>
</div>
<p:dataTable id="clients-table"
style="white-space: nowrap"
var="client"
value="#{clientBean.clients}"
paginator="true"
rows="15"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="10,15">
<p:column headerText="Imię">
<h:outputText value="#{client.name}" />
</p:column>
<p:column headerText="Nazwisko">
<h:outputText value="#{client.lastName}" />
</p:column>
<p:column headerText="Numer telefonu">
<h:outputText value="#{client.phoneNumber}" />
</p:column>
<p:column headerText="Adres">
<h:outputText value="#{client.address}" />
</p:column>
<p:column>
<a href="klienci/#{client.ID}"
class="btn btn-success edit resized-font"><span
class="glyphicon glyphicon-pencil"></span> Edytuj</a>
<a href="klienci/#{client.ID}"
class="btn btn-danger delete resized-font"><span
class="glyphicon glyphicon-trash"></span> Usuń</a>
<a href="klienci/#{client.ID}" class="btn btn-primary resized-font"><span
class="glyphicon glyphicon-book"></span> Informacje</a>
</p:column>
</p:dataTable>
</h:form>
托管Bean代码片段:
private String tags;
private ArrayList<Client> clients;
@PostConstruct
public void init() {
clients = clientDao.findAll();//must return an ArrayList of Client
}
public void doTagsSearch() {
if (tags == null) {
clients = clientDao.findAll();
} else {
clients = getClientsByTags();
}
}
public ArrayList<Client> getClientsByTags() {
//use your tags logic.
//must return an ArrayList of Clients.
...
}
public String getTags() {
return tags;
}
public void setTags(String tags) {
this.tags = tags;
}
public String getClients() {
return clients;
}
public void setClients(ArrayList<Client> clients) {
this.clients = clients;
}
}