在Spring MVC portlet中的IPC

时间:2016-01-21 14:16:08

标签: spring-mvc liferay spring-portlet-mvc

我已经创建了一个Spring MVC portlets项目,我正在尝试在两个portlet(SenderPortlet和ConsumerPortlet)之间实现IPC。 一切正常但我需要初始化我放入会话的bean模型,所以我在初始化bean的方法上使用了SessionAttributes注释和ModelAttribute注释。我有两种状态:

  • 在放入ModelAttribute注释之前: 一切正常。我的意思是IPC。
  • 放入ModelAttribute注释后:当我点击触发IPC发件人事件的操作时抛出异常:$select=*

请注意,myAction是映射操作的方法。这个方法设置了IPC事件。这是它的签名:

org.springframework.web.bind.annotation.support.HandlerMethodInvocationException: Failed to invoke handler method [public void com.test.SenderController.myAction(org.springframework.web.bind.support.SessionStatus,javax.portlet.ActionResponse)]; nested exception is java.lang.IllegalStateException: Current request is not of type [javax.portlet.RenderRequest]: com.liferay.portlet.ActionRequestImpl@127e27ff

我无法理解为什么因为我添加了modelAttribute注释而触发了这个异常。

你可以帮我解决这个问题吗?

非常感谢。

的问候,

编辑1 更多代码

ConsumerPortlet.java

@ActionMapping("action")
    public void myAction(SessionStatus status, ActionResponse response)
    {
// some code...
    }

SenderPortlet.java

@Controller(value = "ConsumerPortlet")
@SessionAttributes( value="products" )
@RequestMapping("VIEW")
public class ConsumerPortlet {

@RequestMapping
public String handleRenderRequest(RenderRequest request, RenderResponse response, Model model) {
    try {
        //some code       
    return "page";
}

@ModelAttribute("products")
public List<ProductBean> initilizeProduct(RenderRequest renderRequest){

   //some  code
    return productList;
}


 @EventMapping(value ="{http://liferay.com/events}myEvent")
 public void processEvent(EventRequest request, EventResponse response) throws PortletException, IOException {
 javax.portlet.Event event = request.getEvent();
         String testValue = (String) event.getValue();
        System.out.println("IPC test value: "+testValue);
 }

portlet.xml中

@Controller(value = "SenderPortlet")
@SessionAttributes( value="products" )
@RequestMapping("VIEW")
public class SenderPortlet {

@RequestMapping
public String handleRenderRequest(RenderRequest request,
        RenderResponse response, Model model) {
    return "page2";
}
    @ModelAttribute("products")
public List<ProductBean> initilizeProduct(RenderRequest renderRequest){
      PortletSession ps = renderRequest.getPortletSession();
        List<ProductBean> productList = (List<ProductBean>) ps.getAttribute("products",PortletSession.APPLICATION_SCOPE);
    return productList;
}

 @ActionMapping("myAction")
 public void myAction(SessionStatus status,
         ActionResponse response)
  {
    QName qname = new QName("http://liferay.com/events", "myEvent", "x");
    response.setEvent(qname, "test-value sent");
    status.setComplete();
   }

的liferay-portlet.xml中

    <portlet>
    <portlet-name>sender</portlet-name>
    <portlet-class>
        org.springframework.web.portlet.DispatcherPortlet
    </portlet-class>
    <init-param>
        <name>contextConfigLocation</name>
        <value>/WEB-INF/sender-portlet.xml</value>
    </init-param>
    <supports>
        <mime-type>text/html</mime-type>
        <portlet-mode>view</portlet-mode>
    </supports>
    <portlet-info>
        <title>Sender</title>
    </portlet-info>
    <supported-publishing-event>
       <qname xmlns:x="http://liferay.com/events">x:myEvent</qname>
    </supported-publishing-event>
</portlet>
<portlet>
    <portlet-name>consumer</portlet-name>
    <portlet-class>
        org.springframework.web.portlet.DispatcherPortlet
    </portlet-class>
   <init-param>
    <name>contextConfigLocation</name>
    <value>/WEB-INF/consumer-portlet.xml</value>
</init-param>
    <supports>
        <mime-type>text/html</mime-type>
        <portlet-mode>view</portlet-mode>
    </supports>
    <portlet-info>
        <title>Consumer</title>
    </portlet-info>       
    <supported-processing-event>
        <qname xmlns:x="http://liferay.com/events">x:myEvent</qname>
    </supported-processing-event>
</portlet>
<event-definition>
    <qname xmlns:x="http://liferay.com/events">x:myEvent</qname>
    <value-type>java.lang.String</value-type>
</event-definition>

Liferay 6.2.10.12 Spring 3.0.7.RELEASE

1 个答案:

答案 0 :(得分:0)

由于期望RenderRequest而不是ActionRequest的方法,它会抛出该错误。以下代码可能会解决问题,或者您可以使用通用的Reuqest / Response,即PortletRequest和PortletResponse

public void myAction(@ModelAttribute("products") List<ProductBean> productList, SessionStatus status,
         ActionResponse response)
  {
    QName qname = new QName("http://liferay.com/events", "myEvent", "x");
    response.setEvent(qname, "test-value sent");
    status.setComplete();
   }