Liferay7 BPM门户开发之40: Form表单的Action到Render的数据传递
Posted 昕友软件开发
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Liferay7 BPM门户开发之40: Form表单的Action到Render的数据传递相关的知识,希望对你有一定的参考价值。
在Form提交后的变量,很多情况是要展现在jsp页面中,这时Action到Render的变量传递就非常有用。
例如,您在数据库中添加了学生的详细信息。
为了实现这一需求,先创建Form表单(学生的细节,如姓名、出生日期等,通过action url来实现)。
当用户提交表单时,这些数据(学生信息)会在你执行CRUD操作的action方法添加。
添加完成后,需要把一些信息展现到jsp页面。
和v6.2不同的是,action方法里不需要写类似这样的语句:
actionResponse.setRenderParameter("XXX", 变量名);
actionRequest.setAttribute("XXX", 变量名);
举例说明
在Action阶段,我们提交了name变量,然后需要在Render阶段展现在JSP
Java代码
package com.lifiti.portlet; import com.liferay.portal.kernel.log.Log; import com.liferay.portal.kernel.log.LogFactoryUtil; import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet; import com.liferay.portal.kernel.util.ParamUtil; import java.io.IOException; import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; import javax.portlet.Portlet; import javax.portlet.PortletException; import javax.portlet.ProcessAction; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; import org.osgi.service.component.annotations.Component; @Component(immediate = true, property = { "com.liferay.portlet.display-category=category.sample", "com.liferay.portlet.instanceable=true", "javax.portlet.requires-namespaced-parameters=false", "javax.portlet.display-name=com.lifiti.parts Portlet", "javax.portlet.init-param.template-path=/", "javax.portlet.init-param.view-template=/view.jsp", "javax.portlet.resource-bundle=content.Language", "javax.portlet.security-role-ref=power-user,user" }, service = Portlet.class) public class FirstPortlet extends MVCPortlet { private static final Log _log = LogFactoryUtil.getLog(FirstPortlet.class.getName()); @Override public void render(RenderRequest request, RenderResponse response) throws PortletException, IOException { String peopleName = ParamUtil.get(request, "name", ""); String age = ParamUtil.get(request, "age", ""); _log.info("peopleName by ParamUtil in render ==>" + peopleName); _log.info("age by ParamUtil in render ==>" + age); _log.info("age By RenderParameter in render ==>" + request.getAttribute("ageBySetParameter")); request.setAttribute("peopleName", peopleName); super.render(request, response); } @ProcessAction(name = "send") public void ReceiveData(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception { String name = ParamUtil.getString(actionRequest, "name"); String age = ParamUtil.getString(actionRequest, "age"); String phone = ParamUtil.getString(actionRequest, "phone"); actionRequest.setAttribute("phone", phone); actionResponse.setRenderParameter("ageBySetParameter", age); _log.info(" 开始接收数据"); System.out.println("FirstPortlet.java name = " + name); System.out.println("FirstPortlet.java age = " + age); System.out.println("FirstPortlet.java phone = " + phone); } }
jsp
<%@page import="com.liferay.portal.kernel.portlet.LiferayPortletMode"%> <%@page import="com.liferay.portal.kernel.portlet.LiferayWindowState"%> <%@ include file="/init.jsp" %> <p> <b><liferay-ui:message key="com_lifiti_parts_ComLifitiPartsmvcportlet.caption"/></b> </p> <c:set var="submit"><liferay-ui:message key="com_lifiti_parts_ComLifitiPartsmvcportlet.submit"/></c:set> <c:if test="${not empty phone }"> Phone: ${phone} added successfully. </c:if> <c:if test="${not empty peopleName }"> PeopleName: ${peopleName} added successfully. </c:if> <portlet:actionURL var="sendURL" name="send"> </portlet:actionURL> <form action="${sendURL}" method="post" name="fm"> <aui:input name="name" label="Name" id="name"/> <aui:input name="age" label="Age" id="age"/> <input type="text" name="<portlet:namespace/>phone" /> <input type="submit" value="${submit}"></input> </form>
log:
FirstPortlet.java name = 王昕
FirstPortlet.java age = 1
FirstPortlet.java phone = 123
16:16:59,946 INFO [http-nio-8080-exec-7][FirstPortlet:32] peopleName by ParamUtil in render ==>王昕
16:16:59,947 INFO [http-nio-8080-exec-7][FirstPortlet:33] age by ParamUtil in render ==>1
16:16:59,947 INFO [http-nio-8080-exec-7][FirstPortlet:34] age By RenderParameter in render ==>null
可以看到RenderParameter没有效果,所以v7.0中这种语句可以不用写:
actionRequest.setAttribute("phone", phone);
actionResponse.setRenderParameter("ageBySetParameter", age);
实际上,只需要2句就能完成数值传递:
String peopleName = ParamUtil.get(request, "name", "");
request.setAttribute("peopleName", peopleName);
感觉很方便
以上是关于Liferay7 BPM门户开发之40: Form表单的Action到Render的数据传递的主要内容,如果未能解决你的问题,请参考以下文章
Liferay7 BPM门户开发之37: Liferay7下的OSGi Hook集成开发
Liferay7 BPM门户开发之17: Portlet 生命周期
Liferay7 BPM门户开发之3: Activiti开发环境搭建
Liferay7 BPM门户开发之14: Liferay开发体系简介