POST parameters not seen by ResourceRequest.getParameter

流过昼夜 提交于 2021-01-28 05:08:37

问题


In Liferay 7 I have a servlet that can serve a resource:

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

I call it with POST, here is the request seen by 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

Log output:

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

Why does getParameter return null instead of "Bonjour"?


回答1:


This will help you.

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



回答2:


Your parameter is text and by default Liferay only takes namespaced parameters, so you have to prefix your params with p_p_id value, ex. nico_my_portlet_MyPortlet_INSTANCE_8bdmSyTiB7R1_text.

Or if you have a requirement to send nonnamespaced params then you have to insert <requires-namespaced-parameters>false</requires-namespaced-parameters> in your liferay-portlet.xml, but be aware about xml order. Reference: https://docs.liferay.com/ce/portal/7.0-latest/definitions/liferay-portlet-app_7_0_0.dtd.html#requires-namespaced-parameters



来源:https://stackoverflow.com/questions/45748298/post-parameters-not-seen-by-resourcerequest-getparameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!