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 codesearch.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
Related articles
- java - spring boot search function sql is not reflected after submitting with characters
- about php search function
- java - about the anonymous function removelistener
- java - spring boot search function
- java - i want to display search results
- python - about function calls
- ruby on rails - [rails] i'm implementing a search function using form_with, but i'm having trouble with the search results not b
- ruby - about implementation of tag function in rails
- jquery - i get an error about $() animate is not a function
- about javascript random function
- about the sequence function of postgresql
- java - about the design pattern iterator
- ruby on rails - about multi-word search to multiple columns using ransack
- about dynamic table design policy in jsp-java servlet-db linkage
- security - about mac based on hash function
- array - about the tag search function in ransack when i press the submit button, an error occurs
- vba - about multiple condition search of the same field using access form check box
- about function pointers
- i want to move to the second page about the pagination function of laravel
- java - about project name display of eclipse
- javascript : How to refresh a page after deletion using checkboxes
- html : Help to correctly display the value from my class
- java : When the value obtained from ArrayList is displayed on the screen with jsp, unnecessary square brackets ("[", "]") are ou
- How to use th:each to pass a string to JavaScript code embedded in an html page
- java : How to set the passed object in jsp form
- java : An error occurs while initializing the Servlet class
- java : SpringMVC request encoding issues
- java : How to pass parameter from one controller method to another
- how to get the value of an element stored in shadow-root? java + Selenide
- java : Transition not firing after Spring Security authorization
Now it works.