Monday 15 March 2010

Integrate struts2-jquery and portlets

In this FAQ we can see how to install the struts2-jquery plugin, but we have to make some little changes if we are developing porlets:

1.Add the next servlet-mapping to the web.xml file:
<servlet>
        <servlet-name>struts2servlet</servlet-name>
        <servlet-class>org.apache.struts2.dispatcher.ng.servlet.StrutsServlet</servlet-class>
</servlet>
<servlet-mapping>
        <servlet-name>struts2servlet</servlet-name>
        <url-pattern>*.action</url-pattern>
</servlet-mapping> 
<servlet>
        <servlet-name>struts2Resources</servlet-name>
        <servlet-class>org.apache.struts2.dispatcher.ng.servlet.StrutsServlet</servlet-class>
</servlet>
<servlet-mapping>
        <servlet-name>struts2Resources</servlet-name>
        <url-pattern>/struts/*</url-pattern>
</servlet-mapping> 

This could be done by a filter-mapping too, but with filter-mapping we can have some problems in a Websphere server.

2.Define some pakage in the struts2 config to manage the ajax request
<package name="ajax" extends="json-default" namespace="/ajax">
...
</package>

3.To make a valid ajax request from the portlet, all the request must be as the next examples:
<form id="compraVentaForm" action='<s:url value="/ajax/compraVentaSave.action"/>'  method="post">
...
</form>
<s:url id="ordenesActivasResultado" value="/ajax/ordenesActivas.action"/>
<sj:div href="%{ordenesActivasResultado}" indicator="indicator" onCompleteTopics="completeDiv" reloadTopics="reloadDiv">
 <img id="loading" src="<s:url value='/img/ajax-loader.gif'/>" alt="Loading..."/>
</sj:div>

2 comments:

  1. Why not using the Struts2 Portlet Dispatcher located in portlet plugin?


    Struts2PortletDispatcherServlet
    org.apache.struts2.portlet.dispatcher.DispatcherServlet

    ReplyDelete
  2. Because in the initial load of the portlet, the portlet is rendered by the Struts2PortletDispatcherServlet (that is defined in the portlet.xml) but all the dynamic content (the ajax) is rendered by the servlet: http://developers.sun.com/portalserver/reference/techart/ajax-portlets/fig2.jpg

    ReplyDelete