liferay的MVCPortlet的ajax請求

1.js
function redirectAndAjax(id){
	$.post('<%=ajaxUrl%>',{'<portlet:namespace/>id':id},function(data){
		 alert(data);
		 });
}
在頁面上寫JS引入JQUERY ,其中的ajaxUrl爲在頁面中定義的。定義代碼爲
<portlet:resourceURL var="ajaxUrl">

要在json的key前加入<portlet:namespace/> 否則後臺無法收到傳入的參數。


後臺返回的值由js中的回調函數的data變量來接收。

jquery的文件爲jquery-1.8.2.min.js 存放於 /docroot/js 目錄下。

在jsp中的引入方式爲在liferay-portlet.xml中的對應的portlet定義中的

<header-portlet-css>/css/main.css</header-portlet-css>

下加入行
<header-portlet-javascript>/js/jquery-1.8.2.min.js</header-portlet-javascript>


2.後臺接收請求與返回值

@Override
	public void serveResource(ResourceRequest resourceRequest,
			ResourceResponse resourceResponse) throws IOException,
			PortletException {
		long id = ParamUtil.getLong(resourceRequest, "id",-1L);
<pre>                response.setContentType("text/html;charset=UTF-8");
                PrintWriter out = null;
                try {
                  out = response.getWriter();
               } catch (IOException e) {
                  e.printStackTrace();
               }
               String result = "這是服務端返回的結果信息:";
               out.println(result);
               out.flush();
               out.close();

  super.serveResource(resourceRequest, resourceResponse);}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章