I am developing the JSP portlet using plugin sdk.
I want to implement AJAX in this portlet.
I am facing the problem while sending the URL in
jQuery.ajax({
type: "GET",
url: "/changemode.jsp",
data: "modeVal=rahul",
success: function(msg){
alert( "Data Saved: " + msg );
}});
the changemode.jsp is @ same location as the calling JSP.
It doest show any error msg, but the changemode.jsp is not get called.
Please advice.
Thanks in advance.
------------------------------------------------------------------------------------
you need to pass url like that
var url = '<portlet:renderURL windowState="<%= LiferayWindowState.EXCLUSIVE.toString() %>"><portlet:param name="struts_action" value="/ext/search/changemode" /></portlet:renderURL>';
and struts action entry should be in struts-config.xml and tiles-defs.xml
-------------------------------------------------------------------------------------
Use the serveResource() method (javax.portlet.GenericPortlet):
1 public void serveResource(ResourceRequest request, ResourceResponse response)
2 throws PortletException, IOException {
3
4 // Make the parameter "modeVal" available in changeMode.jsp
5 String modeVal = ParamUtil.getString(request, "modeVal");
6 request.setAttribute("modeVal", modeVal);
7
8 // Dispatch to changeMode.jsp
9 PortletRequestDispatcher dispatcher = getPortletContext().getRequestDispatcher(
10 "/WEB-INF/jsp/changeMode.jsp");
11 dispatcher.include(request, response);
12 }
In your jsp, set the url to <portlet:resourceURL/> :
1 jQuery.ajax({
2 type: "GET",
3 url: "<portlet:resourceURL />",
4 data: "modeVal=rahul",
5 success: function(msg){
6 alert( "Data Saved: " + msg );
7 }});
If you need to handle dispatching to more than one page, specify an id like this:
1 <portlet:resourceURL id="changeMode" />
Check the id in serveResource() to decide where to dispatch the request.
1 if( request.getResourceID().equals("changeMode")) {
2 . . .
3 }
-------------------------------------------------------------------------------------
It is really helpful .... now i'm one step ahead ...
I have some question regarding serveResource() method .....
1. should i write a servlet class which extends form GenericServlet and implement serveResource() method.
2. But in that case while calling that servlet from jsp with url <portlet:resourceURL /> what
should my url mapping of <servlet-mapping> in web.xml
3. Is it possible to set session a value in the same servlet and use it later.
4. On which object should i set session. PortletSession or HttpSession.
-------------------------------------------------------------------------------------------
Hi Rahul, good to hear that you're making progress!
Now, for your additional questions ...
1. Extend
javax.portlet.GenericPortlet , example:
1 package com.example.portal.test.ajax;
2
3 import java.io.IOException;
4
5 import javax.portlet.GenericPortlet;
6 import javax.portlet.PortletException;
7 import javax.portlet.PortletRequestDispatcher;
8 import javax.portlet.RenderRequest;
9 import javax.portlet.RenderResponse;
10 import javax.portlet.ResourceRequest;
11 import javax.portlet.ResourceResponse;
12
13 import com.liferay.portal.kernel.util.ParamUtil;
14
15 public class AjaxPortlet extends GenericPortlet {
16
17 @Override
18 public void serveResource(ResourceRequest request, ResourceResponse response)
19 throws PortletException, IOException {
20
21 // Make the parameter "modeVal" available in changeMode.jsp
22 String modeVal = ParamUtil.getString(request, "modeVal");
23 request.setAttribute("modeVal", modeVal);
24
25 // Dispatch to changeMode.jsp
26 PortletRequestDispatcher dispatcher = getPortletContext().getRequestDispatcher(
27 "/WEB-INF/jsp/changeMode.jsp");
28 dispatcher.include(request, response);
29 }
30
31 @Override
32 public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
33 PortletRequestDispatcher dispatcher = getPortletContext().getRequestDispatcher("/view.jsp");
34 dispatcher.include(request, response);
35 }
36
37 }
2. Specify the above
portlet class in
/WEB-INF/portlet.xml :
1 <portlet-class>com.example.portal.test.ajax.AjaxPortlet</portlet-class>
3. and 4. If you're not using servlets in your application, use PortletSession. If you need to share session between servlet and portlet, refer to
this article.
---------------------------------------------------------------------------------
but now i'm confused because i already have an entry in portlet.xml for <portlet-class> so,
can a portlet have more than one <portlet-class> and if yes what property needs to be set.
If not then can you suggest any other way ....
--------------------------------------------------------------------------------
You cannot have more than one entry for <portlet-class> in portlet.xml
Is your existing entry
com.liferay.util.bridges.MVCPortlet ? This class extends LiferayPortlet which extends GenericPortlet. MVCPortlet implements serveResource() like this:
1 public void serveResource(
2 ResourceRequest resourceRequest, ResourceResponse resourceResponse)
3 throws IOException, PortletException {
4
5 String jspPage = resourceRequest.getParameter("jspPage");
6
7 if (jspPage != null) {
8 include(
9 jspPage, resourceRequest, resourceResponse,
10 PortletRequest.RESOURCE_PHASE);
11 }
12 else {
13 super.serveResource(resourceRequest, resourceResponse);
14 }
15 }
If this does not suit your needs, you have to replace the entry in <portlet-class> with your own extension of GenericPortlet (e.g., AjaxPortlet).
--------------------------------------------------------------------------------