从表单提交中获取空值

Getting Null Value from Form Submission

在我当前的项目中,我正在尝试实现一个用户注册表单,但每次我尝试将用户数据提交到服务器时,模型对象都保持空值。

这是我的表格:

<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';">&times;</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';">&times;</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) 但也不起作用 - 同样的问题)

在我的服务 class 中,我得到了这个代码:

  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);
  }

我的模型class:

@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);
}

我设法通过更改参数解决了这个问题:

@ModelAttribute("usuario") Usuario object

至:

@ModelAttribute("command") Usuario object

在某种程度上使 ModelAttribute 的名称与方法中使用的名称相同 public String formRegister(Model model)