研究:Liferay研究(五)Liferay中页面Ajax的简单调用

在Liferay中已经把jQuery1.3加入了,而需要调用的话该怎么样调用?

下面以一个简单的加入收藏为例子:

在jsp页面中引入portlet标签

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

在Jsp中加入如下Javascript代码

<script language="javascript">

	var url = "<%=request.getContextPath()%>/portlet_action/islike/view?para=favor";



	function insertfavorite<portlet:namespace />() {

		jQuery.ajax(

			{

				type: "get",

				url: url,

				dataType:"html",

				success: function(msg){

					if(msg=='isok'){

						alert("收藏成功");

					}

					if(msg=='favorited'){

					alert("您已经收藏了");

					}

				},		

				error: function(){

					alert("收藏失败");

				}

			}

		); 

	

	}

</script>

<a href="#" title="收藏该应用"> <img
                                                src="<%=request.getContextPath()%>/resources/images/button_fav.gif"
                                                onclick="insertfavorite<portlet:namespace />()" /> </a>

在action中

if (StringUtils.isNotEmpty(send)) {
            // ajax
            if (StringUtils.equals("favor", send)) {
               PrintWriter out = response.getWriter();
                response.setContentType("text/html; charset=utf-8");
                response.setCharacterEncoding("utf-8");

                               System.out.println("Favaid=====>" + aid);
                String title = request.getParameter("title");
                System.out.println("title====>" + title);
                boolean check = productinfoService.checkFav(userid, sid);
                if (check)
                    out.print("favorited");
                else
                    out.print("isok");

                return null;
            }

}


则可以调用相应的Ajax

你可能感兴趣的:(liferay)