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

时间:2019-02-28 14:46:03

标签: java spring model-view-controller thymeleaf

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

Event.java

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

EventRepository.java

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

EventService.java

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

EventServiceImpl.java

@Service
public class EventServiceImpl implements EventService {

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

EventsController.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

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

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

1 个答案:

答案 0 :(得分: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
    // ...
}