aspose file tools*
The moose likes Struts and the fly likes View and post in one action class in Struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "View and post in one action class in Struts" Watch "View and post in one action class in Struts" New topic
Author

View and post in one action class in Struts

Fawad Ali
Ranch Hand

Joined: Dec 28, 2009
Posts: 112
Hi,
I want to show some data from database in a form. Then this data is changed by the user and will be saved again to the database. I want to minimize the round trips to the database. So I want one action to show data in form and the same action take the data from form and save it to the database. Is there anyway to do it.

An example will help a lot.

Thanks in advance.


Regards, Fawad Ali.
Software Engineer, Stafona Inc. - My Blog
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

Fawad Ali wrote: I want to minimize the round trips to the database.


I don't know how you are going to save trips to the database. Any way you shake it, you have 2: one to get the data to display, one to update the data.
As for using the same Action class for both both purposes, it depends on which version of Struts you are using.
Struts 1.x: One option is to extend Action with a class that looks for a request parameter "step" which can have the values "prepare" and "commit". This class invokes different methods depending on which value is set. If the value is "prepare", run the logic for displaying the data. If the value is "commit", run the logic for updating the data.
Struts 2.x: One can configure the display and save URL's in struts.xml to invoke the display or commit method on the action.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Fawad Ali
Ranch Hand

Joined: Dec 28, 2009
Posts: 112
Hi Joe,
Thanks buddy. I am using Struts 2. Sorry I didnt mention that earlier. I have done it in the same way you told me. I have declared tow actions pointing to the same action class. In a view part, I instantiate an object of class Event event and show it on page. In submit part, it throws Null pointer exception. Here is the code


I think that on each call to the class a new instance is made, so in view part I am getting event from db but in save the event is null. So any suggestions on this?

Thanks for your help
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

You need a getter and a setter for event in your action.
Fawad Ali
Ranch Hand

Joined: Dec 28, 2009
Posts: 112
Hi Joe,
I have done that too. Look at the following code.

Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

Fawad Ali wrote:
I have done that too. Look at the following code.


I don't see a method that gets or sets an Event instance in your code.
Fawad Ali
Ranch Hand

Joined: Dec 28, 2009
Posts: 112
Hi,
They are the getters and setters just below the event instance like.

private Event event;
//getter and setter
public void setEvent(Event event){
this.event=event;
}
public Event getEvent(){
return this.event;
}

public String viewPage throws Exception(){
setEvent(dbUtil.getEvent());
return "view";
}

public String savePage throws Exception(){
System.out.println('Event name=============>"+getEvent().getName());
return "save";
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: View and post in one action class in Struts