Monday, April 21, 2008

Struts2, uploading a null file and disappearing ActionMessages

There are two small errors I found in my Struts2 app today. Two little sneaky bastards, caused by my inattention, and perhaps insufficient documentation of Struts2. I'd like to post them both, in case it saves some other newbie's day.

The first one concerns the use of the FileUploadInterceptor. It's a really handy class that manages uploading a file from your webapp's form. It's a part of the defaultStack, so all you need to enable the file upload is one s:file tag on your jsp, and a bunch of getters/setters in your Action class, which will allow you to access the uploaded file, already residing on the server. It's all described in details on RoseIndia.

However, what I haven't found in any of the example code snippets, is checking whether the user has chosen a file at all. In the case of my application, uploading a company's logo is not mandatory - so I needed a test before loading the image, checking whether there's any file being uploaded. So what did I do? Let's see: the FileUploadInterceptor fills in three properties of my action:

Well, if the File pic references the actual file, I simply checked whether it was null: ...which didn't prove to be a good idea. pic just kept on containing a reference to a nonexistent temp file. At first I thought the params interceptor somehow "forgot" to set it to null, if a file had been transferred previously. However, this was not the case. Every time the filename was different, but it was there. What's the solution, then?

It seems that the other two properties aren't there without purpose. Apparently, FileUploadInterceptor works this way: every time it runs, it generates a file name and creates a File object for it, no matter if a file is actually being uploaded. That's a normal behavior. What I should have done is checking the String picFileName property, which is empty if there's nothing being sent, like this: That's it, worked like a charm. Still, I find it a little misleading and think the javadocs of FileUploadInterceptor lack a suitable warning.

The other mistake of mine was while setting the ActionMessages of my action which extends the Struts2's built-in ActionSupport. There it is, down in black and white:

getActionMessages public Collection getActionMessages() Description copied from interface: ValidationAware Get the Collection of Action-level messages for this action. Messages should not be added directly here, as implementations are free to return a new Collection or an Unmodifiable Collection.
...and so it does. Trying to do: resulted in my message "disappearing" in the always empty collection. Man, that felt creepy! ;) It's a pity I haven't noticed this method before: but there's no way I could blame the missing documentation this time - just me and my blondishness :)

9 comments:

Łukasz Lenart said...

Nie wyglądasz mi na blondynkę ... a może farbowana??!?? :D

Anna Skawińska said...

A od kiedy to informatycy nie mogą używać literackiej przenośni? :)

Łukasz Lenart said...

Nie lubię słowa Informatyk, już lepiej Programista, Inżynier Oprogramowania albo Inżynier Systemowy ;-)

A przenośni używają poeci i tak się czyta twoje wpisy :D

Duży podziw za swobodę z jaką używasz słowa pisanego po angielsku :-)

Anna Skawińska said...

Dzięki. Angielski wtłoczono we mnie w dzieciństwie i żadna w tym moja zasługa. Za to teraz dopiero przyszedł czas na żmudną naukę - Javy :)

Może żeby nie nabijać komentarzy zapraszam na a_skawinskaXo2.pl

Łukasz Lenart said...

Ok, koniec komentowania, a co do prywatnego mailowania, mogło by to być źle odebrane przez twoją połówkę i moją lepszą połówkę ;-)

Powodzenia z pracą magisterską!

Anna Skawińska said...

Nie bardzo rozumiem, co mogłoby połówkom przeszkadzać w dyskusjach na temat słowa "informatyk" czy mojej pracy dyplomowej - no chyba, że uważają, że lepszym miejscem na nie jest blog ten albo Jacka Laskowskiego :>
Dzięki za życzenia - praca wre.

Łukasz Lenart said...

Moja kochana żona jest bardzo podejrzliwa jak dostaję prywatną korespondencje od nieznanych kobiet (dla niej ;-). I tłumaczenie się, że to służbowo, na temat programowania na nie wiele się zdaje :D

Anna Skawińska said...

Cóż mi pozostaje - zmienić nazwę bloga, usunąć zdjęcie... Hm, seksizm w IT to niezły temat na kolejnego posta :>

Łukasz Lenart said...

To nie jest seksizm w IT tylko w małżeństwie ;-)
Chodź fakt, że jeśli już jakaś piękna płeć pojawi się w IT to zostaje zaburzony naturalny "męski" porządek i w ogóle jakieś dziwne rzeczy się dzieją :D