It's not a secret anymore!*
The moose likes Spring and the fly likes manage get and post? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Spring
Bookmark "manage get and post?" Watch "manage get and post?" New topic
Author

manage get and post?

mark smith
Ranch Hand

Joined: Apr 05, 2005
Posts: 250
hi

in spring 3, i use annotation, i need to create 3 web page

so i create a controller

first page display only information from a db
second page, user need to enter some data to do a search and result is displayed...

similar thing for the last page, but don't use the same object...

my first problem is how to manage the get and post of second and last page?

does i need a controller for every page?

thanks
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

No because you are using Annotations, you can have all three pages call three different methods in your controller

@RequestMapping() has an attribute called requestMethod or method you can set one to RequestMethod.POST and the other to RequestMethod.GET but both have the same URL value.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
mark smith
Ranch Hand

Joined: Apr 05, 2005
Posts: 250
Mark Spritzler wrote:No because you are using Annotations, you can have all three pages call three different methods in your controller

@RequestMapping() has an attribute called requestMethod or method you can set one to RequestMethod.POST and the other to RequestMethod.GET but both have the same URL value.

Mark


same url with same jsp?

so the get will net to have the information of the post...
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

mark smith wrote:
Mark Spritzler wrote:No because you are using Annotations, you can have all three pages call three different methods in your controller

@RequestMapping() has an attribute called requestMethod or method you can set one to RequestMethod.POST and the other to RequestMethod.GET but both have the same URL value.

Mark


same url with same jsp?

so the get will net to have the information of the post...


Yes, I would assume the same jsp page. I also would assume your get is to load the page and the post is after the user clicks submit.

So I type

www.mysite.com/mywebapp/customer.jsp?id=5 in my browser. That will do a get call the one method in my controller that is mapped to GET, It say passes in the RequestParameter of five. In my method code I lookup customer #5 and fill in a Customer object and set it in the Model. The jsp renders and goes to the client browser with the form showing with that customer information. The user changes some data then clicks the Submit button, which submits to the same URL but this time it is a POST. The method in the controller then receives the Customer and sends down to the CustomerService and CustomerRepository to save the updated Customer.

Or with the same URL with no parameter, the same Controller method that is mapped to the GET just creates a new Customer and adds it to the Model. The same jsp page gets rendered with an empty form but a Customer object backing it. The user enters the new data, clicks the Submit button, which submits to the same URL but this time it is a POST. The method in the controller then receives the Customer and sends down to the CustomerService and CustomerRepository to insert the new Customer.

Mark
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: manage get and post?