2012-03-27 50 views
0

我有我的門戶的一個問題,我不知道要怎麼去解決它。 我的portlet通過在2個文本字段中插入一個名稱來添加或檢索來自liferay的數據庫的信息。 按下提交按鈕後,我看到來自服務器的響應,這樣的JSON響應:請求與AJAX在Portlet(Liferay的)

{"id":301,"name":"Pepo"} 

如果用戶正確插入如果搜索拋出一個不錯的結果。我必須重新回到瀏覽器才能看到門戶。

如何使用AJAX來從門戶到服務器動態地通過以下網址之後不刷新頁面?

的http://本地主機:8080/C /門戶/ json_service serviceClassName = com.liferay.test.service.TrabajadorServiceUtil & serviceMethodName = findByName & servletContextName = TrabajadorPlugin的portlet & serviceParameters = [參數1] &參數1 = NameInsertedByUser

現在我使用的是<form>標籤是這樣的:

<% 
//Shows "New Employee" in the text field when portlet is rendered, or gets the user input and pass it as a param to the URL 
PortletPreferences prefs = renderRequest.getPreferences(); 
String employee = (String)prefs.getValue("name", "New Employee"); 
%> 

<form id="postForm" method="post" action="http://localhost:8080/c/portal/json_service"> 
    <input name="serviceClassName" type="hidden" value="com.liferay.test.service.TrabajadorServiceUtil" /> 
    <input name="serviceMethodName" type="hidden" value="create" /> 
    <input name="servletContextName" type="hidden" value="TrabajadorPlugin-portlet" /> 
    <input name="serviceParameters" type="hidden" value="[param]" /> 
    <input name="param" type="text" value="<%=employee%>" /> 
    <input type="submit" value="Submit"/> 
</form> 

我明白AJAX是如何工作的,但我需要一些幫助,以實現URL創建我的功能被正確傳送到服務器同時GETPOST請求。這是我第一次嘗試使用AJAX。

非常感謝你,希望有人理解我的問題,能不能幫我。

回答

0

首先,我看見的都是沒有意義在這裏使用JSON服務。只需使用MVC控制器編寫普通的portlet,並在控制器寫入相應動作(存儲數據,搜索等)的動作處理。

在控制器中,您可以直接從java類com.liferay.test.service.TrabajadorServiceUtil(或TrabajadorLocalServiceUtil)調用create或findByName之類的靜態方法 - 人們通常會這樣做。

如果由於某種原因,你真的必須使用JSON,你當然應該做這些動作的使用AJAX調用 - 並使用JavaScript渲染的結果。問題更新後

更新:

  1. 派在Liferay的AJAX請求是使用AlloyUI JS框架,是Liferay的一部分,最簡單,最正確的方法。你可以在這裏閱讀更多關於如何發送AJAX請求:http://www.liferay.com/web/nathan.cavanaugh/blog/-/blogs/4733559

  2. 爲了實現你的目標,我建議在你的控制器/ portlet中實現processAction(ActionRequest actRequest,ActionResponse actResponse)方法。 爲了真正將數據發送給它,你就必須有actionURL,您可以創建使用例如門戶:actionURL標籤:

    <portlet:actionURL />或使用Java代碼PortletURL actionUrl = portletResponse.createActionURL();

  3. 然後使用剛剛提交表單發佈到此網址,並在actionRequest中您將擁有您的參數。

+0

嗨,謝謝你的回答。問題是,是的,我發送和接收JSON中的GET和POST請求,如下所示:{「id」:301,「name」:「Pepo」} 所以,我需要使用AJAX,不知道如何。我的URL如下: http:// localhost:8080/c/portal/json_service?serviceClassName = com.liferay.test.service.TrabajadorServiceUtil&serviceMethodName = findByName&servletContextName = TrabajadorPlugin-portlet&serviceParameters = [param1]&param1 = Pepo – agapitocandemor 2012-03-29 08:09:21

+0

其中Param1由用戶在文本字段中。我只需要將它添加到URL的末尾並使用AJAX發送它,以便您不會看到該空白頁面,並且不需要手動刷新瀏覽器就可以返回到門戶網站。你有什麼建議嗎?謝謝了很多,這是我第一次使用AJAX,而且我有點迷路。 – agapitocandemor 2012-03-29 08:11:55

+0

請參閱我的編輯,我更新了我的問題。 – agapitocandemor 2012-03-29 10:21:57