将值从操作阶段传递到portlet中的呈现阶段

时间:2015-04-03 17:47:33

标签: controller liferay portlet spring-portlet-mvc

我想将字符串值从动作阶段传递到渲染阶段。在这两个教程tut1tut2中,我说我可以通过以下方式完成:

在action()期间,您可以调用ActionResponse.setRenderParameter()方法来设置任何String值,并使用以下渲染调用中的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>

1 个答案:

答案 0 :(得分:1)

你引用正确

  

...使用RenderRequest 检索相同内容。 getParameter()

然后你使用

request.getAttribute("par1")

只需按照教程操作,然后使用getParameter()获取您的价值