Spring Model 4.3.1

Asked

Viewed 61 times

3

I don’t know how to work 100% with #POST and #GET yet, my problem must be in understanding how requests occur and when my objects are instantiated. Follow methods:

CaixaOperacaoController.java

@Controller
@RequestMapping("/caixa")
public class CaixaOperacaoController {

    @Autowired
    private RepositorioCaixaOperacao repositorioCaixaOperacao;

    @Autowired
    private RepositorioDfeEmitidos repositorioDfeEmitidos;

    @Autowired
    private RepositorioRecibo repositorioRecibo;

    @RequestMapping(value = "/pesquisa", method = RequestMethod.GET)
    public String pesquisa() {

        return "caixa.pesquisa.tiles";
    }

    @RequestMapping(value = "/listar", method = RequestMethod.GET)
    public String listar(Model model) {
        List<CaixaOperacao> caixas = repositorioCaixaOperacao.findAll();
        model.addAttribute("caixas", caixas);
        return "caixa.listar.tiles";
    }

    @RequestMapping(value = "/alterar/{idCaixaOperacao}", method = RequestMethod.GET)
    public String alterar(@PathVariable("idCaixaOperacao") Integer idCaixaOperacao, Model model) {
        Recibo reciboAlt = repositorioRecibo.findByIdCaixaOperacao(idCaixaOperacao);
        CaixaOperacao caixaOperacao = repositorioCaixaOperacao.findByIdCaixaOperacao(idCaixaOperacao);

        DfeEmitido dfeEmitido = new DfeEmitido();

        dfeEmitido.setCaixaOperacao(caixaOperacao);

        model.addAttribute("recibo", reciboAlt);
        model.addAttribute("dfeEmitido", dfeEmitido);

        System.out.println(dfeEmitido.getCaixaOperacao().getIdCaixaOperacao());

        return "caixa.alterar.tiles";

    }

    @RequestMapping(value = "/alterar", method = RequestMethod.POST)
    public String alterar(@ModelAttribute("dfeEmitido") @Valid DfeEmitido dfeEmitido, BindingResult result, Model model) {

        System.out.println(dfeEmitido.getCaixaOperacao());

        repositorioDfeEmitidos.save(dfeEmitido);
        return "redirect:/caixa/pesquisa";

    }

}

change.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>


<c:url var="actionEmitirDfe" value="/caixa/alterar"></c:url>
<form:form  modelAttribute="recibo" >

    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <label>Nº Documento Fiscal: </label>
                <form:input path="idCaixaOperacao" cssClass="form-control"
                    disabled="true" />
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <label>Valor: </label>
                <form:input path="valor" cssClass="form-control" disabled="true" />
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <label>Data da Passagem: </label>
                <form:input path="dataOperacao" cssClass="form-control"
                    disabled="true" />
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <label>Forma de Pagamento: </label>
                <form:input path="formaPagamento" cssClass="form-control"
                    disabled="true" />
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <label>Categoria do Veículo: </label>
                <form:input path="categoriaVeiculo" cssClass="form-control"
                    disabled="true" />
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <label>Arrecadador: </label>
                <form:input path="usuario" cssClass="form-control" disabled="true" />
            </div>
        </div>
    </div>
</form:form>

<form:form action="${actionEmitirDfe}" method="post"
    modelAttribute="dfeEmitido">

    <div class="row" hidden="true">
        <div class="col-md-6">
            <div class="form-group">
                <label>Nº Documento Fiscal: </label>
                <form:input path="caixaOperacao.idCaixaOperacao" cssClass="form-control" />
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <label>Placa do Veículo: </label>
                <form:input path="placaVeiculo" cssClass="form-control" />
                <form:errors path="placaVeiculo" cssStyle="color: red"></form:errors>
            </div>
        </div>
    </div>
    <input type="submit" value="EMITIR DFE" class="btn btn-defout">
</form:form>

This page is the result of another page called search, where I put the id Operation, resulting in the record data in the fields.

Yet when I try to save DfeEmitido, the IdCaixaOperação goes nil.

1 answer

4


You’re making a requisition for GET for /caixa/alterar and is not passing the value of the operating box id at the end of the URL, so you get null.

In order to be able to receive the value, you would need to pass the information in the URL. Example, if you wanted to pass the value 123456 as idCaixaOperacao:

<c:url var="actionEmitirDfe" value="/caixa/alterar/123456"></c:url>

There are some strange things in the code also regarding the good use of REST. Normally, it is not used GET to make changes, use PUT:

@RequestMapping(value = "/alterar/{idCaixaOperacao}", method = RequestMethod.PUT)
  • 1

    The spring has the HiddenHttpMethodFilter to work with PUT/DELETE in these cases, as you are using the tag library has already got the field.

  • 1

    @Brunocésar, thank you for the correction, I’ve already made the change.

Browser other questions tagged

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