Spring搜索功能在未输入任何搜索内容时返回完整列表。

huangapple go评论124阅读模式
英文:

Spring search function returning full list when nothing is searched

问题

在我的Spring应用程序中,我已经添加了一个搜索功能,用户可以在数据库中搜索姓名。它能够正常工作,除了当我搜索名称或类似名称时,但是当我不输入任何内容进行搜索时,它应该返回空,但实际上返回了完整的姓名列表。

我在逻辑中设置了如果列表为空则返回null,那么我的逻辑中有什么问题?

从测试和使用"findbyname" SQL查询来看,我认为问题在于我的SQL语句。

控制器方法如下:

  1. @RequestMapping("search")
  2. public ModelAndView SearchBeers(@RequestParam("searchTerm") String searchTerm) {
  3. return new ModelAndView("/Searchbeers", "beerList", service.Search(searchTerm));
  4. }

服务方法如下:

  1. public List<Beers> Search(String searchTerm) {
  2. EntityManager em = DBUtil.getEMF().createEntityManager();
  3. List<Beers> list = null;
  4. try {
  5. list = em.createNamedQuery("Beers.findByLikeName", Beers.class)
  6. .setParameter("name", searchTerm)
  7. .getResultList();
  8. if (list == null || list.isEmpty()) {
  9. list = null;
  10. }
  11. } finally {
  12. em.close();
  13. }
  14. return list;
  15. }

使用的SQL语句如下:

  1. @NamedQuery(name = "Beers.findByLikeName", query = "SELECT b FROM Beers b WHERE b.name LIKE CONCAT('%', :name, '%')")

用于显示列表的JSP如下:

  1. <c:forEach items="${beerList}" var="beer">
  2. <tr>
  3. <td>${beer.id}</td>
  4. <td>${beer.name}</td>
  5. <td>${beer.abv}</td>
  6. <td>
  7. <form action="/Assignment3/beer/viewBeer">
  8. <input type="hidden" name="id" value="${beer.id}"/>
  9. <input type="submit" value="<spring:message code="label.viewDetails"/>"/>
  10. </form>
  11. </td>
  12. </tr>
  13. </c:forEach>
英文:

In my spring application, I have added a search function where the user can search the database for a name, it works except when I'm searching for names or similar names, but for when the I enter nothing and search, it should return nothing but returns the full list of names.

I set the list to return null if it is null, what is the flaw in my logic?

from testing and using the findbyname SQL I think it is a flaw in my SQL state

the controller method used is

  1. @RequestMapping(&quot;search&quot;)
  2. public ModelAndView SearchBeers(@RequestParam(&quot;searchTerm&quot;) String searchTerm) {
  3. return new ModelAndView(&quot;/Searchbeers&quot;, &quot;beerList&quot;, service.Search(searchTerm));
  4. }

service method called is

  1. public List&lt;Beers&gt; Search(String searchTerm) {
  2. EntityManager em = DBUtil.getEMF().createEntityManager();
  3. List&lt;Beers&gt; list = null;
  4. try {
  5. list = em.createNamedQuery(&quot;Beers.findByLikeName&quot;, Beers.class)
  6. .setParameter(&quot;name&quot;, searchTerm)
  7. .getResultList();
  8. if (list == null || list.isEmpty()) {
  9. list = null;
  10. }
  11. } finally {
  12. em.close();
  13. }
  14. return list;
  15. }

and the SQL statement used is

  1. @NamedQuery(name = &quot;Beers.findByLikeName&quot;, query = &quot;SELECT b FROM Beers b WHERE b.name LIKE CONCAT(&#39;%&#39;,:name,&#39;%&#39;)&quot;)

the JSP for when displaying the list is

  1. &lt;c:forEach items=&quot;${beerList}&quot; var=&quot;beer&quot;&gt;
  2. &lt;tr&gt;
  3. &lt;td&gt;${beer.id}&lt;/td&gt;
  4. &lt;td&gt;${beer.name}&lt;/td&gt;
  5. &lt;td&gt;${beer.abv}&lt;/td&gt;
  6. &lt;td&gt;
  7. &lt;form action=&quot;/Assignment3/beer/viewBeer&quot;&gt;
  8. &lt;input type=&quot;hidden&quot; name=&quot;id&quot; value=&quot;${beer.id}&quot;/&gt;
  9. &lt;input type=&quot;submit&quot; value=&quot;&lt;spring:message code=&quot;label.viewDetails&quot;/&gt;&quot;/&gt;
  10. &lt;/form&gt;
  11. &lt;/td&gt;
  12. &lt;/tr&gt;
  13. &lt;/c:forEach&gt;

答案1

得分: 1

你有很多方法可以解决这个问题,但是问题是,当字符串为空时,你的查询是:

  1. SELECT b from Beers b Where b.name LIKE &#39;%%&#39;

因此,当有一个空字符串时,你需要做另一个标记,因为LIKE &#39;%%&#39;会找到所有的名称。

附注:不推荐返回空值,最好是返回一个空列表。

英文:

You have many ways to solve that, but the problem is, when the String is empty your query is:

  1. SELECT b from Beers b Where b.name LIKE &#39;%%&#39;

So, you need do antoher flag, when have an empty String, because ... LIKE &#39;%%&#39; find every name.

PD: Not a good practice return a null, is better a empty list.

huangapple
  • 本文由 发表于 2020年3月16日 22:59:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/60708294.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定