How to get parameters from URL in Liferay portlet?

I use the jsp portlet from the box, for example feed.jspf, in Liferay 6:

String articleId =null;
HttpServletRequest httpReq = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(renderRequest));
articleId = httpReq.getParameter("articleId");

Specifies a null value in a user portlet or in .jsp files, but it must be a value.

+3
source share
6 answers

Of course, you can always use the standard HttpServletRequest to retrieve your parameters. You can get this request using the PortalUtil class, as in the following example:

HttpServletRequest request = PortalUtil.getHttpServletRequest(portletRequest);
String articleId = request.getParameter("articleId");
+9
source

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, in particular, Miguel Gil Martinez on July 23, 2012 at 17:35

+7
source

HttpServletRequest , "" , , . , "articleId". , , , , , "", , example_WAR_exampleportletwar_articleId_w2Xd.

. JSP , renderRequest, . , :

String articleId = renderRequest.getParamenter("articleId");

If this object is not defined, you just need to insert the tag <portlet:defineObjects/>somewhere, and after that the object will be available.

NTN. Let us know if this worked!

+2
source

This 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");

....
....
....
}
+2
source

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 face:

#{originalRequest.getParam('my_param')}
+1
source

I tried ur solutions, but I am processing the request, this gives me an exception, so this is another solution:

public String obtainFromUrl(String keyFromWeb) {

    Object outcome = null;
    Map<String, Object> map = FacesContext.getCurrentInstance().getExternalContext().getRequestMap();
    if (map != null) {
        for (String key : map.keySet()) {
            if (map.get(key) instanceof HttpServletRequestWrapper) {
                HttpServletRequest request = (HttpServletRequest) ((HttpServletRequestWrapper) map.get(key))
                        .getRequest();
                outcome = request.getParameter(keyFromWeb);
                break;
            }
        }
    }
    return (String) outcome;

}
0
source

Source: https://habr.com/ru/post/1790163/


All Articles