Using collections as parameters in Jasper from Grails - grails

I am trying to learn how to get Jasper running from within a grails app using the jasper plugin. On the plugin directory page [ http://grails.org/plugin/jasper ] there is a fairly in-depth tutorial. Which is supposed to show you how to extend a 'racetrack' example to use the jasper plugin. The problem I'm finding is that since the 'registrations.jrxml' is not shown on the tutorial, nor does it come with the source download of the plugin -- its impossible for me to figure out how to setup a jrxml file to work with data being sent to it from grails.
Right now, my application's controller is sending an ArrayList of HashMaps back to the view, which are then turned into html tables using "g:each". It's not a simple representation of a domain, the HashMaps are constructed from several different datasources. I need to also get this data exported through Jasper to an XLS file. Right now I can send the jrxml the ArrayList as a parameter, but I am going nowhere when it comes to correlating each member of the List (a Row) to a Detail in the jrxml.
So for the simplest case say I have a controller that provides a view with an ArrayList of Strings
def index = {
def people = ["Donald", "Richard", "Raymond", "Samir", "Cyrus"]
[people: people, guy: "Frank"]
}
and a view that passes the ArrayList to Jasper as a parameter.
<g:jasperReport format="PDF" jasper="don2" name="Don Report">
<input type="hidden" name="name" value="${guy}"/>
<input type="hidden" name="list" value="${people}"/>
</g:jasperReport>
What would I need to do in a jrxml to make use of the ArrayList as my datasource?
Don

You need JRBeanCollectionDataSource. Construct that in a controller (or in a custom tag of yours). You can't do it directly in the gsp.

Related

GSP Access to Controller Results

I am using the Dojo plugin for Grails to populate a dojox.grid.DataGrid object. Right now my controller action associated with this grid renders the JSON that defines the grid contents. That works just fine as long as I predefine the columns in the GSP. However, my application needs to be able to set the number of columns (and their associated names) dynamically based on some database query results. The JSON format for this grid does not include the column names so I cannot use a g:each tag in my GSP to iterate through the names.
I wanted to do something like this but am unable to do so:
<dojo:grid controller='foo' action='getGridData' ...>
<g:each in=${columns}>
<dojo:col width="15%" name="{it}" field="{it}">{row.{it}}<dojo:col/>
</g:each>
</dojo:grid>
I gave the specific example of the Dojo plugin for background but I would like to extend this to a more general question about Grails. If I am constructing a GSP element by a controller action that renders JSON how do I access other things in the controller for the construction of nested elements? Is this even possible? I am still new to web development so I am sure I am misunderstanding some of the glue between these components.
The main thing I see wrong with your code is you're trying to reference the columns data incorrectly.
<g:each in="${columns}">
<dojo:col width="15%" name="${it}" field="${it}">{row.{it}}<dojo:col/>
</g:each>
Note the quotes around the in= value and the dollar sign used to reference the it variable.
Regarding the last bit
{row.{it}}
I'm unclear on what row is in this case. So I'm not sure how that needs to be fixed, but maybe this gets you moving in the right direction.
You generally don't render a GSP and construct JSON in one request. What will happen here is you render your GSP which takes all your taglib code and produces HTML/JavaScript. Then, once the browser renders it, an AJAX call takes place to fetch the JSON.
Remember that by the time the GSP reaches the browser, it is just HTML.

Using Ruby/Rails To Programmatically Post Form Data To Another Site

I'm trying to figure out a way to automate posting data on this site which does not have an API and thankfully not a captcha as well.
For example there is a form here => http://www.austinchronicle.com/gyrobase/EventSubmission
By examining the form I can figure out that the Name text box is setup as follows......
<input type="text" name="Name" id="EventName" value="" class="rejectPipe">
Using Ruby/Rails is there a way I can programmatically POST to the form on that page through the controller or in a rake task?
I sniffed out some links like => http://biodegradablegeek.com/2008/04/how-to-post-form-data-using-ruby/
and that seems to sort of explain the basic premise but what about inserting data into the time control or the drop down select boxes on another site? Or pretty much anything more complicated than inserting a string into an input box
Your best bet is to use something like Mechanize. I've written a blog post on a related subject (uploading data with Mechanize) : http://davidsulc.com/blog/2011/11/13/uploading-data-using-mechanize/
Alternatively, if you want to see the page while information is being entered, you could user Selenium http://davidsulc.com/blog/2011/11/27/automating-web-site-interactions-with-selenium/
You can use Typhoeus
For datetime selects you need conform to Rails protocol. Just pop open the source of the page and look at the names of their elements and use the same structure when posting. Do the same with select boxes

Is there a Grails equivalent for the jsp form tags?

I have an (as yet) simple Spring 3 MVC web-app using JSP as the view technology. I am considering rewriting it in Grails before I get too far along.
One thing I like about Spring is the "form" tags provided in the spring-form.tld tag-library. Given a model property "myFormModel" with the "myProperty" property, this allows me to write something like: -
<form:form commandName="myFormModel">
<form:input path="myProperty" cssErrorClass="error"/>
The key here is that the form:input tag automatically does all the binding to the property in the command object, so generating (roughly) in HTML: -
<form>
<input type="text" name="myProperty" value="xyz"/>
Spring MVC will bind the form parameters to the class and pass the object to the controller. Less to go wrong.
(Please excuse the JSP and HTML, it's indicative, possibly slightly incorrect)
As I understand the GSP form tags: -
<g:form name="myForm" url="[controller:'myController', action:'foo']">
<g:textField value="${myFormModel.myProperty}" class="${...blah to select error}"/>
I cannot specify a "path" attribute: I must manually generate the name. When the path becomes complex (say a property of a item from a list), this can become hairy and noisy.
I cannot automatically specify both "normal" and "error" CSS classes: I must put EL into the <input> class attribute. Messy!
I must admit I am surprised that GSP is (what I consider) behind Spring, I thought it was all about making the obvious things simple and the hard things possible. Easy-to-read/implement forms would seem a no-brainer.
So, my questions: -
am I missing something?
should I (and can I) use the spring-form.tld in my GSP?
It makes me wonder what other gotcha's I will run into...
The beanFields plugin does everything the Spring form tags do and more. It makes working with forms about as concise as possible.

Grails/Groovy taglib handling parsing dynamically inserted tags

Is there a way to have a custom taglib operate on data loaded in a .gsp file such that it picks up any tags embedded in the data stored in the database. For instance, let's say I'm doing:
<g:each in="${activities}">
<li>${it.payload}</li>
</g:each>
And inside the payload, which is coming from the database, is text like
"Person a did event <company:event id="15124124">Event Description</company:event>"
Can you have a taglib that handles company:event tags on the fly?
You could write a custom tag which uses the GroovyPagesTemplateEngine to process the text and write it to the output stream. I think you can get an instance of the TemplateEngine injected into your tag from the applicationContext.
I don't have any example code sorry,
cheers
Lee

using struts-tags in struts 2 is mandatory. is it?

Is it mandatory in struts2 to use struts-tags. I mean can't we use following:
<td bgcolor="DEDBC6"><div align="left"><font color="#000000"><strong>
Employee Name * :</strong></font></div></td>
<td><input name="empname" type="text" id="empname" size="32"></td>
instead of following:
<s:textfield label="Employee Salary " name="empsal" required="true"/>
I had tried both but when i used 1st i didn't get validation message i.e "empname is required" that i wrote in action-validation.xml although validation is working well.
please comment ?
No, it is not mandatory. In fact a lot of struts2 users are sufficiently dissatisfied with OGNL and choose to use regular HTML instead.
But using the standard HTML tags has the drawback of loosing some functionality, after all that's why the custom tags are there in the first place. It is possible to get validation through validate methods on the controller class even with standard HTML.
If you are just starting out with the framework I suggest you learn the struts tags properly before going off the beaten path.
Is it mandatory in struts2 to use struts-tags.
No, but if you aren't using tags at all then you're not really getting very much out of Struts as a framework. Might as well do it yourself.
i didn't get validation message
If you're using your own markup you'll have to tell it to display the error message. eg.:
<s:fielderror><s:param>empcode</s:param></s:fielderror>
please comment ?
Please stop asking the same questions over and over again.
I don't see why you need any tags at all. Couldn't you just use Struts to generate JSON result types and then use calls to those in your otherwise static jsp pages using JavaScript and JQuery?

Resources