liferay的MVCPortlet的ajax请求

来源:互联网 发布:系统内置软件卸载 编辑:程序博客网 时间:2024/05/21 14:12
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.后台接收请求与返回值

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



0 0
原创粉丝点击