ResourceRequest.getParameter看不到的POST参数

时间:2017-08-18 03:56:38

标签: post xmlhttprequest liferay portlet liferay-7

在Liferay 7中,我有一个可以为资源提供服务的servlet:

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

    // Get the parameter
    log.info("Text:" + resourceRequest.getParameter("text"));

    // Some debug info
    log.info("Parameters:" + Collections.list(resourceRequest.getParameterNames()));
    log.info("ResourceRequest:" + resourceRequest);
    log.info("Private:" + resourceRequest.getPrivateParameterMap());
}

我用POST调用它,这是Wireshark看到的请求:

POST /web/guest/home?p_p_id=nico_my_portlet_MyPortlet_INSTANCE_8bdmSyTiB7R1&p_p_lifecycle=2&p_p_state=normal&p_p_mode=view&p_p_resource_id=addResUrl&p_p_cacheability=cacheLevelPage HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:54.0) Gecko/20100101 Firefox/54.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
x-requested-with: XMLHttpRequest
Referer: http://localhost:8080/
Content-Length: 53
Cookie: COOKIE_SUPPORT=true; USER_UUID=3553584d6b565a536f614863774a6b486d39477a4f486671726b696452533248654c4c62486a3669376e303d; SCREEN_NAME=5850595471493466547236592b557174726e4e5a39413d3d; LOGIN=74657374406c6966657261792e636f6d; GUEST_LANGUAGE_ID=en_US; COMPANY_ID=20116; REMEMBER_ME=true; ID=6b76544c52356b5464343664464e4e6d784a30696e673d3d; PASSWORD=5850595471493466547236592b557174726e4e5a39413d3d; JSESSIONID=D910A4DFF4E53D40BFF02EABE79D07EF
DNT: 1
Connection: keep-alive

text=Bonjour&note=Bla

日志输出:

Text:null
Parameters:[]
ResourceRequest:com.liferay.portlet.ResourceRequestImpl@7c4acfc2
Private:{}

为什么getParameter会返回null而不是"Bonjour"

2 个答案:

答案 0 :(得分:3)

这会对你有帮助。

UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(resourceRequest); 
String text = ParamUtil.getString(uploadRequest, "text"); 

答案 1 :(得分:1)

您的参数为text,默认情况下,Liferay仅采用命名空间参数,因此您必须在序号前加p_p_id值,例如。 nico_my_portlet_MyPortlet_INSTANCE_8bdmSyTiB7R1_text

或者如果您需要发送非命名空间参数,则必须在liferay-portlet.xml中插入<requires-namespaced-parameters>false</requires-namespaced-parameters>,但要注意xml顺序。参考:https://docs.liferay.com/ce/portal/7.0-latest/definitions/liferay-portlet-app_7_0_0.dtd.html#requires-namespaced-parameters