Home>

I'm using Spring Boot to create a simple web diary of sento tours.
Enter the items I want to search on the search screen (search.html) and display the search results on the list screen (list.html).
I want to do a fuzzy search using the facility name that I have already entered, but even if I enter information on search.html and press search, a list screen (list.html) with no facility information appears. ..
On the other hand, when accessing the list screen (list.html) directly from the separately prepared top page, all the registered facility information will be displayed firmly.

Sometimes there is no error message on Spring Boot, so I am having trouble understanding why it does not work.

Applicable source code
search.html<!DOCTYPE html><html xmlns:th="http://www.thymeleaf.org"><head><meta charset="UTF-8"><title>Search</title></head><body><form th:action="@{/search}" method="post"><p>Search<input type="text" name="key" /><input type="submit" value="search"/></p></form></body></html>
list.html<!DOCTYPE html><html xmlns:th="http://www.thymeleaf.org"><head><meta charset="UTF-8"></head><body>  List  <table>    <tr>        <th>Facility name</th>        <th>Location</th>        <th>Evaluation</th>        <th>Details</th>        </tr>    <tr th:each="e:${list}">        <td th:text="${e.name}"></td>        <td th:text="${e.location}"></td>        <td th:text="${e.reputation}"></td>        <td>Show      </tr></table>  To the top</body></html>
controller
package com.example.demo;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class OfuroController {
    @Autowired
    OfuroRepository repository;
・・・Some parts such as unrelated delete functions are omitted
    @RequestMapping(value = "/list")
    public ModelAndView list(ModelAndView mv) {
        mv.setViewName("list");
        List<OfuroEntity>list = repository.findAll();
        mv.addObject("list", list);
        return mv;
    }
    @RequestMapping(value = "/show/{id}")
    public ModelAndView show(@ModelAttribute OfuroEntity entity, @PathVariable Integer id, ModelAndView mv) {
        mv.setViewName("show");
        Optional<OfuroEntity>show = repository.findById(id);
        mv.addObject("form", show.get());
        return mv;
    }
    @RequestMapping("/search")
    public ModelAndView search(@ModelAttribute("form") OfuroEntity entity, ModelAndView mv) {
        mv.setViewName("search");
        return mv;
}
    @PostMapping(value="/search")
    public ModelAndView search(@RequestParam("key") String key,ModelAndView mv) {
        mv.setViewName("list");
        List<OfuroEntity>list = repository.findByNameLike("%" + key + "%");
        mv.addObject("list", list);
        return new ModelAndView("/list");
    }
}
Repository
package com.example.demo;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface OfuroRepository extends JpaRepository<OfuroEntity, Integer>{
    public List<OfuroEntity>findByNameLike(String key);
}
entity
package com.example.demo;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="ofuro")public class OfuroEntity {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column
private Integer id;
@Column(length=50, nullable=false)
private String name;
@Column(nullable=false)
private String location;
@Column(nullable=false)
private String genre;
@Column(nullable=false)
private String reputation;
@Column(length=400, nullable=true)
private String memo;
public Integer getId() {
    return id;
}
public void setId(Integer id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getLocation() {
    return location;
}
public void setLocation(String location) {
    this.location = location;
}
public String getGenre() {
    return genre;
}
public void setGenre(String genre) {
    this.genre = genre;
}
public String getReputation() {
    return reputation;
}
public void setReputation(String reputation) {
    this.reputation = reputation;
}
public String getMemo() {
    return memo;
}
public void setMemo(String memo) {
    this.memo = memo;
}
}
What I tried

I expect that there is a problem with the controller, but I am struggling without knowing which part to rewrite.

Supplemental information (FW/tool ​​version, etc.)

Part of the challenge of using Spring Boot, HTML, CSS.

  • Answer # 1

    @RequestMapping("/search")
        public ModelAndView search(@ModelAttribute("form") OfuroEntity entity, ModelAndView mv) {
            mv.setViewName("search");
            return mv;
    }
        @PostMapping(value="/search")
        public ModelAndView search(@RequestParam("key") String key,ModelAndView mv) {
            mv.setViewName("list");
            List<OfuroEntity>list = repository.findByNameLike("%" + key + "%");
            mv.addObject("list", list);
            return mv;
        }


    Now it works.