Iwo Kucharski
Iwo Kucharski

Reputation: 3825

Load same template from different methods

My case looks like this: I have site with accounts list and an edit button in every row. I have a template which is form used to create new account. Controller:

@RequestMapping(value = USERS + LECTURER + ADD, method = GET)
    public String userAdd() {
        return ADMINISTRATION + USERS + LECTURER + ADD;
    }

I want to use the same template to edit user, but I have method which first recognize if user is for ex. LECTURER or STUDENT:

@RequestMapping(value = USERS + "/{userId}" + EDIT, method = GET)
    public String editUser(@PathVariable(value = "userId") final long userId,
            final ModelMap modelMap) 

Then I want to load all user data (name, surname etc.) into form (USERS+LECTURER+ADD). How can I do it respecting Post/Redirect/Get pattern? I planned to go like this: (someone clicked edit button) -> go to method mapped USERS/{userId}/EDIT which finds user access level -> (if LECTURER) go to USERS/LECTURER/{idUser}/EDIT and load all data to form (template is in USERS/LECTURER/ADD) -> save modified data and redirect to USERS (users list)

Upvotes: 1

Views: 37

Answers (2)

Iwo Kucharski
Iwo Kucharski

Reputation: 3825

Well, I though I can't have 2 methods with same RequestMapping, but it's possible when these methods use different HTTP methods (POST or GET). It solved my problem, because I loaded form in controller method using GET and saved changes using POST (+ redirect). thanks for reply, Martin.

Upvotes: 0

Martin Frey
Martin Frey

Reputation: 10075

As long as your requestmapping method returns a string (or modelandview) you can return whatever template reference you like. The returned string will be used by spring to resolve the template. Take a look at the spring documentation about requestmapping. Its quite well explained there.

Upvotes: 1

Related Questions