Two action URLs in one jsp

一个人想着一个人 提交于 2019-12-11 02:15:20

问题


I just want to know, if I can have two <portlet:actionURL var="wakeup"> action urls in one jsp. Which will be called depending on a filled inputs and JS submiting this forms. Example:

JS:

function startActions() {
    if (document.getElementById("shut").checked ||
        document.getElementById("reboot").checked) {    
        document.getElementById("shutdownRebootForm").submit();
    }
    if (document.getElementById("wake").checked) {
        document.getElementById("wakeUpForm").submit();
    }
}

HTML:

<portlet:actionURL var="shutdownReboot">
    <portlet:param name="action" value="shutdown-reboot" />
</portlet:actionURL>
<form id="shutdownRebootForm" method="post" action="${shutdownReboot}">
    //some HTML code
</form>

<portlet:actionURL var="wakeup">
    <portlet:param name="wakeaction" value="wakeup" />
</portlet:actionURL>
<form id="wakeUpForm" method="post" action="${wakeup}">
    //some HTML code
</form> 

I'm using spring MVC... is this possible to implement? Will it work as expected?


回答1:


Liferay Documentation:

You can have as many actions as you want in a portlet. Implement each one as a method that receives two parameters: an ActionRequest and an ActionResponse. Name the method whatever you want, but note that the method name must match the URL name that points to it.

Therefore, based on suitable JSP, action and scripting structure, you can place as many actionURLs, you want in a single portlet. The important point is to match the value of name attribute specified on URL tag with method's name.

If you are specifying name attribute, then you need to implement separate action methods in listener class.

Pattern 1:

See, your actionURLs will look like following:

<portlet:actionURL var="shutdownRebootURL" name="shutdownReboot">
    <portlet:param name="action" value="shutdown-reboot" />
</portlet:actionURL>

<portlet:actionURL var="wakeupURL" name="wakeup">
    <portlet:param name="wakeaction" value="wakeup" />
</portlet:actionURL>

So, your action slass should contain following methods:

public void shutdownReboot(ActionRequest actionRequest, ActionResponse actionResponse)
    throws IOException, PortletException {
        // Your shutdownReboot logic goes here
}

public void wakeup(ActionRequest actionRequest, ActionResponse actionResponse)
    throws IOException, PortletException {
        // Your wakeup logic goes here
}

However, there is no hard-n-fast need to put two form(s) / actionURLs, you can achieve your requirement by implementing one action method in listener class, filtered based on the parameter's value. On each action, that parameter will identify, which action has been called.

Pattern 2:

Consider following example:

HTML:

<form action="<portlet:actionURL />" method="post">
    <input type="hidden" name="action" id="action" value="" />

    <table>
        <tr>
            <td>shutdown checkbox</td>
            <td>reboot checkbox</td>
            <td>wakeup checkbox</td>
        </tr>
        <tr>
            <td>
                <!-- show specific button based on checkbox checked -->
                <button type="submit" id="shutdownReboot" onclick="startActions('shutdownReboot');">Shutdown / Reboot</button>
                <button type="submit" id="wakeup" onclick="startActions('wakeup');">Wakeup</button>
            </td>
        </tr>
    </table>
</form>

Javascript:

function startActions(action) {
    document.getElementById('action').value = action;
    /* set other required parameters */
}

Action method:

String action = "";
public void processAction(ActionRequest actionRequest, ActionResponse actionResponse)
        throws IOException, PortletException {

    if(actionRequest.getParameter("action") != null && actionRequest.getParameter("action") != ""){
            action = actionRequest.getParameter(action);
    }

    if(action.equals("shutdownReboot")){
        // shutdown-reboot logic
    }else if(action.equals("wakeup")){
        // wakeup logic
    }else{
        // default logic
    }       
}


来源:https://stackoverflow.com/questions/29413937/two-action-urls-in-one-jsp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!