7

在我的jsp中,我有以下代码:

<portlet:actionURL name="addDetails" var="addDetailsURL" />
<aui:form name="addDetails" action="<%=addDetailsURL.toString() %>" method="post" >
        <aui:input type="text" label="name:" name="name" value="" />
        <aui:input type="text" label="surname:" name="surname" value="" />
        <aui:input type="text" label="age:" name="age" value="" />
        <aui:button type="submit" value="addDetails" />
</aui:form>

我正在使用liferay。我想提交将在 java 类中处理的数据。我的 java 类的功能很少。我应该如何在上面的jsp中指定它应该在提交表单后访问java中的特定函数?

4

6 回答 6

14

如果您的 portlet 继承MVCPortlet,只需创建一个与您的 actionURL 具有相同“名称”的公共方法,它接受一个ActionRequestandActionResponse参数:

public void addDetails(ActionRequest req, ActionResponse rsp) {
    // ...
}
于 2013-03-11T06:51:57.937 回答
9

只是为了记录,您也可以使用注释。例如,你有这个 jsp:

<portlet:actionURL var="urlAction">
    <portlet:param name="javax.portlet.action" value="myAction"/>
</portlet:actionURL>

<aui:form action="${urlAction}" method="post">
    <aui:input type="text" label="name:" name="name" value="" />
    <aui:button type="submit" value="Submit" />
</aui:form>

而你的这个Java方法MVCPortlet

@ProcessAction(name = "myAction")
public void method(ActionRequest actionRequest, ActionResponse actionResponse) {
    // ...
}
于 2013-03-12T11:37:25.210 回答
6

如果您只想处理一项操作:

小门户

@Override
public void processAction(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException
{
    // TODO Auto-generated method stub
    super.processAction(actionRequest, actionResponse);
}

JSP

<form action="<portlet:actionURL />" method="post">
    <aui:button type="submit" value="addDetails" />
</form>

如果您想要多个操作方法:

public void myAction(ActionRequest request, ActionResponse response)
{
     Long id = ParamUtil.getLong(request, "myParam");
     // TODO
}

JSP

<portlet:actionURL name="myAction" var="myActionVar">
    <portlet:param name="myParam" value="${currentElement.id}"></portlet:param>
</portlet:actionURL>

<a href="${myActionVar}">Click Me!</a>

但你可能会这样做:

小门户

@Override
public void serveResource(ResourceRequest request, ResourceResponse response) throws IOException
{
    String action = request.getParameter("action");

    if(action.equalsIgnoreCase("myAction")){
        // handle AJAX call
    }
}

JSP

<portlet:resourceURL var="resourceUrl" />
<input id="resourceURL" type="hidden" value="${resourceUrl}" />

JavaScript

$.post($('#resourceURL').val(),{
    action : 'myAction'
}).done(function(result){
    alert('Action completed successfully!')
});
于 2013-03-11T06:50:22.997 回答
3

如果您不使用 MVCPortlet 而是使用类似 GenericPortlet 的东西,请向 actionURL 添加一个参数,如下所示:

<portlet:actionURL name="addDetails" var="addDetailsURL" >
<portlet:param name="method" value="addDetails"/>
</portlet:actionURL>

然后在您的 processAction 方法中,以这种方式处理检查方法类型:

public void processAction(ActionRequest aReq, ActionResponse aResp) throws IOException,
            PortletException {

        final String method = aReq.getParameter("method");

        if ( method != null && method.equals("addDetails")) 
        {   
            addDetails(aReq, aResp);

        } 
于 2013-06-12T17:50:58.207 回答
2

将此粘贴到您的控制器类中

public void addDetails(ActionRequest actionRequest, ActionResponse actionResponse) {
    //this method will be called on submitting the form
    System.out.println("addDetails");
}

或者您可以将其用作

@ProcessAction(name="addDetails")
public void myaddDetails(ActionRequest actionRequest, ActionResponse actionResponse) {
    //this method will be called on submitting the form
    System.out.println("addDetails");
}
于 2015-11-01T23:13:34.817 回答
1

如果您的 java 类正在扩展 MVCPortlet,那么只有方法名称应该与 actionURL 名称匹配,即 addDetails 。如果该类正在扩展 GenericPortlet,您将必须在您的方法之上指定相同的注释,例如 @ProcessAction(name="addDetails")。在这种情况下,您的方法的名称可以不同。

于 2014-07-13T13:48:18.147 回答