Returning NULL in Spring Boot

Asked

Viewed 248 times

0

My page is like this;

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" 
xmlns:th="http://www.thymeleaf.org">

<body>
    <h1>Olá! Thymeleaf configurado!</h1>
</body>

    <form method="POST">
        <label for="numero">numero</label>
        <input type="text" id="numero" name="nomero"/>


        <input type="submit" value="Salvar"/>
    </form>

</html>

And my controller is like this;

package com.controle.boleto.controller;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.controle.boleto.model.Boleto;

@Controller
public class BoletoController {


    @RequestMapping("/boleto/novo")
    public String novo(){
        return "boleto/CadastroBoleto";
    }


    @RequestMapping(value="/boleto/novo", method = RequestMethod.POST)
    public String cadastrar(@Valid Boleto boleto, BindingResult result ){

        if(result.hasErrors()){
            System.out.println("tem erro sim");
        }

        System.out.println(">>>>> numero " + boleto.getNumero());
        return "redirect:/boleto/novo";
    }


}

my model is like this;

@NotBlank
private String numero;
private BigDecimal valor;
private String comentario;

public String getNumero() {
    return numero;
}

public void setNumero(String numero) {
    this.numero = numero;
}

public BigDecimal getValor() {
    return valor;
}

public void setValor(BigDecimal valor) {
    this.valor = valor;
}

public String getComentario() {
    return comentario;
}

public void setComentario(String comentario) {
    this.comentario = comentario;
}

The getNumer is returning null, is someone seeing something I’m not seeing?

2 answers

4


Your input is wrong.

Change: <input type="text" id="numero" name="nomero"/>

For: <input type="text" id="numero" name="numero"/>

  • 1

    thank you very much, I messed up badly.

1

Spring Boot - Form Submission

In the controller is missing the creation of the Boleto object. Something similar as in the example:

@GetMapping("/greeting")
public String greetingForm(Model model) {
    model.addAttribute("greeting", new Greeting());
    return "greeting";
}

Where you have the creation of the object "Greeting".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.