This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Struts and the fly likes Problem with Struts 2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Problem with Struts 2" Watch "Problem with Struts 2" New topic
Author

Problem with Struts 2

Gustavo Villegas
Greenhorn

Joined: Aug 27, 2008
Posts: 11
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:

tableAction_insert.action
tableAction_edit.action

And my struts.xml contain different results for each action:

for example:

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??

I hope you can help me.

Thanks
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
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.
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 684

Hi.

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.


Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
Gustavo Villegas
Greenhorn

Joined: Aug 27, 2008
Posts: 11
Originally posted by Nishan Patel:
Hi.

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.
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
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.
Gustavo Villegas
Greenhorn

Joined: Aug 27, 2008
Posts: 11
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.

Thanks
[ October 03, 2008: Message edited by: Gustavo Villegas ]
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with Struts 2
 
Similar Threads
validate() for multiple actions
Struts 2 - Yet another combobox prepopulation using prepare method question
doPost() not happening
Struts 2 checkboxlist error - list key could not be resolved
Hibernate and Struts 2 question