'Send multipartfile jsp spring mvc to controller
I have a JSP project with Spring MVC. And I have a screen with a registration form (CRUD). I put an input file field to attach files. And I'm trying to send the attached file to the Model. But without success. Could someone tell me what is wrong with my implementation?
index.jsp:
<div class="modal fade" id="modalNovoResultado" tabindex="-1" role="dialog"
aria-labelledby="novoResultadoModal" aria-hidden="true">
<div class="modal-dialog" role="document">
<form:form method="POST" id="newResultado" modelAttribute="exameForm" action="criarResultado.do">
<div class="modal-content novo-resultado" style="padding: 0px !important;" >
<div class="modal-line novo-resultado"></div>
<div class="modal-header novo-resultado">
<p class="modal-title novo-resultado" id="exampleModalLabel" style="margin-top:10px;">Novo
resultado de exame</p>
</div>
<div class="modal-body pb-0">
<div class="container">
<div class="row mt-1">
<div class="form-group text-left col-md-6 tipo-exame">
<label class="modal-resultado">
Tipo de exame: <span class="required">*</span>
</label>
<form:select class="selectpicker form-control" id="tipoExameNew" path="tipoExame">
<form:option value="" selected hidden>Selecione</form:option>
<form:option value="999" selected hidden>Antígeno SarsCovid-19</form:option>
</form:select>
</div>
<div class="form-group text-left col-md-6 mb-0">
<label class="modal-resultado">
Anexe de um a três arquivos: <span class="required">*</span>
<img class="anexar-exame"
src="/PCBS-GestaoNovamed/includes/img/icon-nav-anexar.svg" alt=""
data-toggle="modal">
<form:input path="arquivo" id="fileModal" type="file" name="arquivo"/>
</label>
<label class="anexo-subtitle" style="font-size: 12px;">Arquivos PDF, JPG ou PNG, com
no máximo 10mb</label>
</div>
</div>
<div class="row">
<div style="margin-left: 230px;">
<div class="row botoes-formulario" style="float: right;">
<input type="submit" class="add-unidade-button cadastrar" value="Cadastrar"
onclick="validaForm()"/>
</div>
</div>
</div>
</div>
</div>
</div>
</form:form>
</div>
</div>
javascript:
function adicionarExame(element, event) {
event.preventDefault();
if (validaForm() > 0) return;
var form = document.getElementById('newResultado');
console.log("form:" + $(form).serialize());
$.ajax({
method: 'POST',
url: element.action,
data: $(form).serialize(),
cache: false,
success: function (status) {
Swal.fire({
customClass: {
title: 'title-class',
popup: 'popup-class popup-green',
content: 'content-class',
actions: 'actions-class-green'
},
type: 'success',
title: 'Resultado cadastrado com sucesso.',
text: ''
}).then(function () {
//window.location.reload();
})
},
error: function (xhr, ajaxOptions, thrownError) {
console.log(thrownError);
var msgErro = 'Erro inesperado';
if (xhr.status === 400) {
msgErro = 'Erro ao cadastrar resultado.';
}
Swal.fire({
customClass: {
title: 'title-class',
popup: 'popup-class popup-red',
content: 'content-class',
actions: 'actions-class-red'
},
type: '',
title: msgErro,
text: ''
})
}
});
}
controller:
@RequestMapping(value = "criarResultado.do", method = RequestMethod.POST)
public ResponseEntity<ExameVO> cadastrarNovoExame(
@Valid @ModelAttribute(Constants.MA_MODEL_RESULTADO_EXAME) ExameForm exameForm,
BindingResult bindingResult,
Model model) {
LOGGER.error("entrou em criarResultado form" + exameForm.toString());
}
model:
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class ExameForm implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
//@NotNull(message = Constants.MSG_ERRO_CAMPO_OBRIGATORIO)
private String cpf;
//@NotNull(message = Constants.MSG_ERRO_CAMPO_OBRIGATORIO)
//@DateTimeFormat(pattern = "dd/MM/yyyy")
private String dataNascimento;
//@NotNull(message = Constants.MSG_ERRO_CAMPO_OBRIGATORIO)
private Long tipoExame;
//@NotNull(message = Constants.MSG_ERRO_CAMPO_OBRIGATORIO)
//private List<ExameArquivoVO> resultadoExameArquivos;
private MultipartFile arquivo;
private String observacao;
private String excluido;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCpf() {
return cpf;
}
public void setCpf(String cpf) {
this.cpf = cpf;
}
public String getDataNascimento() {
return dataNascimento;
}
public void setDataNascimento(String dataNascimento) {
this.dataNascimento = dataNascimento;
}
public Long getTipoExame() {
return tipoExame;
}
public void setTipoExame(Long tipoExame) {
this.tipoExame = tipoExame;
}
public String getObservacao() {
return observacao;
}
public void setObservacao(String observacao) {
this.observacao = observacao;
}
public String getExcluido() {
return excluido;
}
public void setExcluido(String excluido) {
this.excluido = excluido;
}
/*
public List<ExameArquivoVO> getResultadoExameArquivos() {
return resultadoExameArquivos;
}
public void setResultadoExameArquivos(List<ExameArquivoVO> resultadoExameArquivos) {
this.resultadoExameArquivos = resultadoExameArquivos;
}
*/
public MultipartFile getArquivo() {
return arquivo;
}
public void setArquivo(MultipartFile arquivo) {
this.arquivo = arquivo;
}
public ExameVO toVo() {
ExameVO vo = new ExameVO();
vo.setId(this.getId());
vo.setCpf(this.getCpf());
vo.setDataNascimento(this.getDataNascimento());
vo.setTipoExame(this.getTipoExame());
vo.setObservacao(this.getObservacao());
vo.setExcluido(this.getExcluido());
//vo.setResultadoExameArquivos(this.getResultadoExameArquivos());
vo.setFile(this.getArquivo());
return vo;
}
@Override
public String toString() {
return "ExameForm [id=" + id + ", cpf=" + cpf + ", dataNascimento=" + dataNascimento + ", tipoExame="
+ tipoExame + ", arquivo=" + arquivo + ", observacao=" + observacao + ", excluido=" + excluido + "]";
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
