简体   繁体   中英

Liferay 7.3 how to get the parameter values inside a portlet and access in view.jsp

I am trying to get the parameters inside a portlet in Liferay 7.3. If I pass the parameter like:

http://localhost:8080/web/guest?name=abhi

in a web page, that contains my portlet. So My question is

Q1) can I get this parameter inside the Portlet Controller (doView method) when user reloads the above page, by doing something like:

import com.liferay.portal.kernel.util.ParamUtil;

public class MySamplePortlet extends MVCPortlet {

 @Override
public void doView(RenderRequest renderRequest, RenderResponse renderResponse)
        throws IOException, PortletException {

   String name = ParamUtil.getString(renderRequest, "name");
   System.out.println(name);

 }
}

Here I am getting a blank for the 'name' when printing.

And

Q2) how I check the condition of this parameter in view.jsp view file (how to get the class variable value inside the view file)?

<% if(name) %>
  render form view
<% else %>
  render messageBox view

......

I want to render different views according to the parameter value.

Parameters to a portlet are typically namespaced - eg a HTTP-parameter name could be anything to any portlet - and a page potentially has many portlets. All of them might think of name as something different: A city, a user, a customer, the currently used computer, a pet...

Thus any form typically submits values that look like (in jsp-lingo): <input name="<portlet:namespace/>name"..../> rather than <input name="name"..../>

If you use tags that are portlet-aware, they might do the decoration automagically, eg <aui:input name="name".../> .

Utilizing undirected and unnamespaced parameters in the portlet world might generate maintenance nightmares, specifically when you run into your first ambiguous name.

However, you can deactivate the namespacing, per portlet. Or you can manually access the underlying HTTPServletRequest. Both are strongly discouraged. Here's how you shoot yourself in the foot if you like to:

  • Set the portlet's property com.liferay.portlet.requires-namespaced-parameters=false
  • Utilize PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(renderRequest)) (what these functions do - and why you need them both - is described, for example, here )

So the ParamUtil will work if the parameter is for the portlet. in this case it seems you want the parameter in the url in general meaning not specific for the portlet. to achieve this you need to use PortalUtil

HttpServletRequest httpReq = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(req));
String name = httpReq.getParameter("name");

Note im looking for the original request. not the portlet request.

hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM