• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

View and post in one action class in Struts

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Fawad Ali
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need a getter and a setter for event in your action.
 
Fawad Ali
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Joe,
I have done that too. Look at the following code.

 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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";
}
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic