我正在使用 Tiles 3、JPA 和所有其他很酷的东西学习 Spring MVC。我创建了一个表单来发送数据,下面是表单:
<form:form method="POST" action="/users/save" enctype="multipart/form-data" modelAttribute="formUsuario">
<form:label path="username" for="username">Username</form:label>
<form:input type="text" id="username" path="username" class="span4" value="${user.username}" />
<form:label path="firstName" for="firstName">First name</form:label>
<form:input type="text" id="firstName" path="firstName" class="span4" value="${user.firstName}" />
</form:form>
控制器内部添加新记录的方法:
@Secured("ROLE_USER")
@RequestMapping(value = "/new", method = RequestMethod.GET)
public String getNew(@ModelAttribute("formUsuario") br.com.rodrigoferra.domain.User user) {
logger.info("Add new user");
return "users/edit";
}
保存新记录的方法:
@Secured("ROLE_USER")
@RequestMapping(value = "/save", method = RequestMethod.POST)
public @ResponseBody String doSave(@ModelAttribute("formUsuario") br.com.rodrigoferra.domain.User user, BindingResult results, Model model) {
if(results.hasErrors()) {
return "users/edit";
}
user = userRepository.save(user);
logger.info("Username: " + user.getUsername());
return "redirect:/users/";
}
它正在保存记录,但它将所有内容都保存为 Null!控制器正在接收来自表单的数据。我搜索了很多并找到了与此相同的示例,但我无法让我的工作!
有没有我错过的配置?
感谢和抱歉我的英语不好。
编辑:
用户 bean:
@Entity(name="users")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY)
private Long id;
private String firstName;
private String lastName;
@Column(unique=true)
private String username;
private String password;
@Transient
private String passwordConfirmation;
private Date birthdate;
private String signature;
private String email;
private String sex;
private int active;