简体   繁体   中英

Doing an ActionRequest call from JS/jQuery/Ajax in Spring MVC

I'm trying to redirect to a portlet with specific parameters from JavaScript. I take parameters based on which link the user clicks (this part is OK, I get all the data I need), and afterwards construct an URL which I try to get with simple JS window.location..

My Controller:

@RenderMapping
public String view(Model model){
    model.addAttribute("some", "stuff");
    return "myPortlet/view";
}

@ActionMapping(params = "action=importantAction")
public void doAction(ActionRequest request, ActionResponse response){
    String foo = request.getParameter("one");
    String bar = request.getParameter("two");
    System.out.println("Got " + one + " & " + two);
}

My JS:

function myFunction(one_val, two_val){
    window.location = "http://www.my.url.com/nameOfMyPortlet?one=" + one_val +
        + "&two=" + two_val + "&action=importantAction";
}

This redirects to the correct page, however the action parameter keeps getting ignored and the whole doAction method doesn't get executed.

How to pass the action parameter to the target portlet from JavaScript?

I'm using Liferay & Spring MVC..

Thanks!

Did you try creating action URL using below way ?

var portletURL = new Liferay.PortletURL('ACTION_PHASE'); portletURL.setWindowState("maximized"); In portletURL you can set your desired parameter for calling your action.

Below link for reference

http://www.liferay.com/web/eduardo.lundgren/blog/-/blogs/liferay-portleturl-in-javascript

I've managed to find a way how it works - it's very similar to what @Ankit P wrote:

I have created hidden fields for the parameters and then sent the form which creates the URL:

JSP:

<liferay-portlet:actionURL var="link">
    <liferay-portlet:param name="action" value="importantAction"/>
</liferay-portlet:actionURL>
<form action="${link}" method="POST" name="the-form" id="the-form">
    <input type="hidden" value="" name="one" id="one"/>
    <input type="hidden" value="" name="two" id="two"/>
</form>

JS:

function myFunction(one_val, two_val) {
    document.getElementById("one").value = one_val;
    document.getElementById("two").value = two_val;
    document.getElementById("the-form").submit();
}    

Controller stays the same. To use @ActionMapping you have to use POST method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM