简体   繁体   中英

How to display search results MVC

I'm a beginner and I came across an anomaly that I cant comprehend. From my point of view everything seems to be fine but I keep getting the same empty response when I perform a search in my MVC WebApp.

Take a look at my code. Any help would be greatly appreciated.

MY CONTROLLER

@Controller
public class ControllerACP {

@RequestMapping("/search")
    public String Homea(Model model, @RequestParam(name = "query") String search) {
        
        model.addAttribute("clients", clientRepository.search(search));
        System.out.println(clientRepository.search(search)); // TO DISPLAY CONTENT ON CONSOLE
        System.out.println(search); // ALSO DISPLAY CONTENT ON CONSOLE
        return "search";
            
    }
}

MY REPOSITORY

public interface ClientRepository extends CrudRepository<Client, Integer> {

    @Query(value =  "select * from client where name LIKE ('%:?%')", nativeQuery = true)
    Iterable <Client> search(String search);
}

MY HTML FILE

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SEARCH</title>
<meta content="Dashboard" property="og:title">
<meta content="Dashboard" property="twitter:title">
<meta content="width=device-width, initial-scale=1" name="viewport">
<meta content="Webflow" name="generator">
<link href="css/normalize.css" rel="stylesheet" type="text/css">
<link href="css/webflow.css" rel="stylesheet" type="text/css">
<link href="css/ui-login-teste.webflow.css" rel="stylesheet" type="text/css">
</head>

<body>
  <div class="w-container">
  <h1>Search results</h1>
    <form action="/search" class="w-form">
      <input type="search" class="w-input" autofocus="true" maxlength="256" name="query" placeholder="Search…" id="search">
      <input type="submit" value="Search" class="w-button">
    </form>
 </div>
      
<div class="w-container" th:each="client, interator: ${clients}">
 <a th:href="@{/crud-cliente/pagina-do-cliente/{id}(id=${client.id})}" class="table-content-link w-inline-block">
<div class="valida-table-field foto" >
<div class="table-image-wrapper avatar" th:style="'background-image: url('+@{${'/uploads/' + client.foto}}+')'"></div>
</div>
<div class="valida-table-field nome-admin"  >
   <div class="_20px" th:text="${client.name}">Nome Completo</div>
</div>
<div class="valida-table-field categoria _6">
   <div class="_20px">[[${client.phone}]]</div>
</div>
<div class="valida-table-field categoria _6">
   <div class="_20px">[[${client.email}]]</div>
</div>
<div class="valida-table-field categoria _6">
   <div class="_20px">[[${client.creation_date}]]</div>
</div>
</a>
</div>
</body>
</html>

You can try this query:

@Query(value =  "select * from client where name LIKE :search", nativeQuery = true)
Iterable <Client> search(@Param("search") String search);

I suggest you debug your code and run the query separately in SSMS. Then alter your query and finalize the one where you get your desired output.

Your query should have the searched text between %%. Something Like

@Query(value =  "select * from client where name LIKE ('%"+ searchedText +"%')", nativeQuery = true)

I hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM