Project setup

Spring Roo brings alternatives for setting up a project: a) Entering project using a script file or b) Creating the database first and reverse engineering it using Roo DBRE commands.

For this article I am using option a), via an script file. Option b) is specially convenient for projects with an database already in place. I’ll vist Roo DBRE in a coming article in the near future.

Script File

Notice content field is of type blob. This field would have to be entered manually into the entity class. You can use the IDE of your choice. I use STS. Either you use STS or the native Roo shell, the project would need to be synced by Roo after the change. More on this later.

Please review the seminal article for more information about the example entity fields. All highlighted code is manually added to the original file created by Roo. Why I added field size and url directly into the entity class?. I could have entered referred fields using the shell too. Right?. I did it in purpose to mentioned that Spring Roo is a round-trip tool. There are two more fields not shown in the entity class. They are the id and version fields, Roo automatically handle them using an ITD. Details about round-trip capabilities and how Roo uses ITDs, please visit Spring Roo website at http://www.springsource.org/roo. One more thing about the blob field. DBMS have different type of blobs. Verify that your project database is using the right one for your purposes.

As mention before: always make sure that Roo synced your latest changes. That would save you a lot of confusion.

It is a good time to deploy the application and verify that everything is working find to this point.

Required Source code changes

Well, to this point Spring Roo has done a lot for us. Properly setup the project. Created a Maven pom.xml with all required dependencies -well, almost; more on this below-. Generate all the code necessary for having a working web app. If this not enough, Roo is there in the background monitoring the project against any possible change.

With exception of the blob field -content- all the other fields are ready to go. Roo took care of them. In order to save/retrieve data from the blob field, we need to modify code in the client and in the server side for allowing file uploads.

Server Side Changes

Basically we need code that allows us to save/retrieve documents that include a blob field. The documents are entered using a web form. Typically Roo by default, generates client and server side code for having CRUD operations for every entity. I am not allowing updates in this version of the application. So, I disabled it, by setting the update=false in the @RooWebScaffold annotation. Please make sure next code is included in the DocumentController project file.

The code above allows CRUD operations with all the document fields including the blob. Very-very important: all this source code must be placed in the DocumentControler.java file to avoid been removed by Roo when the project is synced for changes.

Client Side Changes

On client side, I created a create custom form. Please replace the content of the create.jspx, with:

Create.jpsx Form

As you can see, the form depends on two custom tagx files -multi.tagx and file.tagx- not included in the Spring Roo tag library. These two tags handle the blob field input.

Create Form

Show.jpx Form

This form depends on a custom tag frame.tagx. Basically is a iframe html for rendering images.

Show Form

List Form

Little issue with a missed dependency: When you deploy the app -after the file upload changes- you will find a missing dependence error.

This is it!.

Download Source

Conclusion

Well, the Reader has been exposed to two pretty close but at the same time different ways of implementing a particular web application project. Both are using the same high quality open source code Java-based development stack: Spring 3.X. I believe both ways are good and valid. There is a significant difference in Spring Roo approach though. Roo advocates for removing the “unnecessary abstractions” from web development. So, you won’t find Daos or Services classes in a typical Roo project. This is totally fine with me. However, it is a subject still in revision by the community at large. Not to mention that nothing stops you for implementing them if you need/want them. If you allow me one final point, I like better Roo approach because it gives me more time for thinking and figuring our how to solve the problem.

I saw u r message but i want retrive image from database and display in jsp using form,DAO,DO i am tring to display but some thing is wrong .i got the imgdata but how can i display in jsp ,i saw google they r using but they r also cant display img so please send me some source code to me plz help me?

Dear JD, thanks for a great article. Just one question: with this approach you load the entire BLOB up in RAM before sending it, right? While this works great for small images, larger files will be a problem. Do you have any recommendation on how to keep the memory footprint of the request low while keeping the simplicity you’re offering?

Wow thank YOU. This is the best compliment you can give… Simplicity! I like it.
On your question: You have to use the file system for the case you mention.
I will publish such implementation on my website soon.http://pragmatikroo.blogspot.com/

I understand your disappointment with Roo… I have my concerns too; but they are oftotally different nature: I am more concern on the human component of SR than in the software part.

I bet you; you would have a 180 degrees opinion about SR-or at least much+much better- if there would be available sources and materials that show you how-to apply SR on solving real-world-java-web-development. BTW this is what I’ve been successfully doing with Roo.
Please Google: “Spring Roo Jose Delgado” and you will see what I am taking about.

I have the perception that SR Management believes that the community should and would self-satisfy the own requirements out of what they are delivering. The reality is: This not happening. Consequently Developers get disenchanted with Roo.

On the other hand; I believe Rails, Grails and the Agile stuff are kind-of placebos: they don’t solve the problem but make you fell well -for sometime-.

Thank you for bringing it to my attention…
The source code is coming from compiled/executable code. So the issue found happen when the article was out to my hands. I don’t want to sound defensive or blame others. I only say that is a weakness of the open source ecosystem and it shouldn’t be a big deal.

Couple of question on the applicability of Model2Roo. Roo creates only scaffold-ed implementations which will be changed by the developer all the time. 1)Is Model2Roo able to reflect the introduced changes to the roo script and to uml project?

2)Most of the time you use Roo DBRE tool to create the project entities from a legacy database. How do you apply Model2Roo in this case?

I am sorry that comments on my blog currently malfunctions. I tried to find out what the error meant, and found some indications it could be recaptcha. I think recaptcha has changed API, so I regenerated for my domain, but the error persisted. So for now I have disabled the recaptcha – hope I will not be spammed too much, otherwise I will have to disable it again until I have time to solve the problem.

About Model2Roo, I don’t think you can roundtrip back. I think more of it as a nice thing to have if you have an existing EMF model, or if you prefer to use the EMF tools to get an initial shot at your model.

For my projects currently I have staid with just. roo. The quality (of documentation) for generated GWT from roo is too low though, so currently I am just hooking up smartGWT widgets directly to the database created by roo. For prototyping this seems like the good compromise between getting help from tools and getting things done without focusing energy on the tools beyond the point where they are helpful.

I agree your error in the blog was very minor :) just wanted to help you by pointing it out.

Btw. I have now successfuly applied your image upload to a prototype of mine — thanks a lot for sharing. I wonder whether it would be easy to wrap it into a roo extension, but that will have to wait to be consider for next time I need i.

As it is, the implementation shown could handle small to medium size docs…
In other to handle really big size docs it would have to be modified a little bit to use the filesystem instead the database to store it.

Great tutorial but what about the option B?
Could you tell me how to do it? cuz i got a database schema and add a field named “picture” in one table but when I did reverse engineering it added that field on the form but can’t upload any pic.

The main purpose of my tutorial is to contrast traditional Spring web development vs. Roo development. In order to that I used the referred “original” tutorial referred at the top.
I hope I met my purpose.

At the time of upgrading the tutorial for handling big blog is will include your point.

This is a great article. You mention the commons-io dependency error at the end. My problem is that when I add this dependency (copy directly into the pom.xml or add from STS GUI context menu), nothing happens.

How can I get STS to download and enlist the new dependency with the existing ones?
Thanks.

Hi, Thanks for the Tutorial.
I run it successfully. Here I have question about update Blob in DB by Spring Roo. You disable the update in above code. Bu I am interested in how to update Blob in DB by Spring Roo. DO you have any investigation on update?

I have implemented this solution in a project, using ROO 1.2.0.RC1 and all seems working (I have noticed a —ClientAbortException: java.net.SocketException: Software caused connection abort: socket write error— but write and read worked well, anyway); now I have done the migration from ROO 1.2.0.RC1 to ROO 1.2.0.RELEASE (the current General Available) and I have noticed that I cannot write to or read from database; please, let me know if there are solutions to this…. the error is always —ClientAbortException: java.net.SocketException: Software caused connection abort: socket write error— created by at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:358)…

Thank you for the example. First I couldn’t display the image. After some trying (and failing) the following change to the controller was all I needed. In your example model.attribute() Document.findDocument(id) was the second argument.

This is on os x lion, roo 1.1.5, I have changed the class from Document to File:

Many thanks Jose for sharing with us!
I really appreciate your efforts for the Roo community.

Just tryed it and it works like a charm.
I’m still having problems with the iframe…I don’t know it so I’ll need to study it a little bit. (the HTML code after the is not rendered from the browser, even if it is in the page).

The issue is the same on any browser (Firefox 10, Chrome 17, IE 9), but I built another tagx file with an img and a dojo LightBoxNano, which suits my needs better.
So I didn’t study the iframe yet :-)
For your information, the issue was that every HTML markup that follows the iframe wan not visible: nor the other fields of Document (I showed them after the url-iframe) and the footer neither.
Maybe I messed things somehow – I cut and paste your code into a sample project, to better follow your tutorial.
B.Roogards to you
Vito

Thanks for your tutorial. I almost bring it work in my Spring Roo project. However, when I click create New Document link, I got the following error.
SEVERE: Servlet.service() for servlet turbops threw exception
org.xml.sax.SAXException: No tag “multi” defined in tag library associated with uri “urn:jsptagdir:/WEB-INF/tags/form”

Hi jD, thanks very much for your work. This solution looks very promising, and similar to others I’ve seen, but when I try to upload a file I consistently get the error “The request sent by the client was syntactically incorrect ().” I’m using Roo 1.2.2-RELEASE. Enabling TRACE logging on Spring yields an exception:

hi jD,
i am a noob to spring roo. then i found your great tutorial to help me in my learning. Nice job with Roo :)
could you help me out with the error i am getting [similar to the many others :) ]?
the error message states ” No tag “multi” defined in tag library associated with uri “urn:jsptagdir:/WEB-INF/tags/form” ”
how do i go about this?
really appreciate any help on this
thanks and roogards
Dhruv

Hi, Thanks for the Tutorial.
I run it successfully. Here I have question about update Blob in DB by Spring Roo. You disable the update in above code. Bu I am interested in how to update Blob in DB by Spring Roo. DO you have any investigation on update?

Thanks for the excellent work u have made many life’s easy .
I am interested in second approach the Reverse engineering approach can u guide me to achieve the same functionality of file upload and view
can u please provide me the link to the doc