Liferay <portlet:actionURL>

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中的特定函数?

我使用Liferay。 我想提交这些数据,这些数据将在Java类中进行处理。 我的Java类有几个函数。 在提交表单后,我应该如何在上述JSP中指定它应访问Java中的特定函数?
6个回答

14
如果您的Portlet继承了,只需创建一个与您的actionURL相同的公共方法,该方法需要使用ActionRequest和ActionResponse参数:
public void addDetails(ActionRequest req, ActionResponse rsp) {
    // ...
}

在我的上面的 actionURL 中: <portlet:actionURL name="addDetails" var="addDetailsURL" /> name 是 addDetails,所以我的函数名应该是 addDetails,对吗? - Seeya K
你也可以使用@ProcessAction注释。 - Ankit Katiyar

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>

还有您的MVCPortlet中的这个Java方法:

@ProcessAction(name = "myAction")
public void method(ActionRequest actionRequest, ActionResponse actionResponse) {
    // ...
}

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>

但是你可能需要做的是:

Portlet(门户小部件)

@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!')
});

如果Java类中有许多函数,它将如何知道应该进入哪个函数? - Seeya K

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);

        } 

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");
}

1
如果你的Java类继承了MVCPortlet,则方法名应该与actionURL名称匹配,例如addDetails。如果类继承GenericPortlet,则必须在方法顶部指定相同的注释,例如@ProcessAction(name =“addDetails”)。在这种情况下,您的方法名称可以不同。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接