Spring Model 4.3.1

3

I do not know how to work 100% with #POST and #GET yet, my problem should be in understanding how requests occur and when my objects are instantiated. Here are 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";

    }

}

alter.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 of Inbox, resulting in the registry data in the fields.

However when I try to save DfeEmitido , IdCaixaOperação goes null.

    
asked by anonymous 07.08.2018 / 14:37

1 answer

4

You are making a request from GET to /caixa/alterar and you are not passing the value of the operation box id to the end of the URL, so you get null .

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

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

There are some strange things in the code as well regarding the good use of REST. Normally, you do not use GET to make changes, use PUT :

@RequestMapping(value = "/alterar/{idCaixaOperacao}", method = RequestMethod.PUT)
    
08.08.2018 / 23:57