Я новичок на портале liferay.Я разработал один портлет в LifeRay для демонстрации.В этом примере я использовал связь между портлетами.Что я делаю: - У меня есть один поисковый портлет, в котором у меня есть одно текстовое поле для поиска.Когда я нажимаю кнопку поиска, он выбирает данные из базы данных и отображает эти данные, используя поиск, содержащийся в другом портлете.Я использовал аннотации ProcessEvent и ActionEvent для этого проекта.
Теперь я хочу, когда я нажимаю кнопку поиска, страница не должна обновляться (т.е. я хочу использовать концепцию AJAX), и данные должны бытьотображается в другом портлете.
Фрагмент кода
Портлет A - view.jsp
<%@include file="/html/init.jsp"%>
<portlet:defineObjects />
<!--
<portlet:actionURL var="actionURL" name="pitchBall"></portlet:actionURL>
//-->
**Change to Resource URL**
<portlet:resourceURL var="resourceURL">
</portlet:resourceURL>
<aui:form method="POST" action="<%= resourceURL%>" name=" <portlet:namespace>fm1</portlet:namespace>">
<aui:input name="search" id="search" />
<aui:button type="submit" name="Search" value="Search" />
</aui:form>
Портлет A - класс портлета Search
package com.test;
/**
* Portlet implementation class SearchPortlet
*/
public class SearchPortlet extends GenericPortlet {
@Override
public void render(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
// TODO Auto-generated method stub
super.render(request, response);
}
@ProcessAction(name="pitchBall")
public void pitchBall(ActionRequest request, ActionResponse response) throws SystemException {
String name = ParamUtil.getString(request, "search");
QName qName = new QName("http://liferay.com/events", "ipc.pitch");
response.setEvent(qName, name);
}
public void init() {
editJSP = getInitParameter("edit-jsp");
helpJSP = getInitParameter("help-jsp");
viewJSP = getInitParameter("view-jsp");
}
public void doEdit(
RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
include(editJSP, renderRequest, renderResponse);
}
public void doHelp(
RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
include(helpJSP, renderRequest, renderResponse);
}
@Override
public void doView(
RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
//super.doView(renderRequest, renderResponse);
System.out.println("In doView code");
renderResponse.setContentType(renderRequest.getResponseContentType());
// file to display...
String url = "/html/searchportlet/view.jsp";
// read the above file and output it...
getPortletContext().getRequestDispatcher(url).include(renderRequest, renderResponse);
//include(viewJSP, renderRequest, renderResponse);
}
@Override
public void serveResource(ResourceRequest request, ResourceResponse response)
throws PortletException, IOException {
// TODO Auto-generated method stub
//super.serveResource(request, response);
System.out.println("In serveResource code");
response.setContentType("text/html");
String name = request.getParameter("search");
// this seems to be the page that was calling...?
String resourceID = request.getResourceID();
System.out.println("resourceId was : " + resourceID);
System.out.println("message was : " + name);
PrintWriter writer = response.getWriter();
writer.print(name);
}
protected void include(
String path, RenderRequest renderRequest,
RenderResponse renderResponse)
throws IOException, PortletException {
PortletRequestDispatcher portletRequestDispatcher =
getPortletContext().getRequestDispatcher(path);
if (portletRequestDispatcher == null) {
_log.error(path + " is not a valid include");
}
else {
portletRequestDispatcher.include(renderRequest, renderResponse);
}
}
protected String editJSP;
protected String helpJSP;
protected String viewJSP;
private static Log _log = LogFactoryUtil.getLog(SearchPortlet.class);
}
Портлет B - view.jsp
<%@include file="/html/init.jsp"%>
<portlet:defineObjects />
<%
String name = (String)renderRequest.getParameter("name");
%>
<liferay-ui:search-container
emptyResultsMessage="author-empty-results-message">
<liferay-ui:search-container-results
results="<%= KeyurAuthorLocalServiceUtil.getStudentByName(name) %>" />
<liferay-ui:search-container-row className="com.test.model.KeyurAuthor">
<liferay-ui:search-container-column-text name="authorId"
property="authorId" />
<liferay-ui:search-container-column-text name="authorName"
property="authorName" />
<liferay-ui:search-container-column-text name="authorEmail"
property="authorEmail" />
</liferay-ui:search-container-row>
<liferay-ui:search-iterator></liferay-ui:search-iterator>
</liferay-ui:search-container>
Портлет B - класс SearchResultPortlet
/**
* Portlet implementation class SearchResultPortlet
*/
public class SearchResultPortlet extends GenericPortlet {
public void init() {
editJSP = getInitParameter("edit-jsp");
helpJSP = getInitParameter("help-jsp");
viewJSP = getInitParameter("view-jsp");
}
@ProcessEvent(qname="{http://liferay.com/events}ipc.pitch")
public void catchBall(EventRequest request, EventResponse response) {
Event event = request.getEvent();
String name = (String)event.getValue();
response.setRenderParameter("name", name);
}
public void doEdit(
RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
include(editJSP, renderRequest, renderResponse);
}
public void doHelp(
RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
include(helpJSP, renderRequest, renderResponse);
}
public void doView(
RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
include(viewJSP, renderRequest, renderResponse);
}
protected void include(
String path, RenderRequest renderRequest,
RenderResponse renderResponse)
throws IOException, PortletException {
PortletRequestDispatcher portletRequestDispatcher =
getPortletContext().getRequestDispatcher(path);
if (portletRequestDispatcher == null) {
_log.error(path + " is not a valid include");
}
else {
portletRequestDispatcher.include(renderRequest, renderResponse);
}
}
protected String editJSP;
protected String helpJSP;
protected String viewJSP;
private static Log _log = LogFactoryUtil.getLog(SearchResultPortlet.class);
}