[英]how do I know the url caused the invocation of the @RenderMapping render() method of portlet spring mvc liferay?
[英]How to call another portlet @RenderMapping method in liferay + springmvc?
我有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()方法。
我該怎么做?
您可以通過IPC(內部Portlet通信)在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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.