How to get parameters from URL in Liferay portlet?
Solution 1
Sure, you can always use the standard HttpServletRequest to retrieve your parameters from. You can get this request by using the PortalUtil class, like in the following example:
HttpServletRequest request = PortalUtil.getHttpServletRequest(portletRequest);
String articleId = request.getParameter("articleId");
Solution 2
In my Liferay- JSP I use this:
<!-- ... some imports... -->
<!-- ... some more imports... -->
<%@ page import="com.liferay.portal.util.PortalUtil" %>
<portlet:defineObjects /> <!-- Liferay-specific, defines renderRequest etc.-->
<%
HttpServletRequest r = PortalUtil.getHttpServletRequest(renderRequest);
String wellHole = PortalUtil.getOriginalServletRequest(r).getParameter("well_hole");
%>
(this combines fragments of wisdom from other answers, notably Miguel Gil Martínez's answer from 2012 Jul 23 at 17:35
Solution 3
It worked for me:
public void doView(RenderRequest request, RenderResponse response) throws
PortletException, IOException {
HttpServletRequest requestInsideThePortlet = PortalUtil.getHttpServletRequest(request);
String myURLParam =
PortalUtil.getOriginalServletRequest(requestInsideThePortlet).getParameter("myURLParam");
....
....
....
}
Solution 4
When working with portlets, each HttpServletRequest paramenter has a prefix which informs the "type" of the parameter and a suffix expressing which instance of the portlet should process it. So, the name of the parameters is not just "articleId". I do not know what portlet are you working but if it was a portlet called, let's say, "example" deployed through a war the parameter would be called example_WAR_exampleportletwar_articleId_w2Xd
.
However, you do not have to deal with such complexity. If you are working within a JSP of some already created portlet, there should be an object called renderRequest
which contains all parameters and abstracts all this name mangling. So, instead of retrieving the original servlet requestion you an use it:
String articleId = renderRequest.getParamenter("articleId");
If this object is not defined, you just need to insert the <portlet:defineObjects/>
tag somewhere and after this the object will be available.
HTH. Let us know if it worked!
Solution 5
For JSF I use this:
@ManagedBean
@RequestScoped
public class OriginalRequest {
private HttpServletRequest getOriginalRequest() {
return PortalUtil.getOriginalServletRequest(
PortalUtil.getHttpServletRequest(
(PortletRequest) FacesContext.getCurrentInstance()
.getExternalContext().getRequest() ) );
}
public String getParam( String name ) {
return getOriginalRequest().getParameter( name );
}
public List<String> getParamNames() {
return Collections.list( getOriginalRequest().getParameterNames() );
}
}
Then in your facelet:
#{originalRequest.getParam('my_param')}
Mayur Patel
Updated on November 21, 2020Comments
-
Mayur Patel over 3 years
I'm using jsp of out-of-box portlet like feed.jspf in Liferay 6:
String articleId =null; HttpServletRequest httpReq = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(renderRequest)); articleId = httpReq.getParameter("articleId");
It is giving a null value whether in custom portlet or in .jsp files, but it should have a value.
-
Mayur Patel over 13 yearsya that is right but i want to get articleId from url ...like localhost:8080/author-detail?articleId=12345...can i get that in portlet ..jsp page???
-
brandizzi over 13 yearsSo, does the URL parameter have no prefix or suffix, right? I do not know how to get it (I'd suggest to do what you tried anyway, hoping it works) but I wonder how this paramenter is set. How is it defined? Is it in some portlet/hook made by you?
-
Mayur Patel over 13 yearsFrom one of my custom portlet in liferay ...i'm calling other page by defining the name of the page and passing parameters manually...after it will redirect to author-detail page..in that i hv one portlet ..i hv to access articleId in that??....
-
Al.Boldyrev over 7 yearsstackoverflow.com/questions/39233585/… Could you please look at my thread on the same theme and say if I can use same solution?
-
Serg Shapoval almost 7 yearsis it possible to get it using JSTL or EL?