Victor Mikhailov
Victor Mikhailov

Reputation: 125

Spring MVC controller doesn't handle submit from Thymeleaf

I created simple Thymeleaf form template for user registration. When I hit submit form button, POST request is sent (as I see from browser log), but it never handled by saveRegistration controller method, annotated with (method = RequestMethod.POST)

Spring version 4.2.2 , Spring security 4.0.2, Spring boot starter 1.2.7 Model object uses Hibernate validators (if it make sense)

Controller code:

package org.vmtest.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.vmtest.web.model.User;

import javax.validation.Valid;
import java.util.*;

/**
 * Created by victor on 24.10.15.
 */
@Controller
@RequestMapping("/register")
public class RegistrationController {

    @RequestMapping(method = RequestMethod.GET)
    public String newRegistration(ModelMap model) {
        User user = new User();
        model.addAttribute("user", user);
        return "register";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String saveRegistration(@Valid User user, BindingResult result, ModelMap model){

        if(result.hasErrors()) {
            return "register";
        }

        model.addAttribute("success", "Dear "+ user.getFirstName()+" , your Registration completed successfully");
        return "redirect:/login";
    }

    @ModelAttribute("countries")
    public Map<String, String> initializeCountries() {

        Map<String, String> countries = new TreeMap<>();

        String[] locales = Locale.getISOCountries();
        for (String countryCode : locales) {
            Locale obj = new Locale("", countryCode);
            countries.put(obj.getDisplayCountry(), obj.getCountry());
        }

        return countries;
    }

}

Thymeleaf template:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
    <style>
        form {
            position: absolute;
            top: 20%;
            left: 40%;
            margin-top: -50px;
            margin-left: -50px;
            width: 300px;
            height: 100px;
        }
    </style>
</head>
<body>
<div>
    <form action="#" th:action="@{/register}" th:object="${user}" method="post">
        <div>
            <h2>New user registration</h2>
        </div>
        <div>
            <table style="width:300px">
                <tr>
                    <td>Login</td>
                    <td><input type="text" th:field="*{userName}" name="username"/></td>
                </tr>
                <tr>
                    <td>Password</td>
                    <td><input type="password" th:field="*{password}" name="password"/></td>
                </tr>
                <tr>
                    <td>First Name</td>
                    <td><input type="text" th:field="*{firstName}" name="firstname"/></td>
                </tr>
                <tr>
                    <td>Last Name</td>
                    <td><input type="text" th:field="*{lastName}" name="lastname"/></td>
                </tr>
                <tr>
                    <td>E-Mail</td>
                    <td><input type="text" th:field="*{email}" name="email"/></td>
                </tr>
                <tr>
                    <td>Street address</td>
                    <td><input type="text" th:field="*{street}" name="street"/></td>
                </tr>
                <tr>
                    <td>City</td>
                    <td><input type="text" th:field="*{city}" name="city"/></td>
                </tr>
                <tr>
                    <td>State/province</td>
                    <td><input type="text" th:field="*{state}" name="state"/></td>
                </tr>
                <tr>
                    <td>Country</td>
                    <td>
                        <select th:field="*{country}">
                            <option th:each="sopt:${countries.entrySet()}"
                                    th:value="${sopt.value}" th:text="${sopt.key}">Japan</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>Post code</td>
                    <td><input type="text" th:field="*{postCode}" name="postcode"/></td>
                </tr>
                <tr>
                    <td>Phone number</td>
                    <td><input type="text" th:field="*{phone}" name="phone"/></td>
                </tr>

                <tr>
                    <td>Don't have account?</td>
                    <td><button type="submit">Register</button></td>
                </tr>
            </table>
        </div>
    </form>
</div>

</body>
</html>

Upvotes: 0

Views: 1720

Answers (1)

Faraj Farook
Faraj Farook

Reputation: 14885

I think it works but you dont get the success message printed in the user login page.

It's because you are setting a model attribute and redirecting to another page.

  model.addAttribute("success", "Dear "+ user.getFirstName()+" , your Registration completed successfully");
  return "redirect:/login";

I don't think if this happens, you will get the model attribute pass to the redirected page.

Rather you may use Redirect Attributes https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/mvc/support/RedirectAttributes.html

@RequestMapping(method = RequestMethod.POST)
 public String saveRegistration(@Valid User user, BindingResult result, RedirectAttributes redirectAttrs) {
    if(result.hasErrors()) {
        return "register";
    }
    redirectAttrs.addAttribute("success", "Dear "+ user.getFirstName()+" , your Registration completed successfully");
    return "redirect:/accounts/{id}";
 }

Upvotes: 0

Related Questions