Struts 2.x and Json Plugin

One of the things that I love about the Struts 2.x framework is the amount of plugins you can use with it. I just recently used the JSON plugin in a project and was pleased at how easy it was to setup and use.

First you need to download the Struts 2.x Json plugin from here|http://code.google.com/p/jsonplugin/downloads/list .

Once you have downloaded the jar file put the file in the WEB-INF/lib folder or add it to your projects classpath. Now that the jar is added to the project, you can create actions that will return a JSON object.

To create a action you need to set extend attribute of the package in the struts.xml file to be json-default. You also must return a result type of "json". The example of this is shown below.

  <package name="json" extends="json-default">

	  <default-interceptor-ref name="completeStack" />

	  <!-- Add packages here -->

	  <action name="vendorNbrCheck" class="com.invtracker.web.actions.VendorNbrJsonAction">
	  	<result type="json"/>
	  </action>

  </package>

In the above action "vendorNbrCheck", the system will check to see if the vendor number is found and returns the vendor name for a found vendor number. The action is coded as normal, and the getter/setter variables are used to get data when the action is called and set data into the JSON object.

public class VendorNbrJsonAction extends ActionSupport {

	/** The log. */
	private static Log log = LogFactory.getLog(VendorNbrJsonAction.class);

	/** The vendor nbr. */
	private int vendorNbr;

	/** The vendor name. */
	private String vendorName = null;

	/** The code list dao. */
	private CodeListDAO codeListDao;

	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	public String execute(){

		log.debug("execute() in VendorNbrJsonAction");
		log.debug("vendor number is " + vendorNbr);

		String vendName = null;

		try {
			vendName = codeListDao.getVendorName(vendorNbr);
		} catch (Exception e){
			log.error("ERROR: Getting Vendor Name e " + e);
		}

		if (!GenericValidator.isBlankOrNull(vendName))
		{
			vendorName = vendName;
		}

		return SUCCESS;
	}

Now that the action is returning JSON data, and is setup to use the JSON plugin in Struts 2.x, we need to access this action from the web page. To do this you need to make sure that a few things have been added to the page. First you need to make sure that the following struts header tag for ajax has been added to the page. This tag will allow include the necessary Dojo|http://dojotoolkit.org/ library's needed to call the action using ajax.

<s:head theme="ajax"/>

Now the final piece is to call the "vendorNbrCheck" action. To do this you will need to use the following javascript:

<script language="javascript">
  dojo.require("dojo.io.*");
  dojo.require("dojo.event.*");

  function onchange_vendornbr() {
  	var params = new Array();
 	params['vendorNbr'] = document.getElementById("inv.vendorNbr").value;
 	var bindArgs = {
  		url: "vendorNbrCheck.action",
  		error: function(type, data, evt){
          //alert("error");
         },
  		mimetype: "text/json",
  		content: params
 	};
 	var req = dojo.io.bind(bindArgs);
 	dojo.event.connect(req, "load", this, "populateVendorName");
  }

  function populateVendorName(type, data, evt) {
  	if (data.vendorName != null)
  	{
  		document.getElementById("inv.vendorName").value = data.vendorName;
  	}
  }

</script>

This javascript will set the value of the inv.vendorNbr field into the params Array with a key of vendorNbr. This key matches the setter for the action and the value will be available to the action once it is called. Then you use a Dojo bind, which will call the action url, pass the parameters set in the Array to the action, and set the action data return type to be of type JSON. After the bind is setup, you can run a connect to call the action, and on successful return, the populateVendorName javascript function will be called. The json object returned can be access in the populateVendorName function as part of the data object.

I find that once you get everything setup, that things are quick and easy to use.

Posted on May 14, 2008 by Mike Jennings in Java | 1 Comments | Permalink



Comments:

k

Posted by 12.146.79.173 on May 26, 2009 at 05:40 PM EDT #

Post a Comment:
  • HTML Syntax: Allowed