问题
In portlet What is the best way to read namespace parameter in action method. My form contains
<input id="<portlet:namespace/>param1" name="<portlet:namespace/>param1" value='hello'/>
option1:
request.getParameter(response.getNamespace() + "param1");
option2:
request.getParameter("param1");
option1 does not work in liferay, but does seem will work in websphere. option2 works fine in liferay 6.2. option1 seems to work in before 6.1.
Can anyone please tell me what is the jsr 286 compliant way?
回答1:
As I mentioned in a comment of an answer to this question, the problem is with Liferay 6.2 because IBM WebSphere and previous versions of Liferay are working as expected.
To solve this problem, I added the element <requires-namespaced-parameters>false</requires-namespaced-parameters>
to the liferay-portlet.xml
of the /WEB-INF
directory of the portlet. By doing this, the parameters of the HTML forms are not "namespaced".
Example of /WEB-INF/liferay-portlet.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<liferay-portlet-app>
<portlet>
<portlet-name>Portlet name</portlet-name>
<requires-namespaced-parameters>false</requires-namespaced-parameters>
<instanceable>true</instanceable>
<ajaxable>false</ajaxable>
</portlet>
</liferay-portlet-app>
If you add this element to the liferay-portlet.xml
, the portlet still works correctly in previous versions of Liferay (I tested with versions 5.5 and 6.1). It also works with other portlet contains because they ignore this file.
I claim that Liferay is behaving incorrectly because the JSR-286 spec says the following (top the page 76 of the spec):
If portlets namespace or encode URL parameters or form parameters they are also responsible for removing the namespace. The portlet container will not remove any namespacing the portlet has done on these parameters."
回答2:
There is no "jsr 286 compliant way" You can use both approach. The main purpose of usage tag is pass exact parameter to exact porlet when you have multiple portlet instances on same portal page. In this case same html inputs of different portlets have different names and every portlet will get his own page value.
回答3:
Log statements at the end are from running the code in webphere
<%@ taglib uri='http://java.sun.com/portlet' prefix='portlet'%>
<form name="<portlet:namespace />"
action="<portlet:actionURL windowState='normal'> <portlet:param name='action' value='processAction' /></portlet:actionURL>"
method="post"><br />
<input id="<portlet:namespace/>renderPage"
name="<portlet:namespace/>renderPage"
value='<%=request.getAttribute(Constants.RENDER_PAGE)%>'><br />
</form>
@Override
public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException
{
if (mLogger.isDebugEnabled())
{
mLogger.debug("processAction:: Request Parameter Map:" + request.getParameterMap());
}
// Make all ActionRequest Parameter for RenderRequest
response.setRenderParameters(request.getParameterMap());
if (mLogger.isDebugEnabled())
{
mLogger.debug("processAction:: Latest changes are there");
mLogger.debug(Constants.RENDER_PAGE + "==Namespace=>"+request.getParameter(response.getNamespace()+Constants.RENDER_PAGE));
mLogger.debug(Constants.RENDER_PAGE + "==withoutnamespace=>"+request.getParameter(Constants.RENDER_PAGE));
}
2014-02-12 19:35:23,877 DEBUG ..... renderPage==Namespace=>sites/Component Guide/Home.page 2014-02-12 19:35:23,877 DEBUG ..... renderPage==withoutnamespace=>null
来源:https://stackoverflow.com/questions/21550030/jsr-286-compliant-namespace-parameter