我正在尝试为我的 portlet 创建一个搜索表单。portlet 是一个地址簿应用程序,所有 dao 和服务都使用服务构建器构建。我想为用户提供一个基本/高级搜索表单(就像 liferay 上的其他表单一样,例如控制中心的“用户和组织”。
我已经实现了查看 liferay 源代码(6.1 GA1)的所有逻辑和页面,但是搜索表单没有以任何方式显示,我将把代码放在这里。
在 view.jsp 中:
<%
PortletURL portletURL = renderResponse.createRenderURL();
portletURL.setParameter("jspPage", "/html/addressbookportlet/view.jsp");
pageContext.setAttribute("portletURL", portletURL);
String portletURLString = portletURL.toString();
%>
<aui:form action="<%= portletURLString %>" method="get" name="fm">
<liferay-portlet:renderURLParams varImpl="portletURL" />
<aui:input name="isSearch" type="hidden" value="true" />
<aui:input name="redirect" type="hidden" value="<%= portletURLString %>" />
<liferay-ui:search-container
searchContainer="<%= new ABContactSearch(renderRequest, portletURL) %>"
>
<%
ABContactDisplayTerms displayTerms = (ABContactDisplayTerms)searchContainer.getDisplayTerms();
ABContactSearchTerms searchTerms = (ABContactSearchTerms)searchContainer.getSearchTerms();
Long societyId = GetterUtil.getLong(searchTerms.getSocietyId(),0);
Long contactTypeId = GetterUtil.getLong(searchTerms.getContactTypeId(), 0);
Long brandId = GetterUtil.getLong(searchTerms.getBrandId(),0);
Long channelId = GetterUtil.getLong(searchTerms.getChannelId(),0);
%>
<liferay-ui:search-form
searchContainer="<%=searchContainer%>"
servletContext="<%= this.getServletConfig().getServletContext() %>"
showAddButton="true"
page='<%= request.getContextPath() + "/html/addressbookportlet/contact_search_form.jsp" %>'
/>
<liferay-ui:search-container-results>
<%
if (searchTerms.isAdvancedSearch()) {
results = AddressbookSearchUtil.searchAdvanced(scopes, searchTerms, searchTerms.isAndOperator(), searchContainer.getStart(), searchContainer.getEnd()); //, searchContainer.getOrderByComparator());
total = AddressbookSearchUtil.countAdvanced(scopes, searchTerms, searchTerms.isAndOperator());
}
else {
results = AddressbookSearchUtil.searchFullText(scopes, searchTerms.getKeywords(), searchContainer.getStart(), searchContainer.getEnd()); //, searchContainer.getOrderByComparator());
total = AddressbookSearchUtil.countFullText(scopes, searchTerms.getKeywords());
}
pageContext.setAttribute("results", results);
pageContext.setAttribute("total", total);
%>
</liferay-ui:search-container-results>
<liferay-ui:search-container-row
className="it.mir4unicomm.addressbook.model.ABContact"
escapedModel="<%= true %>"
keyProperty="contactId"
modelVar="abcontact"
>
<liferay-ui:search-container-column-text
title="Surname"
property="surname"
/>
<liferay-ui:search-container-column-text
title="Name"
property="name"
/>
<liferay-ui:search-container-column-text
title="Position"
property="position"
/>
</liferay-ui:search-container-row>
<liferay-ui:search-iterator />
</liferay-ui:search-container>
</aui:form>
contact_search_form.jsp:
<%@ include file="/html/addressbookportlet/init.jsp" %>
<%
the
ABContactDisplayTerms displayTerms = (ABContactDisplayTerms)searchContainer.getDisplayTerms();
List<ABSociety> societyList = ABSocietyLocalServiceUtil.getABSocieties(0, Integer.MAX_VALUE);
List<ABBrand> brandList = ABBrandLocalServiceUtil.getABBrands(0, Integer.MAX_VALUE);
List<ABContactType> contactTypeList = ABContactTypeLocalServiceUtil.getABContactTypes(0, Integer.MAX_VALUE);
List<ABChannel> channelList = ABChannelLocalServiceUtil.getABChannels(0, Integer.MAX_VALUE);
%>
<liferay-ui:search-toggle
id="toggle_id_contact_search"
displayTerms="<%= displayTerms %>"
buttonLabel="search-contact"
>
<aui:fieldset>
<aui:input name="<%= ABContactDisplayTerms.SURNAME %>" size="20" value="<%= displayTerms.getSurname() %>" />
<aui:input name="<%= ABContactDisplayTerms.NAME %>" size="20" value="<%= displayTerms.getName() %>" />
<aui:input name="<%= ABContactDisplayTerms.POSITION %>" size="20" value="<%= displayTerms.getPosition() %>" />
<aui:input name="<%= ABContactDisplayTerms.DETAIL_VALUE %>" size="20" value="<%= displayTerms.getDetailValue() %>" />
</aui:fieldset>
</liferay-ui:search-toggle>
<c:if test="<%= windowState.equals(WindowState.MAXIMIZED) %>">
<aui:script>
Liferay.Util.focusFormField(document.<portlet:namespace />fm.<portlet:namespace /><%= ABContactDisplayTerms.SURNAME %>);
Liferay.Util.focusFormField(document.<portlet:namespace />fm.<portlet:namespace /><%= ABContactDisplayTerms.KEYWORDS %>);
</aui:script>
</c:if>meDisplay.setIncludeServiceJs(true);
ABContactSearch searchContainer = (ABContactSearch)request.getAttribute("liferay-ui:search:searchContainer");
ABContactDisplayTerm.java:
public class ABContactDisplayTerms extends DisplayTerms {
public static final String NAME = "name";
public static final String SURNAME = "surname";
public static final String POSITION = "position";
public static final String SOCIETY_ID = "societyId";
public static final String CONTACT_TYPE_ID = "contactTypeId";
public static final String BRAND_ID = "brandId";
public static final String CHANNEL_ID = "channelId";
public static final String DETAIL_VALUE = "detailValue";
protected String name;
protected String surname;
protected String position;
protected Long societyId;
protected Long contactTypeId;
protected Long brandId;
protected Long channelId;
protected String detailValue;
public ABContactDisplayTerms(PortletRequest portletRequest) {
super(portletRequest);
name = ParamUtil.getString(portletRequest, NAME);
surname = ParamUtil.getString(portletRequest, SURNAME);
position = ParamUtil.getString(portletRequest, POSITION);
societyId = ParamUtil.getLong(portletRequest, SOCIETY_ID);
contactTypeId = ParamUtil.getLong(portletRequest, CONTACT_TYPE_ID);
brandId = ParamUtil.getLong(portletRequest, BRAND_ID);
channelId = ParamUtil.getLong(portletRequest, CHANNEL_ID);
detailValue = ParamUtil.getString(portletRequest, DETAIL_VALUE);
}
public String getName() {
return name;
}
public String getSurname() {
return surname;
}
public String getPosition() {
return position;
}
public Long getSocietyId() {
return societyId;
}
public Long getContactTypeId() {
return contactTypeId;
}
public Long getBrandId() {
return brandId;
}
public Long getChannelId() {
return channelId;
}
public String getDetailValue() {
return detailValue;
}
}
最后是 ABContactSearch.java
public class ABContactSearch extends SearchContainer<ABContact> {
private static Log _log = LogFactoryUtil.getLog(ABContactSearch.class);
static List<String> headerNames = new ArrayList<String>();
static Map<String, String> orderableHeaders = new HashMap<String, String>();
static {
headerNames.add("name");
headerNames.add("surname");
headerNames.add("position");
headerNames.add("society");
headerNames.add("contact-type");
headerNames.add("channel");
headerNames.add("brand");
headerNames.add("detail-value");
orderableHeaders.put("name", "name");
orderableHeaders.put("surname", "surname");
orderableHeaders.put("society", "society");
orderableHeaders.put("contact-type", "contact-type");
}
public static final String EMPTY_RESULTS_MESSAGE = "no-contacts-were-found";
public ABContactSearch(PortletRequest portletRequest, PortletURL iteratorURL) {
super(
portletRequest, new ABContactDisplayTerms(portletRequest),
new ABContactSearchTerms(portletRequest), DEFAULT_CUR_PARAM,
DEFAULT_DELTA, iteratorURL, headerNames, EMPTY_RESULTS_MESSAGE);
ABContactDisplayTerms displayTerms = (ABContactDisplayTerms)getDisplayTerms();
iteratorURL.setParameter(
ABContactDisplayTerms.NAME, displayTerms.getName());
iteratorURL.setParameter(
ABContactDisplayTerms.SURNAME, displayTerms.getSurname());
iteratorURL.setParameter(
ABContactDisplayTerms.POSITION, displayTerms.getPosition());
iteratorURL.setParameter(
ABContactDisplayTerms.DETAIL_VALUE, displayTerms.getDetailValue());
iteratorURL.setParameter(
ABContactDisplayTerms.SOCIETY_ID, String.valueOf(displayTerms.getSocietyId()));
iteratorURL.setParameter(
ABContactDisplayTerms.CONTACT_TYPE_ID, String.valueOf(displayTerms.getContactTypeId()));
iteratorURL.setParameter(
ABContactDisplayTerms.BRAND_ID, String.valueOf(displayTerms.getBrandId()));
iteratorURL.setParameter(
ABContactDisplayTerms.CHANNEL_ID, String.valueOf(displayTerms.getChannelId()));
try {
String orderByCol = ParamUtil.getString(
portletRequest, "orderByCol", "surname");
String orderByType = ParamUtil.getString(
portletRequest, "orderByType", "asc");
// OrderByComparator orderByComparator =
// UsersAdminUtil.getUserOrderByComparator(
// orderByCol, orderByType);
setOrderableHeaders(orderableHeaders);
setOrderByCol(orderByCol);
setOrderByType(orderByType);
// setOrderByComparator(orderByComparator);
}
catch (Exception e) {
_log.error(e.getMessage());
_log.debug(e.getMessage(), e);
}
}
}
搜索容器本身运行良好,因为默认情况下使用空字符串执行基本搜索,但未显示“搜索表单”。我尝试在contact_search_form.jsp 上放置一些调试消息,但它们都没有打印到控制台中。taglib 似乎没有找到或处理该文件。
任何帮助,将不胜感激!