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.
The spring has the
HiddenHttpMethodFilter
to work withPUT
/DELETE
in these cases, as you are using the tag library has already got the field.– Bruno César
@Brunocésar, thank you for the correction, I’ve already made the change.
– Dherik