Html SpringMVC控制器不处理来自Thymeleaf的提交
我为用户注册创建了简单的Thymeleaf表单模板。 当我点击submit form按钮时,POST请求被发送,正如我在浏览器日志中看到的一样,但它从来没有被saveRegistration控制器方法处理过,该方法用method=RequestMethod.POST注释 弹簧版本4.2.2,弹簧安全4.0.2,弹簧启动机1.2.7 模型对象使用Hibernate验证器(如果有意义的话) 控制器代码:Html SpringMVC控制器不处理来自Thymeleaf的提交,html,spring,spring-mvc,thymeleaf,Html,Spring,Spring Mvc,Thymeleaf,我为用户注册创建了简单的Thymeleaf表单模板。 当我点击submit form按钮时,POST请求被发送,正如我在浏览器日志中看到的一样,但它从来没有被saveRegistration控制器方法处理过,该方法用method=RequestMethod.POST注释 弹簧版本4.2.2,弹簧安全4.0.2,弹簧启动机1.2.7 模型对象使用Hibernate验证器(如果有意义的话) 控制器代码: package org.vmtest.web.controller; import org.s
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>
我认为这是可行的,但是你没有在用户登录页面上打印成功消息 这是因为您正在设置模型属性并重定向到另一个页面
model.addAttribute("success", "Dear "+ user.getFirstName()+" , your Registration completed successfully");
return "redirect:/login";
我认为,如果发生这种情况,您将无法将model属性传递到重定向页面
相反,您可以使用重定向属性
您是否在控制台/日志中看到任何错误?控制台中的错误是什么?
@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}";
}