如何在liferay + springmvc中调用另一个portlet @RenderMapping方法?

时间:2014-07-24 08:33:21

标签: java spring spring-mvc liferay liferay-6

我有2个liferay + springmvc portlet应用程序(2个war文件)。

第一个portlet是Category portlet,它列出了所有可用的类别。单击类别链接时,我将显示产品(默认页面)页面,其中包含portlet-2中所选类别的产品列表。我正在通过PortletSession传达所选类别。

在portlet-2中,用户可以将产品添加到购物车并导航到购物车页面(也在portlet-2中)。

现在,如果用户点击了portlet-1上的另一个类别,那么我想显示产品(默认)页面。但目前正在发生的事情是,当在portlet-1上单击类别链接时,然后重新呈现购物车页面,因为购物车页面现在在portlet-2上处于活动状态,这是预期的。

@Controller
@RequestMapping("VIEW")
public class CatalogListingPortlet {

    @Autowired
    private CategoryRepository categoryRepository;

    @RenderMapping
    public String handleRenderRequest(RenderRequest request, RenderResponse response, Model model) {
        model.addAttribute("categories", categoryRepository.findAll());
        return "categories";
    }

    @ActionMapping(params = "action=showCategory")
    public void showCategory(ActionRequest request, ActionResponse response) {
        String categoryId = ParamUtil.get(request, "categoryId",StringPool.BLANK);
        request.setAttribute("categoryId", categoryId);
        PortletSession portletSession = request.getPortletSession();
        portletSession.setAttribute("LIFERAY_SHARED_categoryId", categoryId, PortletSession.APPLICATION_SCOPE);
    }
}


@Controller
@RequestMapping("VIEW")
public class ProductListingPortlet
{
    @Autowired
    private CategoryRepository categoryRepository;

    @Autowired ProductRepository productRepository;

    @RenderMapping
    public String handleRenderRequest(RenderRequest request, RenderResponse response, Model model) {
        PortletSession portletSession = request.getPortletSession();
        String categoryId = (String) portletSession.getAttribute("LIFERAY_SHARED_categoryId", PortletSession.APPLICATION_SCOPE);
        Category category = categoryRepository.findOne(Long.parseLong(categoryId));
        List<Product> products = category.getProducts();
        portletSession.setAttribute("PRODUCTS", products);
        return "products";
    }

    @ActionMapping(params = "action=addProductToCart")
    public void addProductToCart(ActionRequest request, ActionResponse response) {
        //logic to add the selected product to cart       
    }

    @RenderMapping(params = "action=checkout")
    public String checkout(RenderRequest request, RenderResponse response, Model model) {
        return "checkout";
    }

}

当用户点击portlet-1中的类别链接时,我想在portlet-2中调用@RenderMapping方法。

要明确 CatalogListingPortlet.showCategory()方法,我需要触发 ProductListingPortlet.handleRenderRequest()方法。

我该怎么做?

1 个答案:

答案 0 :(得分:2)

您可以通过IPC(Inter Portlet Communication)在Portlet之间发送数据:

在CatalogListingPortlet中:

@ActionMapping(params = "action=showCategory")
public void showCategory(ActionRequest request, ActionResponse response) {
    QName qname = new QName("http://liferay.com/events","ipc.messsage","x");
    response.setEvent(qname, "some message");
}

在ProductListingPortlet中:

@EventMapping(value ="{http://liferay.com/events}ipc.messsage")
public void receiveEvent(EventRequest request, EventResponse response) {
    Event event = request.getEvent();
    String messsage = (String)event.getValue();
    //process the message
}

portlet.xml中的配置:

<portlet>
    <portlet-name>catalogListingPortlet</portlet-name>
    ...
    <supported-publishing-event>
        <qname xmlns:x="http://liferay.com/events">x:ipc.messsage</qname>
    </supported-publishing-event>
</portlet>
<portlet>
    <portlet-name>productListingPortlet</portlet-name>
    ...
    <supported-processing-event>
        <qname xmlns:x="http://liferay.com/events">x:ipc.messsage</qname>
    </supported-processing-event>
</portlet>

<event-definition>
    <qname xmlns:x="http://liferay.com/events">x:ipc.messsage</qname>
    <value-type>java.lang.String</value-type>
</event-definition>