My favorites | Sign in
Project Logo
                
Code license: Apache License 2.0
Labels: Struts2, jQuery, ajax, tags
Links:
Feeds:
People details
Project owners:
  obinna
Project committers:
w...@wantii.com

Struts2 jQuery Plugin

The struts2-jquery-plugin is a plugin providing first-class support for AJAX to struts2.

The plugin uses the jQuery javascript library and a very lightweight publish/subscribe framework built on jQuery to implement a set of tags and tag widgets that enable asynchronous loading of UI components and de-coupled interaction between UI components in a simple, standard way.

Check out the project Showcase

Contents



Basic Usage

To use the struts2-jquery tags

  1. Include the http://code.google.com/p/struts2-jquery-plugin/downloads/list in your application classpath
  2. Include the struts jquery tag library on your jsp page (<%@ taglib prefix="sj" uri="/struts2-jquery-tags" %>)
  3. Include the <sj:head> tag in the head of your jsp page
  4. Use the ajax enabled struts2 tags <sj:xxxx> tags.
   <%@taglib prefix="s" uri="/struts-tags"%>
   <%@ taglib prefix="sj" uri="/struts2-jquery-tags" %>
   <html>
      <head>
         <sj:head compressed="false"/>
         <title><s:text name="application.title"/></title>
      </head>
      <body>  
         <sj:div id="header" src="header.action"/>
         <sj:div id="content" src="content.action"/>
         <sj:div id="footer" src="footer.action"/>
      </body>
   </html>



Tags

The following tags/widgets are implemented (click on each for more details and usage):

Basic Usage

Components

Widgets

Features

The tags have a lot of attributes which provide dynamic functionality and provide relevant event/event-handling hooks using the publish/subscribe framework to interact with each other or with custom client code. The easiest way to summarize the functionality provided by the tags is to list the features that the various 'types' of tags provide:

Loading Remote Content (anchor, dialog, div, submit)

'Input' Components (select,textfield,textarea,datepicker)

'Interactive' Components (div,select,submit,textfield,tab,dialog,datepicker)

General Features (all components)



Publish/Subscribe Framework

The plugin uses a very lightweight but powerful publish/subscribe framework which allows for fully decoupled interaction between component events.

The framework comes with out-of-the box hooks for common component events (clickTopics) and event handlers (such as onClickTopcis) which allow the components on the page to interact very flexibly yet simply with each other. For example, to make one div load when another is finished loading or show a dialog if there's a loading error is as easy as follows:

   <%@taglib prefix="s" uri="/struts-tags"%>
   <%@ taglib prefix="sj" uri="/struts2-jquery-tags" %>
   <html>
      <head>
         <sj:head compressed="false"/>
         <title><s:text name="application.title"/></title>
      </head>
      <body>  
         <sj:div id="firstDiv" onSuccessTopics="loadSuccess" onErrorTopics="loadError" src="first.action"/>
         <sj:div id="dependentDiv" showTopics="loadSuccess" src="second.action" cssStyle="display:none"/>
         <sj:dialog id="errorDialog" showTopics="loadError" src="error.action"/>
      </body>
   </html>

Another example, to update the contents of a second select box when the value in a first one changes is as easy as follows:

   <%@taglib prefix="s" uri="/struts-tags"%>
   <%@ taglib prefix="sj" uri="/struts2-jquery-tags" %>
   <html>
      <head>
         <sj:head compressed="false"/>
         <title><s:text name="application.title"/></title>
      </head>
      <body>  
         First Select: <sj:select id="firstSelect" name="firstSelect" onChangeTopics="firstSelectChanged" src="firstSelect.action"/>
         Second Select: <sj:select id="secondSelect" reloadTopics="firstSelectChanged" src="secondSelect.action" elementIds="firstSelect"/>
      </body>
   </html>

The publish/subscribe framework is fully exposed, so users can also implement custom handlers for any topics and can publish and subscribe to any topics using custom code as follows:

   <%@taglib prefix="s" uri="/struts-tags"%>
   <%@ taglib prefix="sj" uri="/struts2-jquery-tags" %>
   <html>
      <head>
         <sj:head compressed="false"/>
         <title><s:text name="application.title"/></title>
         <script type="text/javascript" >

            $.subscribe('loadSuccess', function(event,element) {
               alert('Successfully loaded first div');
               
               $.publish('loadSuccessConfirm');
            });

         </script>
      </head>
      <body>  
         <sj:div id="firstDiv" onSuccessTopics="loadSuccess" src="first.action"/>
         <sj:dialog id="successDialog" showTopics="loadSuccessConfirm" src="error.action"/>
      </body>
   </html>

The above example will:

  1. Display an alert when the div successfully completes loading by subscribing to the 'loadSuccess' topic which the div publishes (set to the div's onSuccessTopics event publisher)
  2. Display the dialog by publishing the 'loadSuccessConfirm' topic (which the dialog's showTopics event listener is subscribed to)

For additional information on using the integrates publish/subscribe framework, see the jquery.subscribe.js source document included with the plugin or visit the jquery subscribe/publish plugin site at http://plugins.jquery.com/project/jQuerySubscribe.

Extending the Framework

The framework provides four explicit extension points:

  1. Implementing custom subscribe/publish topics and handlers
  2. Implementing and binding custom widgets
  3. Implementing custom pre-binding function for existing components
  4. Implementing custom post-binding function for existing components

See Customizations / Extensions for more...









Hosted by Google Code