struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Frank W. Zammetti" <>
Subject Re: Struts 1 & JSON
Date Fri, 12 Jan 2007 16:35:32 GMT
Hi Priya,

Struts 1.x doesn't natively understand JSON as either input or output. 
That doesn't stop you from doing it yourself though.

The output side is easy: just generate it, either in an Action (probably
not a great idea) or in a JSP (preferred).  For instance, here's a JSP
from a prod app I have here at work that does it:

<%@ page language="java" import="java.util.*,*" %>
  if (company == null) {
    company = "DEFAULT";
  CompanyTabs companyTabs = TabWorker.getTabsForCompany(company);
  "selectTab" : "<%=companyTabs.getSelectedTab()%>",
  "tabs" : [
  List tabs = companyTabs.getTabs();
  for (Iterator it = tabs.iterator(); it.hasNext();) {
    Tab tab = (Tab);
      "label" : "<%=tab.getLabel()%>", "id" : "<%=tab.getId()%>",
      "disabled" : "<%=tab.getDisabled()%>", "href" : "<%=tab.getHref()%>",
      "onClick" : "<%=tab.getOnclick()%>"

After you get done yelling at me for using scriplets :) ... what this is
actually for isn't terribly important, but just for completeness, it's the
response to a request (AJAX) for the definition of what tabs are seen in
the app based on what company the user has selected to work with.  But
that aside, as you can see it's just a simple, plain old JSP that happens
to  be generating JSON rather than HTML.  It's that simple.

If your interested in auto-generation of JSON by the way, it's probably a
bit trickier... you could possibly lift the code I wrote for S2, it might
be similar enough that you could hack it... here's the JIRA ticket with
code attached:

Like I said, it's for S2, but at least conceptually the Results might be
adaptable to S1 as either Commands or a RequestProcessor alteration.

On the input side it's trickier... if your using 1.3.x, you should be able
to write a Command to parse the incoming JSON, after recognizing it as
such, and poplating the form.  If your not using 1.3.x, you could alter
the RequestProcessor to do this... alternatively, if you don't want to
modify Struts itself, don't count on form auto-population and instead
parse the JSON in your Action (probably create your own base Action, call
is JSONAction, and extend your own Actions from that so that the JSON
parsing is in one place).  Another alternative is a filter, but since you
can't add request parameters on the fly, you'll have to add them as
attributes and again forgoe auto-form population.  Yet another approach is
a filter that throws a redirect after parsing the JSON and appending all
the values as a query string to the original URL... that would have the
effect of "restarting" the request processing chain, but now your JSON
data is request parameters and Struts can go on happily doing its normal


Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
AIM/Yahoo: fzammetti
Author of "Practical Ajax Projects With Java Technology"
 (2006, Apress, ISBN 1-59059-695-1)
Java Web Parts -
 Supplying the wheel, so you don't have to reinvent it!

On Fri, January 12, 2007 8:26 am, Priya Khanna wrote:
> Hi All,
>      I am using struts 1. How can I use JSON with that?
> Regds
> Priya Khanna
> **************** CAUTION - Disclaimer *****************
> solely for the use of the addressee(s). If you are not the intended
> recipient, please notify the sender by e-mail and delete the original
> message. Further, you are not to copy, disclose, or distribute this e-mail
> or its contents to any other person and any such actions are unlawful.
> This e-mail may contain viruses. Infosys has taken every reasonable
> precaution to minimize this risk, but is not liable for any damage you may
> sustain as a result of any virus in this e-mail. You should carry out your
> own virus checks before opening the e-mail or attachment. Infosys reserves
> the right to monitor and review the content of all messages sent to or
> from this e-mail address. Messages sent to or from this e-mail address may
> be stored on the Infosys e-mail system.
> ***INFOSYS******** End of Disclaimer ********INFOSYS***
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message