我正在使用 Liferay 门户 5.x。我已经部署了一个简单的 portlet(使用扩展 GenericPortlet 的 jsp 和 servlet)。此 Portlet 将包含用户名和密码字段。我能够在查看模式下看到表单。但是当我提交表单时,该操作将进入 Portlet 类的 processAction(),但用户名和密码请求参数变为空。
有任何想法吗?
Is this a JSR 168 or 286 portlet?
It sounds like you're confident that the processAction()
method is actually being invoked. If not, I'd start by verifying that. The key there would be the action attribute on your form. Are you using the actionURL tag to render the action attribute on the JSP?
That said, whenever I find calls to getParameter()
returning null, it means I've misspelled the parameter, either in the name attribute on the input element in the form, or in the argument to getParameter()
. Also, the parameters are case sensitive.
Any chance you could update your question with the code for the form and the processAction()
method?
Here is an example portlet(JSR 286) that pulls the request parameters (package statement and imports omitted):
public class TestPortlet extends GenericPortlet {
public void init() throws PortletException {
super.init();
}
public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
response.setContentType(request.getResponseContentType());
PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher("/jsp/view.jsp");
rd.include(request,response);
}
public void processAction(ActionRequest request, ActionResponse response) throws PortletException {
System.err.println(request.getParameter("username"));
System.err.println(request.getParameter("password"));
}
}
Here is a sample JSP:
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<div>
<form action="<portlet:actionURL />">
<table>
<tr>
<td>
User Name:
</td>
<td>
<input type="text" name="username" value="">
</td>
</tr>
<tr>
<td>
Password:
</td>
<td>
<input type="password" name="password" value="">
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="submit" name="submit" value="Submit">
</td>
</tr>
</table>
</form>
</div>
我们应该添加作为名称的前缀。
<input type="text" name="<portlet:namespace/>username">
为每个属性添加命名空间很麻烦。避免参数命名空间的另一个解决方案是在 liferay-portlet.xml 中将属性 requires-namespaced-parameters 设置为 false
<portlet>
<portlet-name>portlet-name</portlet-name>
<icon>/icon.png</icon>
<!-- Set this property to false to avoid namespacing parameters -->
<requires-namespaced-parameters>false</requires-namespaced-parameters>
<header-portlet-css>/css/main.css</header-portlet-css>
<footer-portlet-javascript>/js/main.js</footer-portlet-javascript>
<css-class-wrapper>portlet-name-portlet</css-class-wrapper>
</portlet>