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.
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)...
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?
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...
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.
But the product class is not inherited from Action interface. Isn't it required for actions to be a sub-type of Action interface??
Joined: Aug 23, 2009
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?
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
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...
@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).
Joined: Aug 23, 2009
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.
Ankit Garg wrote:(at least this is how it is done in Netbeans, can't say about eclipse)...
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?
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...
@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.
Joined: Jan 16, 2009
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.