1

I have two html templates which do same function, but handled by two different controllers:

1st html handled by moderator controller

<form th:action="@{/moderator/new-user-form}" th:object="${caltest}" method="post"
     enctype="multipart/form-data" class="form-validate form row">
    <!-- some code -->
</form>

2nd html handled by admin controller

<form th:action="@{/admin/new-user-form}" th:object="${caltest}" method="post"
     enctype="multipart/form-data" class="form-validate form row">
    <!-- some code -->
</form>

As you can see these templates differ only by action url:

th:action="@{/someurl}"

Is it possible to use the same template with dynamic url from different controllers?

6
  • I am asking this question to avoid repeating the same code many times Commented May 9, 2017 at 10:16
  • from where do you get the identifier to call the right controller? In your case admin or moderator? Or at least the full url? Commented May 9, 2017 at 14:38
  • Have you tried that? Did you get any errors? The Same template can be returned from different controllers as long as they are mapped to different URL paths Commented May 9, 2017 at 23:21
  • @SAP yes I am using that now without any errors. My urls differ, since each controller is mapped accordingly(For instance: /admin/save-user-form, /moderator/save-user-form) Commented May 10, 2017 at 10:40
  • @Patrick this is how exactly I am getting url in my project: @Controller @RequestMapping(value = "/admin") @PropertySource("classpath:application.properties") public class AdminController { //some other code @RequestMapping("/submit-murojaat") public String newMurojat(Model model) { model.addAttribute("caltest", new Caltest()); model.addAttribute("action", "/save-new-murojaat"); return "murojaatNewS"; } } Commented May 10, 2017 at 10:44

1 Answer 1

3

There are many different ways to do this... the simplest, is to use the same template in the controllers, and in each controller pass a variable that contains the correct action.

For example:

// Moderator controller
@RequestMapping(value = "/moderator")
public String moderator(Map<String, Object> model) {
  model.put("action", "/moderator/new-user-form");
  return "new-user-form";
}

// Admin controller
@RequestMapping(value = "/moderator")
public String moderator(Map<String, Object> model) {
  model.put("action", "/admin/new-user-form");
  return "new-user-form";
}

And in the html

<form th:action="@{${action}}">

If that isn't suitable, you could turn the form itself into a fragment, and then pass the action as a parameter. Something like this:

<!-- Fragment -->
<form th:fragment="userform (action)" th:action="@{${action}}">
  .
  .
  .
</form>

<!-- Including the fragment -->
<div th:replace="fragments/userform :: userform(action='/admin/new-user-form')" />
<!-- or -->
<div th:replace="fragments/userform :: userform(action='/moderator/new-user-form')" />
Sign up to request clarification or add additional context in comments.

1 Comment

This worked perfectly! <form th:action="@{${action}}"> Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.