Springwebmvc - - Search Data with @Modelatribute

Asked

Viewed 130 times

0

Hello, I have the following method:

@RequestMapping(value = "/pesquisa", method = RequestMethod.POST)
public String pesquisa(@ModelAttribute("relSintetico") @Valid RelSintetico relSintetico, BindingResult bindingResult) {

    Timestamp dataInicial =  relSintetico.getDataInicial();

    Timestamp dataFinal = relSintetico.getDataFinal();

    System.out.println("Pesquisando");
    System.out.println(dataInicial);


    List<VE_RelResumoArrecadacao> relatorio = repositorioSintetico.findByDataOperacao(dataInicial, dataFinal);

    System.out.println("Pesquisado!");
    System.out.println(relatorio.size());


    return "sintetico.relatorios.tiles";
}

Such method takes the "modelAtribute" relay from the page below:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>

Relatório Sintético


This is the Relsintetico Model class:

package br.com.apasi.spring.dominios;

import java.io.Serializable; import java.sql.Timestamp;

import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table;

@Entity @Table(name = "Relsintetico", Catalog = "Pedagio", schema = "dbo") public class Relsintetico Serializable Mplements {

/**
 * 
 */
private static final long serialVersionUID = 1L;

@Id
@Column(name = "IdRelSintetico")
private Integer idRelSintetico;

@Column(name = "DataInicial")
private Timestamp dataInicial;

@Column(name = "DataFinal")
private Timestamp dataFinal;

public Integer getIdRelSintetico() {
    return idRelSintetico;
}

public void setIdRelSintetico(Integer idRelSintetico) {
    this.idRelSintetico = idRelSintetico;
}

public Timestamp getDataInicial() {
    return dataInicial;
}

public Timestamp getDataFinal() {
    return dataFinal;
}

public void setDataInicial(Timestamp dataInicial) {
    this.dataInicial = dataInicial;
}

public void setDataFinal(Timestamp dataFinal) {
    this.dataFinal = dataFinal;
}

}

But the method returns null for getDataInitial and getDataFinal... Is it something to do with data conversion? or am I taking this data from the Model wrong?

1 answer

1


Enter the annotation @org.springframework.format.annotation.DateTimeFormat date attributes giving the format, adding javax.persistence.Temporal and changing the type of timestamp to java.util.Date in the example below:

@DateTimeFormat(pattern = "dd/MM/yyyy")
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "DataInicial")
private Date dataInicial;

@DateTimeFormat(pattern = "dd/MM/yyyy")
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "DataFinal")
private Date dataFinal;
  • Still Returns null in variables :(

  • Put the JSP where the form is... In my project it is mapped as java.util.Date, it can be that too... I re-edited the answer, see if it works now...

  • <c:url var="actionPsearch" value="/reports/search"></c:url> <div> <form:form action="${actionPesquisar}" modelAttribute="relSintetico" method="post"> <div class="Row"> <div class="col-Md-6"> <div class="form-group"> <form:input path="startData" type="date" cssClass="form-control" /> </div> </div> </div> <div class="Row"> <div class="col-Md-6"> <div class="form-group"> <form:input path="dataFinal" type="date" cssClass="form-control" />

  • I switched to Date and still comes as null

  • Tenta colocar o objeto no ModelAndView antes de carregar a página, tipo: ModelAndView modelAndView = new ModelAndView("viewName"); modelAndView.addObject("relSintetico", new RelSintetico()); no mais não vejo porque não está funcionando...

  • this type="date" tag does not exist in <form:input> To trying to solve this problem.

  • God right buddy... No more return with value Null, what you said was right, the problem is that I did not erase the get and set with Timestamp kkkk Valew

Show 2 more comments

Browser other questions tagged

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