【问题标题】:Parameter value from URL is nullURL 中的参数值为空
【发布时间】:2015-11-21 08:42:46
【问题描述】:

Liferay 6.1.0 中有一个 portlet,其中一个 jsp 被重定向到另一个带有 URL 参数的 jsp。

http://localhost:8081/web/guest/debitpayment?fishId=1467

我想用下面的代码获取fishId的值,但是为空:

@Override
protected Object formBackingObject(PortletRequest request) throws Exception {
    HttpServletRequest httpServletRequest = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(request));
    String fishId = httpServletRequest.getParameter("fishId");
    //fishId is null
    return super.formBackingObject(request);
}

最后我尝试了这段代码从queryString 获取值并返回该值。但我想知道为什么从httpServletRequest 获取它是空的。

@Override
protected Object formBackingObject(PortletRequest request) throws Exception {
    String fishId = getParameter(request,"fishId");
    //fishId is now 1476
    return super.formBackingObject(request);
}

public static String getParameter(PortletRequest portletRequest, String paramName) {
    HttpServletRequest httpRequest = PortalUtil.getHttpServletRequest(portletRequest);
    HttpServletRequest httpServletRequest = PortalUtil.getOriginalServletRequest(httpRequest);
    String retval = portletRequest.getParameter(paramName);
    if (isNull(retval)) {
        retval = httpRequest.getParameter(paramName);
        if (isNull(retval)) {
            retval = httpServletRequest.getParameter(paramName);
            if (isNull(retval)) {
                retval = getParameterFromQueryString(httpServletRequest.getQueryString(), paramName);
                if (isNull(retval)) {
                    HttpServletRequest request = httpServletRequest;
                    try {
                        while (request != null) {
                            Method m = httpServletRequest.getClass().getMethod("getRequest", null);
                            request = (HttpServletRequest) m.invoke(request, null);
                            retval = request.getParameter(paramName);
                            if (Utility.isNotNull(retval))
                                break;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    return retval;
}

private static String getParameterFromQueryString(String queryString, String paramName) {
    String[] paramValues = queryString.split("&");
    for (String paramValue : paramValues) {
        String[] param = paramValue.split("=");
        if (param.length == 2 && param[0].equalsIgnoreCase("currentURL")) {
            String url = param[1];
            String[] url_splittedWithQuestionMark = url.split("%3F");// ?
                                                                        // mark
            if (url_splittedWithQuestionMark.length == 2) {
                String url_params = url_splittedWithQuestionMark[1];
                String[] url_paramValues = url_params.split("%26");// & mark
                for (String url_paramValue : url_paramValues) {
                    String[] one_paramValue = url_paramValue.split("%3D");
                    // =
                    // mark
                    if (one_paramValue.length == 2 && one_paramValue[0].equals(paramName))
                        return one_paramValue[1];
                }
            }
        }
    }
    return null;
}

【问题讨论】:

  • 您提供的 URL 看起来缺少 portlet 标识符。这可能会导致值无法到达 portlet。它应该类似于 http://localhost:8081/web/guest/debitpayment?p_p_id=myportlet_WAR_myportletportlet&fishId=1467。 P_p_id 是您想要接收值的 portlet 的 id。

标签: spring liferay


【解决方案1】:

免责声明:我没有使用过 Spring 和 Liferay。我在liferay 中使用过MVCPortlet。所以我的回答是基于 Liferay 中的 MVCPortlet 方法。你或许也可以在 Spring/Liferay 中做类似的事情。

为了将值来回传递给各种 JSP 和自定义 Portlet 类,我们通常使用呈现请求或操作请求属性。可以在自定义 Portlet 类的 doViewprocessAction 方法中设置和检索这些请求属性。我了解这是推荐且经过验证的方法。您也许可以使 URL 参数正常工作,但您会发现更多示例/支持和使用请求属性的帮助。

自定义 Portlet 类中的 API 示例如下:

public void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws
   renderRequest.setAttribute("name", value);

public void processAction(ActionRequest actionRequest, ActionResponse actionResponse) throws
    actionRequest.setAttribute("name", value);

在您的 JSP 中,您将能够使用 JSTL 核心和 aui 标签访问它:

JSP 代码示例:

<c:forEach items="${name}" var="varname">
<c:out value="${name}" />
<aui:option label="${name}" value="${name}" />

你也可以参考这个帖子Passing variable to jsp

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    我找到了问题的原因和答案。 我在 liferay-portlet.xml 中为这个 portlet 使用了 render-weight 配置来改变它在页面中加载的优先级。当我删除此配置时,一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 2020-08-24
      • 2011-10-13
      • 2014-07-12
      • 2014-10-01
      • 2021-02-16
      相关资源
      最近更新 更多