在我当前的项目中,我正在尝试实施用户注册表格,但是每次尝试将用户数据提交到服务器时,模型对象都将保留为空值。
这是我的表单:
<form class="form-container" id="form" method="post" th:object="${command}" th:action="@{/usuario/register}">
<div class="form-row">
<div class="col-75">
<input type="text" th:field="*{username}" placeholder="Login"/>
</div>
</div>
<div class="form-row">
<div class="col-75">
<input type="password" th:field="*{password}" placeholder="Senha"/>
</div>
</div>
<div class="form-row">
<div class="col-75">
<input type="text" th:field="*{firstName}" placeholder="Nome"/>
</div>
</div>
<div class="form-row">
<div class="col-75">
<input type="text" th:field="*{lastName}" placeholder="Sobrenome"/>
</div>
</div>
<div class="form-row">
<div class="col-75">
<input type="email" th:field="*{email}" placeholder="E-mail"/>
</div>
</div>
<div class="form-row">
<div class="col-75">
<button type="button" class="button" onclick="register();">Cadastre-se</button>
</div>
</div>
<div class="alert-ok" id="ok" style="display: none;">
<span class="closebtn" onclick="this.parentElement.style.display='none';">×</span>
<strong>Sucesso!</strong> Cadastro realizado com sucesso.
</div>
<div class="alert-error" id="error" style="display: none;">
<span class="closebtn" onclick="this.parentElement.style.display='none';">×</span>
<strong>Erro!</strong> Não foi possivel realizar o cadastro.
</div>
</form>
在我拥有的控制器中:
@RequestMapping(value = "/register", method=RequestMethod.GET)
public String formRegister(Model model) {
model.addAttribute("command", new Usuario());
return "register";
}
@RequestMapping(value = "/register", method=RequestMethod.POST)
@ResponseBody
public void doRegister(@ModelAttribute("usuario") Usuario object) throws Exception {
this.serv.register(object);
}
(我也尝试过doRegister(@Valid Usuario object, BindingResult result)
,但也不起作用-同样的问题)
在服务类中,我得到了以下代码:
public void register(Usuario novo) throws Exception {
novo.setEnabled(true);
novo.setLocked(false);
novo.setCredenciais(new HashSet<Credencial>());
Credencial credencial = credencialDao.findBy("nome", "web");
novo.getCredenciais().add(credencial);
this.dao.insert(novo);
}
我的模型班:
@Entity
public class Usuario extends Model implements UserDetails {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@Column
private String username;
@Column
private String password;
@Column
private String firstName;
@Column
private String lastName;
@Column
private String email;
@OneToMany(fetch = FetchType.EAGER)
private Set<org.loja.model.credencial.Credencial> credenciais;
@Column
private Date dataExpiracao;
@Column
private Boolean enabled;
@Column
private Boolean locked;
@OneToOne(fetch = FetchType.EAGER)
private org.loja.model.cesta.Cesta cesta;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "usuario")
private Set<org.loja.model.pedido.Pedido> pedidos;
}
任何人都可以看到这里有什么问题吗?我在这个问题上呆了几个小时,但不明白是什么原因导致了这个问题。我认为该代码看起来与我在网络上搜索到的同一操作的其他代码非常相似。
更新
function register() {
var formData = new FormData(document.getElementById("form"));
var url = document.getElementById("form").action;
var xhr = new XMLHttpRequest();
xhr.open("POST", url);
xhr.onload = function(event) {
event.preventDefault();
var result = this.responseText;
if(result == "")
document.getElementById("ok").style.display = 'block';
else
document.getElementById("error").style.display = 'block';
};
xhr.send(formData);
}
答案 0 :(得分:0)
我设法解决了更改参数的问题:
@ModelAttribute("usuario") Usuario object
收件人:
@ModelAttribute("command") Usuario object
以某种方式使ModelAttribute的名称与方法public String formRegister(Model model)
中使用的名称相同。