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>