Liferay <portlet:actionurl> </portlet:actionurl>

时间:2013-03-11 06:45:19

标签: java jsp tags liferay liferay-6

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

6 个答案:

答案 0 :(得分:14)

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

public void addDetails(ActionRequest req, ActionResponse rsp) {
    // ...
}

答案 1 :(得分: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) {
    // ...
}

答案 2 :(得分:6)

如果您只想处理一个动作:

Portlet

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

答案 3 :(得分: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);

        } 

答案 4 :(得分: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");
}

答案 5 :(得分:1)

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