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,这是我在测试很久及查资料找到的
执行该方法:
下面是请求的数据及参数
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