Hi I recently started using Struts and I'm still very noob I wanted to develop something easy, so I decided to create a project to Insert, Delete, Update and Select Records from a Single Table and I don't know how to create the Actions, Should I create something like this ??:
InsertAction extends ActionSupport EditAction extends ActionSupport UpdateAction extends ActionSupport SelectAction extends ActionSupport And my struts.xml should only containg results for,input,error and success for each Action?
I'm asking this because I was creating only one Action Class named TableAction and there I was calling methods in this way:
And my struts.xml contain different results for each action:
insert method returns "insertSuccess"
and I have a result called "insertSuccess" which is mapped to "insertSuccess.jsp"
But I have some troubles when trying to validate the form, cause I didn't have any "input" result, so my form wasn't validated.
So what's the most correct way to do what I'm trying to do??
There are a lot of options for this and the best one depends on the specific requirements of your project (since its your own its nice since you can taylor the requirments to your needs). Since each action class in struts 2 gets populated with the data from your html form a basic way to set things up is to have one action per different html form lay out that will be submitted to your application server. Basically one action per jsp that submits data to your application server. I usually assume that I will use this model and wait until my requirements dictate that I do it in anoteher way. This has worked resonably well for me.
When you validate your form you have to define <result name="input" >
Because if validation is fail then your form understand it is in input mode.
So input is required if you are not written result input type then it gives you an error but not failed validation.
Ya that was my problem, I got an error, sorry for my poor explanation, 'cause I dont have a result configured for "input".
Tom, then you are telling me that I should create an Action for each JSP on my Project, so should I have InsertAction, EditAction etc... ??
Thanks for the answers.
Sorry for poor english.
Joined: Aug 29, 2008
Thats the basic pattern I use when I'm setting up a screen. Some screens, like those that just display errors don't have thier own action but any jsp that creates a form to submit I give its own action.
On this forum I've seen some people re-using the same action for multiple screens. I think that this can make sense in a scenario where the JSPs that share the action are all using the same fields in thier forms. This brings with it some extra complexities and I haven't done it so I can't speak to the best way to do that.
Joined: Aug 27, 2008
I really don't know what do to, cause I try creating an Action for each screen and Now I have like 5 classes for a simple Insert Form =S I guess this stuff isn't still very clear for me. I don't know if this is right (5 Classes for a simple Insert Process) but I this I right I guess I'll continue developing like this =D.
Hi Gustavo, it also seems to me like you're using a lot of classes for this. Usually when I have a table that I want to insert, update, and read to I make a single JSP for it (depending on your specific requirements more pages may be necessary). I send a different command to may action class depending on what the user is trying to do and process the data from the screen based on that command. Since this is just a practice application it might be a good experience to see how to do it with many different pages but it isn't the only option.