在 Portlet 中将值从操作阶段传递到呈现阶段

Passing value from action phase to render phase in portlets

我想将字符串值从动作阶段传递到呈现阶段。在这两个教程中 tut1 and tut2 说我可以通过 :

在 action() 期间,您可以调用 ActionResponse.setRenderParameter() 方法来设置任何字符串值,并在以下渲染调用中使用 RenderRequest.getParameter() 检索相同值。

这是我的视图模式的控制器

GraphViewController.java

package graphui;

import com.liferay.portal.kernel.util.ParamUtil;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.ModelAndView;
import org.springframework.web.portlet.bind.annotation.ActionMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;
import org.springframework.web.portlet.bind.annotation.ResourceMapping;

/**
 *
 * Controller for VIEW mode of portlet.
 */
@Controller("graphViewController")
@RequestMapping(value = "VIEW")
public class GraphViewController{

    private static final Logger logger = Logger.getLogger(GraphViewController.class);

    @ActionMapping(GraphUIConstants.SUBMIT_FORM)
    public void handleActionRequest(ActionRequest request, ActionResponse response)throws Exception {

            logger.info("action phase executed");

            response.setRenderParameter("par1", "par1");

    }

    @RenderMapping
    public ModelAndView handleRenderRequest(RenderRequest request, RenderResponse response, ModelMap model) {

      logger.info("render phase executed");  

      logger.info(request.getAttribute("par1"));

    }

}

我的日志结果:

17:43:06,196 INFO  [GraphViewController:56] render phase executed
17:43:06,205 INFO  [GraphViewController:58] null
17:43:09,491 INFO  [GraphViewController:40] action phase executed
17:43:09,498 INFO  [GraphViewController:56] render phase executed
17:43:09,499 INFO  [GraphViewController:58] null

portlet.xml

    <?xml version='1.0' encoding='UTF-8' ?>
<portlet-app
    xmlns='http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd'
    xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
    xsi:schemaLocation='http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd'
    version='2.0'>

    <portlet>
        <portlet-name>GraphUI</portlet-name>
        <portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>
        <init-param>
            <name>contextConfigLocation</name>
            <value>/WEB-INF/GraphUI-portlet.xml</value>
        </init-param>
        <supports>
            <mime-type>text/html</mime-type>
            <portlet-mode>VIEW</portlet-mode>
            <portlet-mode>EDIT</portlet-mode>
        </supports>
        <supported-locale>en</supported-locale>
        <resource-bundle>content.graph</resource-bundle>
    </portlet>


</portlet-app>

您引用正确

...retrieve the same using RenderRequest.getParameter()

然后你使用

request.getAttribute("par1")

只需按照教程使用 getParameter() 即可获得您的价值