Kallel Omar
Kallel Omar

Reputation: 1218

Liferay portlet: redirect to a new jsp with a js parameter

I'm working on a liferay portlet. What I want to do is opening a new jsp with sending to it a URL parameter coming from javascript variable. I thought about it and I found two ideas:

1)Send the js variable to the jsp using ajax and then create a render url in jsp with a parameter the value received from js. But how I send js variable to jsp I don't find a good example in the internet.

2)Build the render url in javascript using the received parameter and then redirect from the script itself to the new jsp file using the render url that I found. For this idea I posted this question Liferay portlet: redirect to an other jsp page from javascript but I didn't get solution for it yet.

Has someone a suggestion how I can achieve what I want using one of my ideas or may be an other idea?

Upvotes: 0

Views: 1765

Answers (1)

Kallel Omar
Kallel Omar

Reputation: 1218

I found finally a solution for this problem. I added in the jsp page a hidden post form that contains just one input and posts to an action method like that:

<portlet:actionURL var="jsVarActionURL" name="jsVarAction"/>
<form:form name="JsVarModel" method="post" modelAttribute="JsVarModel" action="<%=jsVarActionURL.toString() %>">
    <form:input id="jsVar" type="text" path="receivedMessage" style="display:none;"/>
    <input id="submit" type="submit" value="Add" style="display:none;"></input>
</form:form>

So my want was to for certain condition in javascript I have to send a js variable to new jsp page and open it. So in the script when the condition is valid I set to the input #jsVar the javascript value and I make a virtual click button in the submit button with trigger function of jquery like that:

var jsToJsp="Hello jsp I'm a js variable"

if(/*condition*/)
{
    $("#jsVar").val(jsToJsp);
    $("#submit").trigger("click");
}

In the controller the action method will receive the value coming from the form input field then it will redirect it to a render method:

@RenderMapping(params={"action=displayPageRender"}) 
public ModelAndView openUserProfilPage(RenderRequest request,RenderResponse response,Model model) 
{
    ModelAndView modelAndView= new ModelAndView("display");
    modelAndView.addObject("jsVar", request.getParameter("jsVar"));
    return modelAndView;

}

@ActionMapping(value = "jsVarAction")
public void sessionKeyActionMethod(@ModelAttribute("JsVarModel")ActionReceiveModel jsVar,ActionRequest actionRequest, ActionResponse actionResponse,Model model)
{
    actionResponse.setRenderParameter("jsVar", jsVar.getMessage());
    actionResponse.setRenderParameter("action", "displayPageRender");
}

Then I can receive it in display.jsp with ${jsVar} and everything works fine.

Upvotes: 0

Related Questions