File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes web.xml and Struts 2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "web.xml and Struts 2" Watch "web.xml and Struts 2" New topic
Author

web.xml and Struts 2

Luigi Quarta
Greenhorn

Joined: Jul 30, 2009
Posts: 17
I'm trying to make this tutorial work: http://struts.apache.org/2.0.14/docs/hello-world.html

The structure of my webapp is

tutorial/
tutorial/META-INF
tutorial/WEB-INF
tutorial/WEB-INF/web.xml
tutorial/WEB-INF/lib
tutorial/WEB-INF/classes
tutorial/WEB-INF/classes/HelloWorld.class
tutorial/WEB-INF/classes/HelloWorld.jsp
tutorial/WEB-INF/classes/struts.xml

in lib i've copied all the Struts 2 lib's jars
this is my web.xml structure:


this is my HelloWorld.jsp structure:


this is my struts.xml structure:


and this is the HelloWorld.class code:


When i go in the Tomcat admin control panel this web app doesn't run... probably the container doesn't like my web.xml file. Why?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

This line in struts.xml looks wrong to me



The fully qualified class name is tutorial.HelloWorld and not hello-world.HelloWorld...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

The JSP file shouldn't be in the classpath, it should be in either under the webapp root or under /WEB-INF.
Luigi Quarta
Greenhorn

Joined: Jul 30, 2009
Posts: 17
i've re-organized my project like this:



index.jsp:


web.xml:


struts.xml


struts.properties


HelloStruts2World.java:


But as before my application cannot be started in Tomcat and if i try to run it with NetBeans I get this error:


I also notice that undeploy the application is impossible cause struts2-core-2.1.6.jar cannot be deleted!
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17



This is wrong, the HelloStruts2World action is mapped to HelloStruts2World path, so use

Luigi Quarta
Greenhorn

Joined: Jul 30, 2009
Posts: 17
Thank you,
So, every time, only the action name must be provided as action attribute in a form? I've made this change but the application server still doensn't want to run my application
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Which Struts 2 libraries are you deploying? If you're deploying "all" of them as you claim it most likely won't work because you won't have satisfied the plugin dependencies. If your logging is configured correctly you'll see errors on startup.
Luigi Quarta
Greenhorn

Joined: Jul 30, 2009
Posts: 17
The log folder is empty cause the application server refuse even to start my application... I've imported the minimum library set as described in the struts 2 official tutorial (ognl-2.6.11.jar, xwork-2.1.2.jar, struts2-core-2.1.6.jar, freemarker-2.3.13.jar, commons-logging-1.0.4.jar)

Usually when the server refuse to run an application it's a web.xml question, but the different respect the others times is only in the filter part...
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

I think you are missing the commons file upload library. Download the essential dependencies from here...
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

And commons-io.

You *really* need to configure logging--how else will you spot startup errors?!

Application startup errors *are* logged.
Luigi Quarta
Greenhorn

Joined: Jul 30, 2009
Posts: 17
Excuse me, as you can see this is my first Struts 2 project... i've added to the lib folder common-io e common-file-upload. The project doesn't run but i get several log files:

catalina.2009-09-17.log


localhost.2009-09-17.log:


The error seems to be in the HelloStruts2World class isn't it?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Yes.

You configure the action like this:But that isn't the package you're using in the action itself.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

As David said, the actual class name of your Action is action.HelloStruts2World. So it should go under WEB-INF/classes/action/HelloStruts2World.class in the deployed archive...
Luigi Quarta
Greenhorn

Joined: Jul 30, 2009
Posts: 17
I would like to place my actions class in view/action/ so i should modify the class name but i don't understand how
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Change the package name.
Luigi Quarta
Greenhorn

Joined: Jul 30, 2009
Posts: 17
I've tried many times without result; every time in the log files the action is not found...
What i have to change the name or the package namespace? I've changed the package name into "/view" "/view/action" and athers...
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Post your *current* code and configuration.
Luigi Quarta
Greenhorn

Joined: Jul 30, 2009
Posts: 17
Code:


Configuration:



Application structure:

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Your action class's package doesn't match where it is in the file hierarchy.

You're declaring your action class as:but in the file hierarchy it's:
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

And, while it isn't really a problem, if you're using Struts 2.1 you should probably use the 2.1 DTD.
Luigi Quarta
Greenhorn

Joined: Jul 30, 2009
Posts: 17
Thanks for the fast reply, so in my code it should write:

?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

What happened when you tried it?

But no, it should beYou might want to brush up on some basic Java before going too much further--it'll save a lot of time.
Luigi Quarta
Greenhorn

Joined: Jul 30, 2009
Posts: 17
Now all run perfectly, thank you so much!
It was a class package question, anyway i'll follow your advice and i'll use 2.1 DTD too.

I think i should study better the java pakages and the classpath... Another configuration dubt: actually the result is the "HelloWorld.jsp" placed in the application root, in general i will place the JSP in:



to obtain map the result to the page I should write in the struts.xml sopething like this:


?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Well, I wouldn't place the JSP files in the class directory--they're not classes. I *would* keep them under WEB-INF, but I always keep them in their own directory.
Luigi Quarta
Greenhorn

Joined: Jul 30, 2009
Posts: 17
Thanks, that's a good advice, i'm following the rest of the tutorial on the struts 2 wiki and now many things are clear.
Best regards
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: web.xml and Struts 2