Spring MVC(文本字段和按钮)使用CrudRepository和Thymeleaf

问题描述 投票:0回答:1

我需要使用CrudRepository通过自定义方法创建一个简单的搜索按钮,并在网站上显示搜索结果。

event.Java

@Entity
@Table(name = "events")
public class Event {
    private String name;
}

event repository.Java

public interface EventRepository extends CrudRepository<Event, Long>{
    public Iterable<Event> findByName(String name);
}

event service.Java

public interface EventService {
    public Iterable<Event> findByName(String name);
}

event service imp了.Java

@Service
public class EventServiceImpl implements EventService {

    @Override
    public Iterable<Event> findByName(String name) {
        return eventRepository.findByName(name);
    }
}

events controller.Java

@Controller
@RequestMapping(value = "/events", produces = { MediaType.TEXT_HTML_VALUE })
public class EventsController {

    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public String showEventsByName(@ModelAttribute Event event, @RequestParam (value = "search", required = false) String name, Model model) {
        model.addAttribute("event", event);
        model.addAttribute("searchResult", eventService.findByName(name));

        return "events/index";
}

的index.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorate="~{layouts/default}">
<head>
  <title>All events</title>
</head>
<body>
  <div layout:fragment="content">
    <h3>Search for an event by name</h2>
    <form th:object="${event}" th:action="@{/events/search}" method="get">
        <input type="text" name="search" id="search" th:value="${search}"/>
        <input type="submit" value="Search"/>
        <div th:if="${not #lists.isEmpty(search)}">
            <h2>Events search results</h2>
            <table class="table table-striped table-hover">
                <thead>
                    <tr>
                      <th><i class="fa fa-bolt"></i> Event</th>
                      <th><i class="fa fa-map-marker"></i> Venue</th>
                      <th><i class="fa fa-calendar"></i> Date</th>
                      <th><i class="fa fa-clock-o"></i> Time</th>
                    </tr>
                </thead>
                <tbody>
                    <tr th:each="event: ${searchResult}">
                      <td th:text="${event.name}"><a href="/event/${event.name}">My Event</a></td>
                      <td th:text="${event.venue.getName()}">Event venue</td>
                      <td th:text="${{event.date}}">Event date</td>
                      <td th:text="${{event.time}}">Event time</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </form>
    <h1>All events</h1>

    <table class="table table-striped table-hover">
      <thead>
        <tr>
          <th><i class="fa fa-bolt"></i> Event</th>
          <th><i class="fa fa-map-marker"></i> Venue</th>
          <th><i class="fa fa-calendar"></i> Date</th>
          <th><i class="fa fa-clock-o"></i> Time</th>
        </tr>
      </thead>
      <tbody>
        <tr th:each="e : ${events}">
          <td th:text="${e.name}">My Event</td>
          <td th:text="${e.venue.getName()}">Event venue</td>
          <td th:text="${{e.date}}">Event date</td>
          <td th:text="${{e.time}}">Event time</td>
        </tr>
      </tbody>
    </table>

  </div>

</body>
</html>

我使用自定义CrudRepository方法的命名约定,这意味着实现应该由接口提供。当我运行Web应用程序并在表单中提交一些输入时,会显示所需的html视图,但搜索结果从不显示任何事件。 Picture 1 Picture 2

我尝试删除@ModelAttribute Event事件和model.addAttribute("event", event);,因为在this post which addresses the same issue它没有使用,但结果是相同的。

我知道已经有一个类似的帖子,但解决方案表明这对我不起作用,我尝试了不同的方法,但结果并不像预期的那样。

java spring model-view-controller thymeleaf
1个回答
0
投票
eventService.findByName(name);

将寻找确切的名称匹配

创建一种新的搜索方法

服务接口

public interface EventService {
    public Iterable<Event> findByName(String name);
    public Iterable<Event> searchForName(String name);
}

知识库

public interface EventRepository extends CrudRepository<Event, Long>{
    public Iterable<Event> findByName(String name);
    public Iterable<Event> findByNameContainingIgnoreCase(String name);
}

服务类

@Service
public class EventServiceImpl implements EventService {

    @Override
    public Iterable<Event> findByName(String name) {
        return eventRepository.findByName(name);
    }

    @Override
    public Iterable<Event> searchForName(String name) {
        return eventRepository.findByNameContainingIgnoreCase(name);
    }

}

更新:

什么都不搜,你想要什么都不显示?我更喜欢后者。如果您不想显示任何内容,请在调用服务之前检查搜索参数。

@RequestMapping(value = "/search", method = RequestMethod.GET)
public String showEventsByName(@RequestParam (value = "search", required = false) String name, Model model) {
    if(name!=null && !name.isEmpty()){
       model.addAttribute("searchResult", eventService.searchForName(name));
    } // else{ } create an empty list or handle null in view
    // ...
}

如果你想隐藏表头,如果没有结果使用

th:if="${searchResult !=null and !searchResult.isEmpty()}" <!-- For Both -->
© www.soinside.com 2019 - 2024. All rights reserved.