来自 URL 的参数值为空

Parameter value from URL is null

Liferay 6.1.0 中有一个 portlet,其中一个 jsp 被重定向到另一个 jsp,在 URL.

中有一个参数

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 中获取值,并且它是 returns 的值。但我想知道为什么从 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;
}

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

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

下面是自定义 Portlet API 中的几个示例 class:

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}" />

你也可以参考这个postPassing variable to jsp

希望对您有所帮助!

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