Java Spring MVC error 400 - Search for 2 fields

Asked

Viewed 209 times

0

You guys, blz?!

Next, I’m finalizing a project to complete a chair in college and I’m getting a very strange mistake. I’ve already checked with another classmate, but we can’t identify.

I need to search for two fields, one field per title and another field per price. The two searches can be combined. When searching for price, I get the query performed successfully, when I search for title and price, I get the query 100%; however, when I search only for title, I get error 400. I honestly don’t know what’s going on. Below are the code and error prints. Thanks for any help.

inserir a descrição da imagem aqui

My form

<form class="form-inline" action='<c:url value="/pesquisarDeslogado"></c:url>' method="post"
                        style="margin-left: 300px;">
                        <label>Anúncio</label> 
                        <input type="text" class="form-control" name="titulo" placeholder="Título do anúncio" /> 
                        <input type="number" class="form-control" name="valor" placeholder="Preço" />
                        <button type="submit" class="btn btn-primary">Pesquisar</button>
                    </form>

My controller

@RequestMapping(value = "/pesquisarDeslogado", method = RequestMethod.POST)
public ModelAndView pesquisa(Anuncio anuncio, RedirectAttributes redirect){
    ModelAndView mav = new ModelAndView("/todosAnunciosDeslogado");

    List<Anuncio> anuncios = anuncioDao.pesquisar(anuncio);
    if(anuncios.size()>0){
        mav.addObject("todosAnuncios", anuncios);
    }else{
        redirect.addFlashAttribute("vazio",true);
        mav.addObject("todosAnuncios", anuncioDao.findAll());
    }

    return mav;
}

My search

public List<Anuncio> pesquisar(Anuncio anuncio){
    StringBuffer query = new StringBuffer("select a from anuncio as a where 1=1");

    if(!anuncio.getTitulo().isEmpty()){
        query.append(" and a.titulo like :titulo");
    }
    if(anuncio.getValor() != 0){
        query.append(" and a.valor = :valor");
    }
    query.append(" order by a.id desc");

    Query q = this.manager.createQuery(query.toString(),Anuncio.class);

    if(!anuncio.getTitulo().isEmpty()){
        q.setParameter("titulo", "%"+ anuncio.getTitulo() +"%");
    }
    if(anuncio.getValor() != 0){
        q.setParameter("valor", anuncio.getValor());
    }

    return q.getResultList();
}
  • Post the Tomcat log for better analysis.

  • @Geferson does not appear anything in the Tomcat log... By debugging, the request does not even reach the controller. It must be a bug.

  • Try adding simple quotes on this line, leaving it this way: q.setParameter("title", "'%"+ advertisement.getTitulo() +"%'");

No answers

Browser other questions tagged

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