Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 从表单提交中获取空值_Spring_Forms_Spring Boot_Form Submit_Modelattribute - Fatal编程技术网

Spring 从表单提交中获取空值

Spring 从表单提交中获取空值,spring,forms,spring-boot,form-submit,modelattribute,Spring,Forms,Spring Boot,Form Submit,Modelattribute,在我当前的项目中,我正在尝试实现一个用户注册表单,但是每次我尝试向服务器提交用户数据时,模型对象都保持空值 这是我的表格: <form class="form-container" id="form" method="post" th:object="${command}" th:action="@{/usuario/register}"> <div class="form-row"> <div class="col-75"> <

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

这是我的表格:

<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>
(我也试过:
doRegister(@Valid Usuario object,BindingResult)
但也不起作用-同样的问题)

在我的服务课上,我得到了以下代码:

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

通过改变参数,我成功地解决了这个问题:

@ModelAttribute("usuario") Usuario object
致:


在某种程度上,ModelAttribute的名称与方法
公共字符串formRegister(Model Model)
中使用的名称相同。您好,请附上更多信息?JavaScript register()函数除了提交表单外还做什么?您是否检查了浏览器网络控制台,以便确定在POST请求正文中向服务器发送了哪些数据?你查过e了吗。G比较一下?这里有一个简单的输入提交按钮——您也尝试过这个变体吗?@PetrBodnár添加了处理表单提交的javascript函数。是的,我检查了浏览器网络控制台,所有参数都显示正确。另外,您是否100%确定“模型对象保持空值”。它的字段不是保持为空(“…具有空值”)吗?那么您的问题的答案可能在这里:。@PetrBodnár No.我分别检查每个参数(使用BindingResult)和对象本身(在控制器中)。两者都接收到一个空值。@PetrBodnár并且关于您指示我的链接,我有一个InitBinder方法,它为我的所有模型层提供PropertyEditorSupport类。
@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