从表单提交中获取空值

mcvgt66p  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(315)

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

<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) 但也不起作用(同样的问题)
在我的服务课上,我得到了以下代码:

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

yquaqz181#

我通过改变参数解决了这个问题:

@ModelAttribute("usuario") Usuario object

收件人:

@ModelAttribute("command") Usuario object

使modelattribute的名称与方法中使用的名称相同 public String formRegister(Model model) .

相关问题