Wednesday, 29 July 2009

Eclipse Galileo and Struts2

I was quite surprised with how many people came to my blog and read my earlier post on how to create a blank Struts2 project and start using this framework in NetBeans 6.5

With a release of new Eclipse, I thought it would be interesting to do the same thing, but now in this IDE.
As you probably already know, Eclipse is one of the famous and certainly one of the best Java IDEs.
Well, about a month ago people from Eclipse foundation released a 3.5 version of Eclipse, called Galileo.

I develop almost all my project using NetBeans, and have never done any Struts2 programming using Eclipse.
That is why I decided to write a post on how to create a Struts2 example in new Eclipse release: Galileo. That's quite fun because that way I (we) get to learn on how to use Struts2 in Eclipse, and get to see Galileo in action a little bit closer…Note: In this post, I’ll use much of the text , entire source code and application logic from my previous post and concetrate only on the differences between these two IDEs.

Next thing we need is Struts2 itself. Go to http://struts.apache.org/2.x/index.html and download the latest version of Struts2.
I downloaded struts-2.1.6-all.zip
You won't be needing all the libraries in it. Just 6 of them. Because of this, you can download essential dependencies only.

When you download one of these archives, extract it to some location, for example: c:\Struts2

It is now time to start a new project. Start Eclipse Galileo.

Click file --> new --> Dynamic web project

Let’s call this project GalileoStruts2Example
I’ll use Tomcat 6 . You can use some other server that you have available , it is completely up to you.

Click Finish.

Your project tree will look like this:

Now, in order to use Struts2 libraries, let's add those essential dependencies to project:

Most of the tutorials on the web are written for older version of struts2, at the time when commons-fileupload-1.2.1.jar was not mandatory, so even when following all the steps in tutorial , you just couldn't get your project to work ( with newer version of Struts2 ) !

Now, your project will look like this:

Now, copy these six JARs into the “lib” folder located in the root of WEB-INF folder.

These files will be uploaded to App Server , and a deployed application will use it.
We could put this files into main lib folder of application server, but it is most probably better this way...

We need to create one more folder in the root of the WEB-INF , and name it "classes".
This is where you will create struts.xml, and if needed - a struts.properties file.

I'll put all my JSP's into new folder, "jsp" , which I will create in the root of "WebContent" folder.

All Java code that we write, we will place into some package in the "Java Resources: src" folder.
I'll create one now, called "struts2Example" and a "Hello" Class in it.
If you use validaton, put all Validation XMLs inside class's package too...

This is how, at minimum, your Struts2 project structure should look like.

I'll create one small Struts2 application now. Nothing complicated.
User will enter his name, and press "submit" and other page will open , saying Hello to this user.

struts.xml must be located in “classes” folder located in the root of WEB-INF folder:

This is where place a file struts.properties if we need to use it, as well…

web.xml:

And if we press “Run” button, and choose a server on which it is supposed to run

we’ll get something looking like this:

But don't worry. Everything is OK, we just didin't handle default page...
If we append entername.action on the URL , our new application will open:

If we do not enter name and press “submit”:

but if we do enter name and press “Submit”:

So, I hope I helped you to understand how to create an empty struts2 project in Eclipse Galileo.

At the end, our project looked like this:

A small conclusion of mine: great thing about Eclipse is that you can do just about everything with it , with just a little few extra settings and installations...
Unfortunately, for me - many things and options remained a little bit more complicated and unintuitive regarding to some other IDEs. That is such a shame!
Nevertheless , for me , Eclipse continues to be one of the best choices!.

76 comments:

I recently came accross your blog and have been reading along. I thought I would leave my first comment. I dont know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.

Thanks a lot for simple working tutorial. I always look for tutorials which simply lets me run a concept without getting into complex example logic.But I had to insert "init-param" tag into web.xml to make it run, Could you please tell me what could be the reason.

Just one small suggestion regarding libraries. Instead of adding them first to Java Build Path then to WEB-INF/lib it is much easier to add them to Java EE Module Dependencies. This way those libraries will be automatically resolved to WEB-INF/lib at deployment time.

here i'm ready for the new query,never mind ;)i wanna create a new project but don wanna follow each and every steps every now and then i.e. starting from File > New > Dynamic Web Project and so on..rather i prefer copy and paste my existing project then refactor>rename in order to create my new project in galileo itself.now i'm facing the problem while running both of the application on Tomcat as it's taking same context path for both the applications though i renamed the previous application's name.do you know from where can i change the context path of the new application in Tomcat which is there in Galileo?as we can do in case of external deployment of war on Tomcat with just changing the Folder name in it..

Me too like you, means till now I have used only netbeans and newbee for both galileo and struts 2. I found your blog quite to the point and very helpful. but i was just wondering why do we need to copy all the jars into web-inf/lib even when we already have included the external jar? can you shed some light on it.Thanks

I am getting errors:Unable to load configuration. - bean - jar:file:/D:/netapp2/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/StrutsHW/WEB-INF/lib/struts2-core-2.0.9.jar!/struts-default.xml:14:148 at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:58) at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:395) at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:452) at org.apache.struts2.dispatcher.FilterDispatcher.init(FilterDispatcher.java:201) at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275) at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:397) at org.apache.catalina.core.ApplicationFilterConfig.init(ApplicationFilterConfig.java:108) at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3696) at org.apache.catalina.core.StandardContext.start(StandardContext.java:4343) at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045) at org.apache.catalina.core.StandardHost.start(StandardHost.java:719) at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045) at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443) at org.apache.catalina.core.StandardService.start(StandardService.java:516) at org.apache.catalina.core.StandardServer.start(StandardServer.java:710) at org.apache.catalina.startup.Catalina.start(Catalina.java:566) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)Caused by: Unable to load bean: type:com.opensymphony.xwork2.util.ObjectTypeDeterminer class:com.opensymphony.xwork2.util.GenericsObjectTypeDeterminer - bean - jar:file:/D:/netapp2/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/StrutsHW/WEB-INF/lib/struts2-core-2.0.9.jar!/struts-default.xml:14:148 at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register(XmlConfigurationProvider.java:224) at org.apache.struts2.config.StrutsXmlConfigurationProvider.register(StrutsXmlConfigurationProvider.java:101) at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:169) at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:55) ... 21 moreCaused by: java.lang.ClassNotFoundException: com.opensymphony.xwork2.util.GenericsObjectTypeDeterminer at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1358) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1204) at com.opensymphony.xwork2.util.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:146) at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register(XmlConfigurationProvider.java:195) ... 24 moreNov 4, 2009 10:47:50 PM org.apache.catalina.core.StandardContext startSEVERE: Error filterStart

I am using following jars:commons-fileupload.jarcommons-logging-1.1.1.jarfreemaker.jarognl-2.6.11.jarstruts2-core-2.0.9.jarxwork-2.1.3.jar

Because, the jar files that we include in our "web-inf/lib" folder are to be deployed to AS, so that AS can use all the dependencies it needs when running our application ( there are no struts jar files inside the server itself )...

Included external jars are there for us in order to write code that uses classes inside included JARs.

I am Sachin..I have replicated exactly what you have tutored. But I get this error:Mar 6, 2010 7:38:47 PM com.opensymphony.xwork2.util.logging.commons.CommonsLogger warnWARNING: Could not find action or resultThere is no Action mapped for namespace / and action name entername. - [unknown location] at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:178) at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:61) at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39) at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:47) at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:478) at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454) at java.lang.Thread.run(Thread.java:595)

hi darko, thanks to you. It really helped me not to learn struts 2 though but in another thing. I was unable to install log4j in eclipse galileo and tried it all day. I used to Right click on webapps folder in project explorer mode and then using build path tried to install into the library log4j.jar. But however i tried eclipse refused to find it. But after following ur way i was able to load log4j and got the logs also. You said it right, though its great, its so complicated to learn and add specially these library things into the classpath that is taking the maximum time. Hope for some early solution. Thank you once again. :-)

I did exactly as u demonstrated in the example, its really good and so far the easiest example i came across...

But still i am having some problems when i hit the URL its giving me URL resource not found error...

my web,struts.xml are same as yours...

Any suggestions on the same will be really helpful.

Thanks,Ashish"

Hey Darko,

First thing i was getting Error FilterStart on my console ... It was not giving me any other message... What i did was i changed my Tomcat 5 to Tomcat 5.5Then it gave me a complete stacktrace which was Class Not Found Exception...Then i simply changed some jars and added javaassist.jar and it worked...

Unfortunatelly, I can't see your struts.xml . Until I can see it: You probably didn't do everything as I explained... Please check everything again and confirm you mapped web.xml and struts.xml as I suggested...

I am a beginner with struts. I have been searching in the internet from the past 3 days and came across many struts starter app, but there was a small prob, i.e each time i added struts.xml from eclips galileo, it used to go into Java SRC folder and my apphad failed. After looking at ur tutorial i put in in the web-inf classes manually(eclipse by default takes struts.xml)into java src. But now I am happy, urs was the 1st successful app i ran. thank u soooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo much.