2013-03-11 30 views
7

Trong jsp của tôi, tôi có đoạn mã sau:Liferay <portlet: actionURL>

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

Tôi đang sử dụng liferay. Tôi muốn gửi dữ liệu này sẽ được xử lý trong một lớp java. lớp java của tôi có ít chức năng. làm thế nào tôi nên xác định trong jsp ở trên mà nó nên truy cập vào chức năng cụ thể trong java sau khi nộp mẫu?

Trả lời

14

Nếu portlet của bạn được thừa hưởng MVCPortlet, chỉ cần tạo một phương pháp nào với "tên" giống như actionURL của bạn, rằng có một đối ActionRequestActionResponse:

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

perp: do đó, trong actionURL của tôi ở trên: tên là addDetails nên tên hàm của tôi phải là addDetails, phải không? –

+0

@cya k: Bạn hiểu rồi. – perp

+0

Cảm ơn !! xóa các nghi ngờ của tôi –

6

Nếu bạn muốn xử lý chỉ có một hành động:

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> 

Nếu bạn muốn nhiều hơn một phương pháp hành động:

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> 

Nhưng có lẽ bạn sẽ làm gì:

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

gì nếu tôi có nhiều chức năng trong lớp java? sau đó làm thế nào nó sẽ biết được chức năng nó nên đi đến? –

+0

Tôi đã cập nhật câu trả lời của mình. Chấp nhận nó nếu có bất kỳ trợ giúp nào của nó :) – kayz1

9

Chỉ cần cho các hồ sơ, bạn cũng có thể sử dụng các chú thích. Ví dụ, bạn có jsp này:

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

Và phương thức Java này trong MVCPortlet của bạn:

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

Nếu bạn không sử dụng MVCPortlet mà là một cái gì đó giống như GenericPortlet, thêm một param đến actionURL như thế này :

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

Sau đó, trong phương pháp processAction của bạn, xử lý kiểm tra các loại phương pháp theo cách này:

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

     } 
1

Nếu lớp java của bạn đang mở rộng MVCPortlet thì chỉ cần tên phương thức phải khớp với tên actionURL tức là addDetails. Nếu lớp đang mở rộng GenericPortlet, bạn sẽ phải chỉ định chú thích cho cùng một phương thức của bạn như @ProcessAction (name = "addDetails") .Trong trường hợp này tên của phương thức của bạn có thể khác nhau.

2

Dán này trong lớp điều khiển của bạn

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

hoặc bạn có thể sử dụng nó như

@ProcessAction(name="addDetails") 
public void myaddDetails(ActionRequest actionRequest, ActionResponse actionResponse) { 
    //this method will be called on submitting the form 
    System.out.println("addDetails"); 
} 
Các vấn đề liên quan