Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot get Struts 2 to Work

 
J Ellis
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to learn Struts 2 and I am having a problem getting my first application to work. The application is supposed to have two actions, "Product_input" and "Product_save." When I try to perform either action (e.g., I navigate to " http://localhost:8080/Struts2App02/app02a/Product_input.action") I get a 404 error message: "There is no Action mapped for action name Product_input."

I am using Eclipse for my test environment (I can get non-strut JSP Model 2 applications to work just fine). One strange thing is that in my console it first says, "INFO: Unable to locate configuration files of the name struts.xml, skipping" and then it says, "INFO: Parsing configuration file [struts.xml]." Does this mean it is, or it is not finding my struts.xml file? The struts.xml file is sitting in the "WebContent" directory.

Here is my console output:



Here is my Web.xml:



Here is my struts.xml:



Here is my directory structure:



Anyone have any ideas?

J
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your struts.xml file should go into the WEB-INF/classes directory in the generated .war file. For that I think you will need to put it into the root of your src folder i.e. directly under the src folder (at least this is how it is done in Netbeans, can't say about eclipse)...
 
Nishan Patel
Ranch Hand
Posts: 689
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

(at least this is how it is done in Netbeans, can't say about eclipse


Hi Ankit, yes it is same at the eclipse and MyEclipse...

Struts.xml file put under the direct src folder of your application... or WEB-INF/classe at your deployment environment .....

 
J Ellis
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit and Nishan,

Thank you very much, moving struts.xml to the src directory worked! However, my application is still not working as I expected, and the book I am using to learn this is very vague. Basically, the application is supposed to allow you to input some text fields (you enter a product's name, the product's description, and a price) and then it is supposed to display a page showing what you just entered. On my application the second page is displaying, but it is displaying blanks where it is supposed to show the information I entered. This application consists of 1 java class and 2 jsp pages. Would you please look at the code and the configuration files above and tell me where I am going wrong?

Project.class:



ProductForm.jsp:



ProductDetails.jsp:

 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show us the code for your Action class...
 
Nishan Patel
Ranch Hand
Posts: 689
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Please make sure your action class declare
and getter and setter for product.

Now make some change at your jsp code.. like...



this will set your Product bean automatically and then you can show at your jsp page....

 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nishan is correct. You need to put the word product (the name of Product instance in your Action) before the property names. I was just wondering to see the action because you might have used the model-driven way...
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Product class (as written) *is* the action:See the execute() method? The JSP as written is correct; there is no Product bean. That makes the JSP incorrect, since it *does* reference a product bean.

So it should be written one way or the other, but preferably not both.
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the product class is not inherited from Action interface. Isn't it required for actions to be a sub-type of Action interface??
 
J Ellis
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the book I am following ("Struts 2 Design and Programming" by Budi Kurniawan) and as far as I can tell, David Newton is correct, the Product class is the action class. From what I gather (and I am very new at this), implementing Action is completely optional in Struts 2. (Ankit, are you thinking about Struts 1?)

David, I am a bit confused by the line in your post:
See the execute() method? The JSP as written is correct; there is no Product bean. That makes the JSP incorrect, since it *does* reference a product bean.
Are you saying that my JSP (the original JSP) is correct or incorrect? It looks like you are saying that it is correct but this line is a little ambiguous.

Assuming the JSP is correct (and I will try it with the word "product" in front when I get home this evening), does anyone have any other idea as to why this would not be working? I am not getting any errors on my console and as far as I can tell I have followed everything in the book. Is there some way I can get more information from Struts 2 as to what is going on behind the scenes?

Thank you all again for your help,

J
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No Ellis, I was referring about Struts 2. I think I need to read my book again as its clear I've forgotten a few things . Anyways what David meant was that your JSP in itself is correct. Its just that in your JSP you are referring to properties with product prefix. Since your action class doesn't have any bean named product, so your EL returns empty string. So instead of



use



PS, your productName property doesn't follow the general naming guidelines (its not necessary to follow this one but its a recommended practice). Like the other properties, it should also have been only name instead of productName. If you are doing that because of some database reasons (as many databases don't allow "name " as a column name, then its fine...
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Ankit: No, actions can be POJOs.

@J Ellis: I meant that one of the two JSPs is (probably) wrong: there's either a product bean, or there isn't. The ProductDetails.jsp file assumes there's an action property named "product", but there doesn't seem to be one in the code.

The Struts 2 documentation wiki discusses some more of the "behind the scenes" stuff, but the book you're referring to is also pretty good and should be enough to get you going--but that doesn't mean there aren't mistakes in it. It also covers a fairly early version of S2, which might trip you up unless you're using the same version (which I wouldn't--lots of stuff has been fixed and/or improved).
 
J Ellis
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you everyone for your help!

David and Ankit: you were spot-on, removing the "product." prefix from in front of the three input variables in my ProductDetails.jsp page fixed the problem. I will keep in mind the fact that Struts has changed between the version in the book and the version I am using. Also, Ankit, thank you for the tip about naming conventions. I used "productName" only because it is what they used in the book; I myself was wondering why the author used a different convention for that particular variable, perhaps he had in mind the database limitation you mentioned but just did not state as much in his book.

Thanks again,

J
 
Karl Krasnowsky
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:(at least this is how it is done in Netbeans, can't say about eclipse)...

Hey Ankit,
I'm trying to figure out the best (easiest) method for generating a Struts2 base project within Netbeans 6.7x
I've been able to perform this manually, but I was hoping to find a plugin that would perform the boilerplate work for me.
Got any ideas?

regards,
Karl
 
vishnu priya
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
while i was copying the struts.xml in src its not pasting in eclipse
but in my war i have it in web-inf\classes folder
but still i am getting the same error

INFO: Starting service Catalina
Nov 6, 2009 11:46:32 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying erb application archive Sample4.ear
NOV 6, 2009 11:46:33 PM com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
INFO: Parsing configuration file [struts-default.xml]
NOV 6, 2009 11:46:34 PM org.apache.catalina.core.StandardContext Start
SERVERE: Error filterStart
NOV 6, 2009 11:46:35 PM org.apache.catalina.core.StandardContext Start
SERVERE: Context [/Sample4]startup failed due to previous errors

I am using servlet 2.4,tomcat 6.0.16, struts 2.1.8 jars
What can i do and will you please send me the list of jars used for a struts 2 application?
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vishnu try to start a new thread for your new question. You can start a new thread using the New Topic Button

The error that you have shown is from the netbeans output which is not very informational about the problem. Show us the relevant portion of the tomcat logs when you deployed the application (netbeans also has a tab in output window which shows the tomcat output). The dependencies for struts 2.1.8 are listed here. I've not used eclipse much so can't say about the eclipse problem. You can ask IDE specific problems in the IDEs-Version-Control-other-tools forum...
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Karl: Please start new threads when you're asking a completely unrelated question.

As far as I know there's no "wizard" to create a base Struts 2 in NetBeans. There are, however, Maven archetypes, which should be the preferred methodology. Considering how simple it is, though, I'm not really sure either are really necessary.
 
Karl Krasnowsky
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:@Karl: Please start new threads when you're asking a completely unrelated question.


Hey, I was set up. When they moved an ill posted message to my original thread to this new one they also grabbed parts of my original. Check the date.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic