Liferay

2019-06-17 10:34发布

In my jsp I have the following code:

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

I am using liferay. I want to submit this data which will be processed in a java class. my java class has few functions. how should I specify in the above jsp that it should access the particular function in java after submitting the form?

6条回答
ら.Afraid
2楼-- · 2019-06-17 10:36

Just for the record, you can also use annotations. For example, you have this 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>

And this Java method in your MVCPortlet:

@ProcessAction(name = "myAction")
public void method(ActionRequest actionRequest, ActionResponse actionResponse) {
    // ...
}
查看更多
趁早两清
3楼-- · 2019-06-17 10:48

Paste this in your controller class

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

or you can use it as

@ProcessAction(name="addDetails")
public void myaddDetails(ActionRequest actionRequest, ActionResponse actionResponse) {
    //this method will be called on submitting the form
    System.out.println("addDetails");
}
查看更多
Explosion°爆炸
4楼-- · 2019-06-17 10:49

If you want to handle only one action:

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>

If you want more than one action method:

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>

But you probably would do:

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!')
});
查看更多
劳资没心,怎么记你
5楼-- · 2019-06-17 10:54

If you are not using MVCPortlet but rather something like GenericPortlet, add a param to the actionURL like this:

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

Then in your processAction method, handle checking the method type this way:

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

        } 
查看更多
成全新的幸福
6楼-- · 2019-06-17 10:56

If your java class is extending MVCPortlet then just the method name should match actionURL name i.e. addDetails . If the class is extending GenericPortlet you will have to specify annotaion for the same on top of your method like @ProcessAction(name="addDetails").IOn this case the name of your method can be different.

查看更多
家丑人穷心不美
7楼-- · 2019-06-17 10:59

If your portlet inherits MVCPortlet, simply create a public method with the same "name" as your actionURL, that takes an ActionRequest and ActionResponse argument:

public void addDetails(ActionRequest req, ActionResponse rsp) {
    // ...
}
查看更多
登录 后发表回答