掲示板

Liferay get parameter from render request in mvc portlet

9年前 に Marius Marin によって更新されました。

Liferay get parameter from render request in mvc portlet

New Member 投稿: 14 参加年月日: 14/09/04 最新の投稿
I have a big problem with Liferay portal. I have used both 6.1 and 6.2 but they behave the same in my case. I would be grateful if someone could help me with this.

I have searched everywhere on the internet for more than an year and I still can't find a solution to this problem. The explanation is either that I don't know how to do the simplest of tasks, or the system is broken. Ok, here it goes...

How do you obtain in a MVC portlet a parameter from the renderRequest and what is the explanation for the liferay behavior when it comes to parameters?

It should seem like the simplest of tasks, but obviously for me it never worked as it should, otherwise I wouldn't be here expressing my frustrations. I always worked around this, by using the http servlet request together with other tricks.
This is what I am trying: (in the past I tried everything imaginable, I have used the correct-marked answers from forums which were supposed to do exactly this, but without luck)

- in one jsp i am creating a portlet URL programmatically:

HttpServletRequest servletRequest = PortalUtil.getHttpServletRequest(renderRequest);

PortletURL createTeamURL = PortletURLFactoryUtil.create(servletRequest, portletName, plid, PortletRequest.RENDER_PHASE);
createTeamURL.setParameter("MyParameter", String.valueOf(Page.Pages.CREATE_TEAM));

- then i change via js the current url to this portlet url, and after the page reloads i can see my parameter in the browser address bar, with the correct portlet id as prefix and with the correct value.

- in the jsp that is rendered i am reading the parameter like so:

if((value = renderRequest.getParameter("MyParameter")) != null)
{
p = Integer.valueOf(value.toString());
}

or like so:

if((value = renderRequest.getParameter(renderResponse.getNamespace() + "MyParameter")) != null)
{
p = Integer.valueOf(value.toString());
}

- or like tons of other tries which i found on forums, because an official documentation is nowhere to be found (something like explained classes, methods, attributes etc).

The conclusion is that every time the call to getParameter() returns null.

Well, that's about it. I am seriously looking for an explanation for this issue, as it really makes me hate the toolbox that i'm using and to think that it belongs in the trash. Hope I'm wrong.
9年前 に Marius Marin によって更新されました。

RE: Liferay get parameter from render request in mvc portlet

New Member 投稿: 14 参加年月日: 14/09/04 最新の投稿
Can someone at least provide the simplest functional example about how to get a parameter from a render request?? Maybe it's not possible, but I doubt it?!
I wish to create programmatically a render url in A.jsp, add a parameter to it via java, change the browser url to this new render url when an action occurs (such as a plain HTML button click) and then read the parameter from the render request, either in the same jsp or in another one. Maybe someone does know how to do it? Because it shouldn't be that hard in theory, thanks!
thumbnail
9年前 に Jitendra Rajput によって更新されました。

RE: Liferay get parameter from render request in mvc portlet

Liferay Master 投稿: 875 参加年月日: 11/01/07 最新の投稿
Did you tried by getting parameter using ParamUtil ? . If you are using ParamUtil then need not to worry about portletNameSpace.

ParamUtil.getString(renderRequest,paramName);
9年前 に Marius Marin によって更新されました。

RE: Liferay get parameter from render request in mvc portlet

New Member 投稿: 14 参加年月日: 14/09/04 最新の投稿
Hi Jitendra Rajput,

Well, I didn't try that exact instruction, so I will have to do it in order to be sure. What I have tried however is the obvious renderRequest.getParameter(name) and it didn't work. Are the 2 ways not the same?
thumbnail
9年前 に Aditya Bhardwaj によって更新されました。

RE: Liferay get parameter from render request in mvc portlet

Junior Member 投稿: 78 参加年月日: 15/01/08 最新の投稿
Hi Marius Marin,

You can use ParamUtils methods like :-

For String :-
1)ParamUtil.getString(portletRequest, param);
2)ParamUtil.getString(portletRequest, param, defaultValue);

For Integer :-
1)ParamUtil.getInteger(portletRequest, param);
2)ParamUtil.getInteger(portletRequest, param, defaultValue);

Benifit of using ParamUtil Class is that :-
a)No need to type cast to string,integer etc
b)You can provide default value if no string is found so you can handle null pointer exception.

You can also use GetterUtil Classes also
9年前 に Marius Marin によって更新されました。

RE: Liferay get parameter from render request in mvc portlet

New Member 投稿: 14 参加年月日: 14/09/04 最新の投稿
Hi Aditya Bhardwaj,

I have no problem to cast the parameter and i don't want a default value, I need THE VALUE of the parameter. The call to renderRequest.getParameter should return my parameter if it was set previously with renderURL.setParameter. Right?
thumbnail
9年前 に Aditya Bhardwaj によって更新されました。

RE: Liferay get parameter from render request in mvc portlet

Junior Member 投稿: 78 参加年月日: 15/01/08 最新の投稿
Hi ,

If you have a form with fields(text boxes) and you submit the form with action Url then you get the values by ParamUtils easily.
thumbnail
9年前 に Olaf Kock によって更新されました。

RE: Liferay get parameter from render request in mvc portlet

Liferay Legend 投稿: 6403 参加年月日: 08/09/23 最新の投稿
As you say you're creating the URL programmatically in a JSP, how about doing it through the standard tags? This will make the code more readable and give you another way to check on which side of the URL (generation or consumption) the problem is:

<portlet:renderurl var="createTeamURL">
    <portlet:param name="MyParameter" value="<%=String.valueOf(Page.Pages.CREATE_TEAM) %>" />
</portlet:renderurl>


If you look at the URL you'll notice that the parameter is actually namespaced - on HTTP level, it's not named "MyParameter", but it has some portlet scope attached to it. I'm not sure if this happens automatically when you construct the URL like you posted in your initial question. You might have to take care of the namespacing yourself.
9年前 に Marius Marin によって更新されました。

RE: Liferay get parameter from render request in mvc portlet

New Member 投稿: 14 参加年月日: 14/09/04 最新の投稿
Hi Olaf Kock,

I remember trying with the renderURL tag also, the same problem happens. But isn't the following:

PortletURL createTeamURL = PortletURLFactoryUtil.create(servletRequest, portletName, plid, PortletRequest.RENDER_PHASE);
createTeamURL.setParameter("MyParameter", String.valueOf(Page.Pages.CREATE_TEAM));

supposed to create a renderURL programmatically? Because there is no documentation on how things should be done "the proper way", only scraps of code that benevolent other people left behind after struggling with these problems. I will try again with the tag, maybe i don't remember well if i already tried it. I remember doing every possible version, but in the end only the http servlet request had the parameter i was looking for and helped me to resolve my problem.
9年前 に Marius Marin によって更新されました。

RE: Liferay get parameter from render request in mvc portlet

New Member 投稿: 14 参加年月日: 14/09/04 最新の投稿
I will try to prepare a sample MVC portlet with the suggestions that everyone made, test it, and if it doesn't work, I'll upload the project so that it can be inspected for errors. Thanks for the responses.
thumbnail
9年前 に Sudarshan Kumar によって更新されました。

RE: Liferay get parameter from render request in mvc portlet

New Member 投稿: 4 参加年月日: 14/05/30 最新の投稿
Hi Marius,

Please try to give element name prefix with <portlet:namespace/> . e.g. if you have to submit a form having text box,

<input type="text" name="<portlet:namespace/>paramName" /> .

In controller side we can get the value like this:

String paramVal = ParamUtil.getString(renderRequest,paramName);

Regards,
Sudarshan
9年前 に Marius Marin によって更新されました。

RE: Liferay get parameter from render request in mvc portlet

New Member 投稿: 14 参加年月日: 14/09/04 最新の投稿
Sudarshan Kumar:
Hi Marius,

Please try to give element name prefix with <portlet:namespace/> . e.g. if you have to submit a form having text box,

<input type="text" name="<portlet:namespace/>paramName" /> .

In controller side we can get the value like this:

String paramVal = ParamUtil.getString(renderRequest,paramName);

Regards,
Sudarshan



Hi Sudarshan,

I tried that and many more other things. Unfortunately, it didn't work. I have found out what my problem actually is, though. The call to retrieve the parameter may be functioning well, I suppose, BUT THE REAL PROBLEM IS HOW DO I SET THE RENDER PARAMETER VIA JAVASCRIPT? I have tried half of what you suggested, in the past, something like:

<input type="text" name="<portlet:namespace/>paramName" />

and then

renderRequest.getParameter("<portlet:namespace/>paramName")

or without the namespace. Neither one seems to work. Do I need to specifically use ParamUtil to get the damned parameter? I will try it immediately, because i have run into a problem recently where the HttpServletRequest workaround can't help me anymore. I really need to find out how to get the damned parameter from the renderRequest, because it's the lowest brick of the building, so there's no excuse why it shouldn't work.

Maybe if someone can provide the correct way to add a parameter to a render url via javascript, so that i may retrieve it as it should be retrieved (from the renderRequest) I will find meaning in life and regain confidence in humanity emoticon
9年前 に Marius Marin によって更新されました。

RE: Liferay get parameter from render request in mvc portlet

New Member 投稿: 14 参加年月日: 14/09/04 最新の投稿
I have tried to call in the jsp:

System.out.println(ParamUtil.getLong(renderRequest, Page.Parameters.TEAM_REPEATER_CURRENT));

which should write to the java log the current page of a personal paginator (not the aui one).
Whenever a HTML button is pressed to change the current page of the paginator, the following javascript code is executed in a function:

function xxx(current)
{
var repeaterCurrentParameter = document.createElement("input");
repeaterCurrentParameter.type = "hidden";
repeaterCurrentParameter.name = "<portlet:namespace /><%= Page.Parameters.TEAM_REPEATER_CURRENT %>";
repeaterCurrentParameter.value = current;

var formElement = document.createElement("form");
formElement.method = "GET";
formElement.action = "<%= renderURL.toString() %>";
formElement.appendChild(repeaterCurrentParameter);
formElement.submit();
}

What is actually written in the java log is everytime "0", which is the default long value returned by ParamUtil, in case it doesn't have the parameter.
9年前 に Marius Marin によって更新されました。

RE: Liferay get parameter from render request in mvc portlet

New Member 投稿: 14 参加年月日: 14/09/04 最新の投稿
UPDATE:

I am always loading the same jsp, which is the main one. Inside it I include other jsps dynamically, using the <jsp:include> tag. Could this be the problem? I mean, the HttpServletRequest's parameters are kept for the included jsps, but not the renderRequest's parameters? I just don't know.
9年前 に Marius Marin によって更新されました。

RE: Liferay get parameter from render request in mvc portlet (回答)

New Member 投稿: 14 参加年月日: 14/09/04 最新の投稿
Happy day, I have managed to solve my very old nemesis. I have dropped the <jsp:include> tags and replaced them with static includes, however after this maneuver the problem still persisted. After that I realized that the issue happens with "GET" html forms for some reason. I have manually rewritten the parameter values from the url via javascript and the render parameters now appear in the logs.
Perhaps someone knows why is there a problem with submitting of html GET forms? Probably the parameters get encoded or what could be the explanation?

In any case, thanks to everyone who tried to understand the problem and help, I'm pretty sure every bit of information helped me get nearer the solution.
8年前 に kalkidan chekol によって更新されました。

RE: Liferay get parameter from render request in mvc portlet

New Member 投稿: 3 参加年月日: 15/06/15 最新の投稿
I had the same problem for two days and here is my solution that worked for me:

I have a home page that shows a list of books with title and author
<%@ taglib prefix="portlet" uri="http://java.sun.com/portlet_2_0"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ page contentType="text/html" isELIgnored="false"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<portlet:renderURL var="detail">
<portlet:param name="myaction" value="detail" />
</portlet:renderURL>
<portlet:renderURL var="showAddBookUrl">
<portlet:param name="myaction" value="addBookForm" />
</portlet:renderURL>
<portlet:renderURL var="showUrl">
</portlet:renderURL>
<form:form name="book" method="post" action="${showAddBookUrl}">

<table border="1">
<tr bgcolor="#99CCFF">
<td valign="top"><b>Title</b></td>
<td valign="top"><b>Author</b></td>
</tr>
<tr>
<c:forEach var="book" items="${books}">
<portlet:renderURL var="detail">
<portlet:param name="id" value="${book.id}"/>
<portlet:param name="myaction" value="detailOfBook" />
</portlet:renderURL>
<tr><td valign="top">

<a href="${detail}">


<c:out value="${book.title}" />
</a>
</td>
<td valign="top"><c:out value="${book.author}" /></td>
</tr>

</c:forEach>
</table>

<table align="right">
<tr>
<td><input type="submit" value="Add Book" /></td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
</table>
</form:form>
<br></br>


What I want to do is when "title" of any book is clicked I want to show another page that shows detail of this book, id,title,author, isbn, so I need to pass the id of the selected book to a different jsp page.
at the the top of the jsp page above I have:
<portlet:renderURL var="detail">
<portlet:param name="myaction" value="detail" />
</portlet:renderURL>
which is to redirect my page to a page that shows detail of a book

and then I have the following inside for each loop
<portlet:param name="id" value="${book.id}"/>
<portlet:param name="myaction" value="detailOfBook" />
</portlet:renderURL>
to store id of a book on variable "id" and I have the following code to link each title with the <portlet renderURL ... I defined on the top of the page, so I used the variabele ${detail} as a refrence
<tr><td valign="top">
<a href="${detail}">
<c:out value="${book.title}" />
</a>
here is the trick, notice, that the first render action parameter(name="myaction" value="detail") and the second one in the middle of the page for the sake of holding id of a book --render parameter(name="myaction" value="detailOfBook"). Note that the first paramter (name="myaction" value="detail") will no more be useful technically! since they have the same param name="myaction" it is replaced by (name="myaction" value="detailOfBook").
finally, I have the following in my controller:
@RenderMapping(params="myaction=detailOfBook") //notice the parameter
public String showDetail(RenderRequest request)
{
return "detail";
}
and to hold the variable
@ModelAttribute("bookDetail")
public Book getBook(@RequestParam(required=false) String id) {
if(id==null)id="1";
//System.out.println("in getBook%%%%%%%%%%%%%%%%%%%"+id);
Book book=bookDAOImpl.getBook(Integer.parseInt(id));
System.out.println(book.getTitle());
return book;
}
so in my detail.jsp
table border='1'>
<tr>
<td bgcolor="green">Title</td>
<td>${bookDetail.title}</td>
</tr>
<tr>
<td bgcolor="green">Author</td>
<td>${bookDetail.author}</td>
</tr>
<tr>
<td bgcolor="green">ISBN</td>
<td>${bookDetail.isbn}</td>
</tr>
<tr>
<td bgcolor="green">Summary</td>
<td>${bookDetail.summary}</td>
</tr>
</table>
note that I used "bookDetail" model that I used for storing book object using @ModelAttribute in controller.

I hope this will help!