开发者

How to get parameters from URL in Liferay portlet?

开发者 https://www.devze.com 2023-02-08 22:36 出处:网络
I\'m using jsp of out-of-box portlet like feed.jspf in开发者_JS百科 Liferay 6: String articleId =null;

I'm using jsp of out-of-box portlet like feed.jspf in开发者_JS百科 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.


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


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


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!


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

....
....
....
}


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')}


I tried ur solutions but render request it 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

精彩评论

暂无评论...
验证码 换一张
取 消