Monday, October 31, 2011

Hello world – Struts 2

It took awe fully a lot of time for me to do this simple stuff as I was not too comfortable with JSP, tomcat and eclipse Indigo. Though I followed most of the hello world tutorials available out there, I always ended up with some errors or the other.

Some of the important lessons that I have learnt with this sufferings are,

1.       Java frameworks are not reverse compatible. I hate that.

2.       Code developed with say jdk1.5 environment probably won't work with jdk1.6 and vice versa

3.       Only use prescribed files by all means and do not try including any new stuffs before u know what it is as far as the java technology is concerned.

Since I come from a technology which is RAD and reverse compatibility is never an issue, IT IS REALLY HARD FOR ME TO DIGEST ABOUT THIS IN JAVA WORLD L   

Any ways I am starting to getting used to this and here is my hello world to Struts 2.

Prerequisites,

1.       Tomcat server version 6.0 or 6.0.33 needs to be installed. No other versions. Probably other versions in 6 might support but I am not sure

2.       JDK 1.5 is required. Don't switch to higher version or u might end up having a tough time finding what the problem is. Well if you are a java guy, you would know it better than me.

Following is the folder structure that you need to have

The lib directory should contain all of the following files

         

 

All the files that you find in the above screen shot are frame work files that comes with Struts 2 package version 2.1.2 I guess. You can google down with the names of the files to find the appropriate sub version of struts 2. Believe me it did not work with version 2.1.6.

Please don't blame me if you can do it better. I am very new to this technology J

Key in the following code to HelloWorldAction class

You web.xml file should have the following code

The popup text that you find in the above image shows the filter class that helps your web.xml file understand the presence of struts.xml file.

Following is how my struts.xml file looks like.

 

The action name "helloworldAction" can be any name of your choice.

The class name "web.HelloworldAction"  tells the compiler to look into this specific class to execute the specific method

The method name "helloWorld" asks the compiler to look for this particular method and perform the necessary action inside the provided class in the class name parameter

Finally the index.jsp is as follows

Now save your work, move your project to the tomcat server, by doing the following

After doing this click finish.

 

Now restart your tomcat server or simply start it if its not already running.

Now key in the following url in your browser. I did this one with the browser provided by eclipse it self

http://localhost:8080/HelloWorld/

http://localhost:8080/HelloWorld/helloworldAction.action

Note the differences in the above mentioned images,

When you provide the url for helloworldAction class directly you can see the print statements provided inside the helloWorld method in the console. Possibly you can use this one to your advantage

Hope this helps J

No comments:

Post a Comment