Monday, March 12, 2007

Getting started with Struts 2 and Tiles 2

I do not recommend you using Struts 2 for any reason. For more information, read the last paragraphs of the article.


I suppose this would be a easy work for 2, 3 hours but I spend some days in it, there are little information yet on internet, so I'll try to correct this.
What I've done for you - a simple war file, it is called struts2+tiles2-blank-2.0.6.zip (edit: sorry, I lost the file), so you can use it as template, base to start. It's a some kind of HOW-TO, first steps or something when you decide to build applications on Struts 2 framework with the new Tiles 2.
Versions tath I use are struts 2.0.6, tiles 2.0.6.
In zip file you have mine project with lib included, and some jsp. What it can do? You have a one page with listing fo imputed news in application, and one page for editing them. They use actions & form beans.
I disabled some jar files beceuse I will not use their funcionality - JSF, String and Plexus.
Here is a list of more critical moments for me, where I spend a lot of time:

web.xml

struts.xml

tiles.xml

struts.properties

stdLayout.jsp

listNews.jsp

editNews.jsp

NewsAction.java

News.java



One of the tricks was with tiles TLD: copy tiles-core.tld from tiles-core.jar (open it with WinRAR) and extract the TLD file to WEB-INF directory, and add in web.xml
Other trick is to remove some jars: spring plugin, jsf plugin and plexus plugin. In version struts 2.0.6 they do problems.

And a little hint, Struts 2 is based on WebWorks framework, you can search with this keyword for more results in google.

If you have questions, post comments here. I'll try to update this howto with your experience.

P.S. for UTF-8 charset managment:  In version 2.0.6 there are bugs in tiles, so setting charset / encoding of page to UTF-8 is problematic. If the lastest GA release is 2.0.6 yet, please update to lastest snapshot of struts2-core, struts2-config-browser-plugin, struts2-tiles-plugin, tiles-core, tiles-api from http://people.apache.org/builds/jakarta-struts/nightlies/2.0.x/ and http://people.apache.org/maven-snapshot-repository/org/apache/struts/tiles/tiles-core/2.0-488434-SNAPSHOT/ 
This bug is reported in https://issues.apache.org/struts/browse/WW-1724;jsessionid=8E3A7086E59002AC8802744730F17487?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel




EDIT: I don't know when but I have removed the archive for kickstarting :(    I searched my linux box and my desktop PC for it, but with no result...

I will strongly recommend you NOT to use any Struts framework for your new projects, cause for time I have spend with it, I have faced with a numerous number of bugs! In my case using of General Availability releases (in simple words this means it is stable enough to be used) and a lot problems go out while doing only simple things. For example with mentioned version of Struts 2, I encounter problems with outputing text strings from resource bundle - I have used the simplest usage of that tag. Later I sam that many people complained from this! Very unserious for stable version.

Other problem with the release was using the plugin which would allow me to use Struts 1 and Struts 2 together. Another dead end - this does not work. Ohhh, shit.

Later I switched to the other big framework - Sun's JSF. For a kickstart I used AppFuse. I highly recommend you to do this way - using AppFuse. Easy, high-tech, fast, feature-rich solution! Many examples come with default installation. Everything I needed.

For JSF I can tell you only good things - stable (!!!) framework, authors have thought for many programmer needs before designing the backbone of it, so many pleasant tricks expect you if you switch to it. I have found many of my workarounds I made in Struts 1, have been implemented in a clear way in JSF. And in better way than I could imagine. So the feeling of going to JSF framework was as I have replaced my old Trabant with a new spaceship.

For a conclusion, I will highly recommend using JSF in place of Struts 2 again.

No, no one give me money to tell you this :)

17 comments:

  1. I've found an error :)

    In layouts\dummyBody.jsp there is a line:
    <div align="center">DUMMY BODY</center>

    and should be:
    <div align="center">DUMMY BODY</div>

    Anyway, great thanks for an article and example. I've spend a lot of time searching for how to allow actions to point to tiles template (rather than dummy page only including template) and this page was the only one I've found answer on. And whats more - an easy answer :) Thanks again :)

    ReplyDelete
  2. Hey Joro, your blank war gave us a good start and we're using it for our project - so, Thank you so much!

    Trouble is in using the standard struts validations. We can't call the JSP pages directly (the layouts won't apply), so if i use the standard Struts2 validations, the error messages for required fields won't go away. Any ideas on a workaround to this?

    Also, any ideas if we still need to have the TLD outside of the war in v2.0.9 ? I tried but it didn't work, so I ask.

    ReplyDelete
  3. Thank you Joro, this was exactly what we needed. Good job, keep it up !!!

    ReplyDelete
  4. Could you please reupload you war.

    Thanks in advance

    ReplyDelete
  5. It really awesome....it has given a very good startup.

    -Sreehari

    ReplyDelete
  6. Hi Joro,
    I've tried to download the war file but the server seems to be down....
    I cant find any other help on the web for struts2 with tiles2.
    Can you or anybody send me this file please? [ptr.langer@googlemail.com]


    Greetings Pete

    ReplyDelete
  7. Hi Joro
    I ve tried to download the war file but the server seems to be down.so ,could you reupload the war?

    ReplyDelete
  8. Hi Joro,
    Great article .However i m trying to work on struts 2.0.11 and tiles 2.0.6 and the configuration for 2.0.6 still gives me exception.Can u suggest any change of approach for struts 2.0.8+

    ReplyDelete
  9. Dear Joro, the blank project is not downloading. please look into it... thanks!

    ReplyDelete
  10. Joro On restart we sometimes get org.apache.tiles.definition.NoSuchDefinition Exceptions. Restarting again usually resolves it. I noticed the spring listener class was commented in web.xml. Is it possible something there is stepping on our definitions. I would appreciate any ideas. Using tiles 2.04.

    ReplyDelete
  11. Hello Joro,
    the explanation is quite clear, but the blank project is not downloading. Could you post it again,please? Thanks!

    ReplyDelete
  12. I am approving of you so much!you have say the words I just want to cry out-struts2 is a shit!
    I have encountered the same problem as you did,see this link:
    https://issues.apache.org/struts/browse/WW-2897?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel

    ReplyDelete
  13. HI Joro.............teh way u explained was superb..plz make sure that where i can find the tiles-core.tld file..as i am not able to get in both tiles-core-2.0.4 and tiles-core-2.0.5 jar files

    ReplyDelete
  14. I don't know when but I have removed the archive for kickstarting :( I searched my linux box and my desktop PC for it, but with no result...
    Sorry :(

    In struts related questions I cant help you any more, because i do not use struts now! I recommend you not to start any new projects based on struts, cause there are a huge number of bugs you will meet. Every software has bugs but in struts they are many 'on the surface'

    A better start with JSF (or also with struts 2, if you ignore my recommendations) is AppFuse, i have used it and it is great... Many extras are added in the distributions, many examples exists! Great work for the author, Matt Raible.
    The site is:

    http://appfuse.org/

    ReplyDelete
  15. Thanks a lot !
    I was struggling with this for a while and just when I was giving up this post gave me hope.
    And this together with the things I learned the last few days I could write a simple
    project.

    I understand you are not very into struts and prefer more stable and simple options. And I wish our management would be more pragmatic about this, but struts is a hype in our administration. You master it, or you go away.

    ReplyDelete
  16. Hi,
    can I have an action inside put-attribute tag? I got an error. Is there any other way to achieve this?

    ReplyDelete
  17. Fantastic blog!I have hardly came across with nice informative blog like this..Great work done..I would like to have more from you in the future..

    tilesngrout.co.uk/shop

    ReplyDelete