如何从 Liferay portlet 中的 URL 获取参数?

发布于 2024-10-16 06:37:40 字数 329 浏览 7 评论 0原文

我在 Liferay 6 中使用开箱即用的 portlet 的 jsp,例如 feed.jspf:

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

无论是在自定义 portlet 还是在 .jsp 文件中,它都会给出 null 值,但它应该有一个值。

I'm using jsp of out-of-box portlet like feed.jspf in 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

又怨 2024-10-23 06:37:40

当然,您始终可以使用标准 HttpServletRequest 来检索参数。您可以使用 PortalUtil 类获取此请求,如下例所示:

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

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");
殤城〤 2024-10-23 06:37:40

在我的 Liferay- JSP 中,我使用了这个:(

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

%>

这结合了其他答案的智慧片段,特别是 Miguel Gil Martínez 在 2012 年 7 月 23 日 17:35 的回答

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

谈场末日恋爱 2024-10-23 06:37:40

使用 portlet 时,每个 HttpServletRequest 参数都有一个前缀(用于通知参数的“类型”)和一个后缀(用于表示 portlet 的哪个实例应该处理该参数)。因此,参数的名称不仅仅是“articleId”。我不知道您正在使用什么 portlet,但如果它是一个名为“example”的 portlet,通过战争部署,则参数将被称为 example_WAR_exampleportletwar_articleId_w2Xd

但是,您不必处理如此复杂的问题。如果您正在某个已创建的 portlet 的 JSP 中工作,则应该有一个名为 renderRequest 的对象,它包含所有参数并抽象所有这些名称修改。因此,您不必检索原始 servlet 请求,而是使用它:

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

如果未定义该对象,您只需在某处插入 标记,之后该对象将可用。

HTH。让我们知道它是否有效!

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!

dawn曙光 2024-10-23 06:37:40

它对我有用:

public void doView(RenderRequest request, RenderResponse response) throws 
  PortletException, IOException {

HttpServletRequest requestInsideThePortlet = PortalUtil.getHttpServletRequest(request);

String myURLParam =
 PortalUtil.getOriginalServletRequest(requestInsideThePortlet).getParameter("myURLParam");

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

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

....
....
....
}
千と千尋 2024-10-23 06:37:40

对于 JSF,我使用这个:

@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() );
    }
}

然后在你的 Facelet 中:

#{originalRequest.getParam('my_param')}

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')}
回首观望 2024-10-23 06:37:40

我尝试了你的解决方案,但渲染请求给了我一个例外,
所以这是另一个解决方案:

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;

}

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;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文