按条件搜索说明
文章搜索页面如图所示:
helpContentList组件中的部分代码如下:
@Name("helpContentList")
public class HelpContentList extends EntityQuery<HelpContent> {
private static final String EJBQL = "select helpContent from HelpContent helpContent";
private static final String[] RESTRICTIONS = {
"lower(helpContent.title) like lower(concat(#{helpContentList.helpContent.title},'%'))",
"helpContent.helpCat.id=#{helpContentList.helpContent.helpCat.id}", };
private HelpContent helpContent = new HelpContent();// 主要用来做查询,构造el表达式
public HelpContentList() {
setEjbql(EJBQL);
setRestrictionExpressionStrings(Arrays.asList(RESTRICTIONS));
setMaxResults(10);
setOrder("show_order desc and id desc");
}
}
文章列表的部分代码如下:
<rich:dataTable var="_helpContent" value="#{helpContentList.resultList}">
<h:column>
<f:facet name="header">标题</f:facet>
#{_helpContent.title}
</h:column>
</rich:dataTable>
· resultList列表的获取有几个参数,如:Ejbql、RestrictionExpressionStrings、MaxResults、Order。 在构造函数中通过设置
setEjbql(EJBQL);
setRestrictionExpressionStrings(Arrays.asList(RESTRICTIONS));
setMaxResults(10);
setOrder("show_order desc and id desc");
来获取列表
· EJBQL是固定的
· RESTRICTIONS是条件。
当#{helpContentList.helpContent.title}和#{helpContentList.helpContent.helpCat.id}为空的时候,RESTRICTIONS条件不起作用。当它们不为空时,RESTRICTIONS起作用。
· MaxResults设置列表的分页单位,即一个页面最多显示的文章条数
· Order设置文章的根据什么条件来排序
当第一次打开页面的时候 #{helpContentList.helpContent.title}和#{helpContentList.helpContent.helpCat.id}都为空,所以显示出来的是所有的文章,分页单位是10,按照id和show_order倒序排序
搜索部分的代码如下:
<h:inputText value="#{helpContentList.helpContent.title}" />
<h:selectOneMenu value="#{helpContentList.helpContent.helpCat}">
<s:selectItems value="#{helpCatList.helpLeafCats}" var="_helpCats"
label="#{_helpCats.wrapName.replace(' ',' ')}"
rendered="#{not empty helpCatList.helpLeafCats}"
noSelectionLabel="==请选择==" />
<s:convertEntity></s:convertEntity>
</h:selectOneMenu>
这部分代码中文本框组件绑定了#{helpContentList.helpContent.title},选择框组件绑定了#{helpContentList.helpContent.helpCat},当它们有值的时候,后台的setRestrictionExpressionStrings(Arrays.asList(RESTRICTIONS));
成立。没有值的时候不成立,从而达到根据条件搜索的功能。