Spring MVC控制器不处理来自Thymeleaf的提交

时间:2015-10-25 20:28:09

标签: html spring spring-mvc thymeleaf

我为用户注册创建了简单的Thymeleaf表单模板。 当我点击提交表单按钮时,发送POST请求(正如我从浏览器日志中看到的那样),但它从未由saveRegistration控制器方法处理,用(method = RequestMethod.POST)注释

Spring version 4.2.2,Spring security 4.0.2,Spring boot starter 1.2.7 模型对象使用Hibernate验证器(如果有意义)

控制器代码:

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模板:

<!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>

1 个答案:

答案 0 :(得分:0)

我认为它有效但你没有在用户登录页面上打印成功消息。

这是因为您正在设置模型属性并重定向到另一个页面。

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

我不认为如果发生这种情况,您会将模型属性传递给重定向的页面。

相反,您可以使用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}";
 }