liferay 使用ajax请求

前端之家收集整理的这篇文章主要介绍了liferay 使用ajax请求前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

view.jsp


<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

<script type="text/javascript" src="/html/js/jquery-easyui-1.3.6/jquery.min.js"></script>

<portlet:defineObjects />
<portlet:resourceURL id="rentalsUserListURL" var="rentalsUserListURL"></portlet:resourceURL>


<script>
var rentalsUserListURL = "<%=rentalsUserListURL%>";
var namespace = '<portlet:namespace/>';

function testAjax2(){

$.ajax({
url : rentalsUserListURL,
type : "post",
dataType : "json",
data : {
<portlet:namespace/>id : 100,
<portlet:namespace/>version :'house'
},
success : function(versionData) {
console.info(versionData);
},
error : function(XMLHttpRequest,textStatus,errorThrown) {
console.info("测试失败: ");
}
});

}
</script>


MVCPortlet.java


@Override
public void serveResource(ResourceRequest resourceRequest,
ResourceResponse resourceResponse) throws IOException,
PortletException {

try {
super.serveResource(resourceRequest,resourceResponse);
HttpServletRequest request = PortalUtil.getHttpServletRequest(resourceRequest);
// 获取用户信息
User user = PortalUtil.getUser(request);
System.out.println("userId = " + user.getUserId() + "userName = " + user.getScreenName());

PrintWriter write = resourceResponse.getWriter();
String resourceID = resourceRequest.getResourceID();

System.out.println("ResourceID = " + resourceRequest.getResourceID());
System.out.println("id = " + resourceRequest.getParameter("id"));
System.out.println("version = " + resourceRequest.getParameter("version"));

// Map map = resourceRequest.getParameterMap();
//Map map = request.getParameterMap();
//
//for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();) {
//Map.Entry entry = (Entry) iterator.next();
//System.out.println("Key = " + entry.getKey() + ",Value = " + entry.getValue());
//}

// 获取参数键值名称
Enumeration<String> enu = request.getParameterNames();
while (enu.hasMoreElements()) {
// 取得参数名称列表
String paramName = (String) enu.nextElement();
// 处理本请求参数以及发送给第三方服务器的参数
String paramValue = request.getParameter(paramName);
System.out.println("Key = " + paramName + ",Value = " + paramValue);
}

System.out.println("id = " + request.getParameter("id"));
System.out.println("version = " + request.getParameter("version"));

if (resourceID.equals("rentalsUserListURL")){
write.write("{\"message\":\"maomao\",\"success\":\"true\"}");
}
} catch (PortalException e) {
e.printStackTrace();
} catch (SystemException e) {
e.printStackTrace();
}
}


说明:

1.Portletextends MVCPortlet重新serveResource方法

2.ajax请求参数

data : {
<portlet:namespace/>id : 100,
<portlet:namespace/>version :'house'
},

红色部分是必须加上的,后台才能获取id和name参数,否则获取全是null,这是我在测试很久及查资料找到的


执行该方法

下面是请求的数据及参数


Request URL:


Query String Parametersview sourceview URL encoded

p_p_id:rentalsusermanagement_WAR_ossportlet
p_p_lifecycle:2
p_p_state:normal
p_p_mode:view
p_p_resource_id:rentalsUserListURL
p_p_cacheability:cacheLevelPage
p_p_col_id:column-1
p_p_col_count:1


Form Dataview sourceview URL encoded

_rentalsusermanagement_WAR_ossportlet_id:100
_rentalsusermanagement_WAR_ossportlet_version:house


后台打印的数据


userId = 10436userName = kouht
ResourceID = rentalsUserListURL
id = 100
version = house
Key = id,Value = 100
Key = version,Value = house
id = 100
version = house




@H_403_218@ 
原文链接:https://www.f2er.com/ajax/162682.html

猜你在找的Ajax相关文章