POST ResourceRequest.getParameter 未看到的参数
POST parameters not seen by ResourceRequest.getParameter
在 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¬e=Bla
日志输出:
Text:null
Parameters:[]
ResourceRequest:com.liferay.portlet.ResourceRequestImpl@7c4acfc2
Private:{}
为什么 getParameter
return null
而不是 "Bonjour"
?
您的参数是 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
这对你有帮助。
UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(resourceRequest);
String text = ParamUtil.getString(uploadRequest, "text");
在 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¬e=Bla
日志输出:
Text:null
Parameters:[]
ResourceRequest:com.liferay.portlet.ResourceRequestImpl@7c4acfc2
Private:{}
为什么 getParameter
return null
而不是 "Bonjour"
?
您的参数是 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
这对你有帮助。
UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(resourceRequest);
String text = ParamUtil.getString(uploadRequest, "text");