Java Spring MVC error 400 - Search for 2 fields

0

Galera, blz?!

Next, I'm finalizing a project for college completion and I'm getting a very strange error. I already analyzed with another class friend, but we could not identify.

I need to search for two fields, one field per title, and another field for price. The two searches can be combined. When searching by price, I get the query successfully, when I search by title and price, I get the query 100%; however, when I perform the search only by title, I get the error 400. I honestly do not know what's going on. Following is the code and error prints. Thanks in advance for any help.

Myform

<formclass="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();
}
    
asked by anonymous 10.06.2016 / 06:01

0 answers