1.查询功能
@Stateful
@Name("hotelSearch")
@Scope(ScopeType.SESSION)
@LoggedIn
public class HotelSearchingAction implements HotelSearching
{
@PersistenceContext
private EntityManager em;
private String searchString;
private int pageSize = 10;
private int page;
@DataModel
private List<Hotel> hotels;
public void find()
{
page = 0;
queryHotels();
}
public void nextPage()
{
page++;
queryHotels();
}
private void queryHotels()
{
hotels = em.createQuery("select h from Hotel h where lower(h.name) " +
"like #{pattern} or lower(h.city) like #{pattern} " +
"or lower(h.zip) like #{pattern} or " +
"lower(h.address) like #{pattern}")
.setMaxResults(pageSize)
.setFirstResult( page * pageSize )
.getResultList();
}
public boolean isNextPageAvailable()
{
return hotels!=null && hotels.size()==pageSize;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
@Factory(value="pattern", scope=ScopeType.EVENT)
public String getSearchPattern()
{
return searchString==null ?
"%" : '%' + searchString.toLowerCase().replace('*', '%') + '%';
}
public String getSearchString()
{
return searchString;
}
public void setSearchString(String searchString)
{
this.searchString = searchString;
}
@Remove
public void destroy() {}
}
网页:
<h:form id="searchCriteria">
<fieldset>
<h:inputText id="searchString" value="#{hotelSearch.searchString}" style="width: 165px;">
<a:ajax event="keyup" render="searchResults" listener="#{hotelSearch.find}"/>
</h:inputText>
 
<a:commandButton id="findHotels" value="Find Hotels" actionListener="#{hotelSearch.find}" render="searchResults"/>
 
<a:status id="status">
<f:facet id="StartStatus" name="start">
<h:graphicImage id="SpinnerGif" value="/img/spinner.gif"/>
</f:facet>
</a:status>
<br/>
<h:outputLabel id="MaximumResultsLabel" for="pageSize">Maximum results:</h:outputLabel> 
<h:selectOneMenu id="pageSize" value="#{hotelSearch.pageSize}">
<f:selectItem id="PageSize5" itemLabel="5" itemValue="5"/>
<f:selectItem id="PageSize10" itemLabel="10" itemValue="10"/>
<f:selectItem id="PageSize20" itemLabel="20" itemValue="20"/>
</h:selectOneMenu>
</fieldset>
</h:form>
</div>
<a:outputPanel id="searchResults">
<div class="section">
<h:outputText id="NoHotelsFoundMessage" value="No Hotels Found" rendered="#{hotels != null and hotels.rowCount==0}"/>
<h:dataTable id="hotels" value="#{hotels}" var="hot" rendered="#{hotels.rowCount>0}">
<h:column id="column1">
<f:facet id="NameFacet" name="header">Name</f:facet>
#{hot.name}
</h:column>
<h:column id="column2">
<f:facet id="AddressFacet" name="header">Address</f:facet>
#{hot.address}
</h:column>
<h:column id="column3">
<f:facet id="CityStateFacet" name="header">City, State</f:facet>
#{hot.city}, #{hot.state}, #{hot.country}
</h:column>
<h:column id="column4">
<f:facet id="ZipFacet" name="header">Zip</f:facet>
#{hot.zip}
</h:column>
<h:column id="column5">
<f:facet id="ActionFacet" name="header">Action</f:facet>
<s:link id="viewHotel" value="View Hotel" action="#{hotelBooking.selectHotel(hot)}"/>
</h:column>
</h:dataTable>
<s:link id="MoreResultsLink" value="More results" action="#{hotelSearch.nextPage}" rendered="#{hotelSearch.nextPageAvailable}"/>
</div>
</a:outputPanel>