Home>

Thank you for visiting.
The content is the same as the previous question, but there is progress and further correction is needed.
I often take this opportunity to ask questions.

Already implemented except for the search function among the simple CRUD functions.
When you enter the same name as the registered list in the search field in list.html and submit, only the ones hit in the search will be displayed.
I want to add a search function (only search by name for the time being)
Since it is simple, I would like to manage to implement the function.

If i do not enter it, you can display the list, but if you enter the characters to search for, the list will not be displayed.
(If i press the search button without inputting, SELECT processing will be entered, but if you enter characters and press the button, SELECT processing will not be entered. Is it because SQL does not pass in the first place?)
I was pointed out that the cause was that return null was performed in SearchRepositoryCustom.java, but when I tried to modify it so that return null was not entered, an error notation stating that the non-static method static could not be referenced appeared. I don't know if I should fix it.

We would appreciate your help. Thank you.

Corresponding source code

DemoController.java

package com.example.samplelist;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
import javax.annotation.PostConstruct;
@ComponentScan
@Controller
public class DemoController {
    private final UserRepository repos;
    @Autowired
    SearchService service;
    @Autowired
    public DemoController (UserRepository repos) {
        this.repos = repos;
    }
   /* Transition to list screen (initial screen) * /
    @GetMapping ("/")
    ModelAndView getAlllists (@RequestParam (required = false) String name, @RequestParam (required = false) String address,
            @RequestParam (required = false) String tel,
            @RequestParam (required = false) String zip,
            @RequestParam (required = false) String area) {
        ModelAndView mav = new ModelAndView ();
        User data = new User ();
        mav.addObject ("formModel", data);
        mav.addObject ("name", name);
        mav.addObject ("addrass", address);
        mav.addObject ("tel", tel);
        mav.addObject ("zip", zip);
        mav.addObject ("area", area);
        // COMMENT table: Get all records
       mav.addObject ("lists", repos.findAll ());
       mav.setViewName ("users/list");
        return mav;
    }
   /* Transition to new screen * /
    @GetMapping ("/ add")
    ModelAndView add () {
        ModelAndView mav = new ModelAndView ();
        User data = new User ();
        mav.addObject ("formModel", data);
        mav.setViewName ("users/new");
        return mav;
    }/ * Transition to edit screen * /
    @GetMapping ("/ edit")
    ModelAndView edit (@RequestParam int id) {
        ModelAndView mav = new ModelAndView ();
        User data = repos.findById (id);
        mav.addObject ("formModel", data);
        mav.setViewName ("users/new");
        return mav;
    }
   /* Update process * /
    @PostMapping ("") // Fix here
    public String addComment (User user, BindingResult result, Model model) {
        model.addAttribute ("views", repos.findAll ());
        if (result.hasErrors ()) {
            return "users/list";
        }
        // COMMENT table: Comment registration
        repos.save (user);
        // Redirect to the root path ("/")
        return "redirect: /";
    }
   /* Delete process * /
    @DeleteMapping ("/ delete") // Modify here as well
    public String deleteComment (@Validated @ModelAttribute User user, BindingResult result, Model model) {
        model.addAttribute ("views", repos.findAll ());
        // COMMENT table: Delete record
        repos.deleteById (user.getId ());
        // Redirect to the root path ("/")
        return "redirect:/deletes";
    }
    @RequestMapping (value = "/ search", method = RequestMethod.GET)
    public String index () {
        return "users/list";
    }
    // Search function
     @RequestMapping (value = "/ search", method = RequestMethod.POST)
        public ModelAndView search (ModelAndView mav,
                @RequestParam (required = false) String name, @RequestParam (required = false) String address,
                @RequestParam (required = false) String tel,
                @RequestParam (required = false) String zip,
                @RequestParam (required = false) String area) {
            mav.setViewName ("users/list");
            mav.addObject ("name", name);
            List<User>lists = service.search (name, address, tel, zip, area);
            mav.addObject ("lists", lists);
            return mav;
     }
    @PostConstruct
    public void init () {
/ * Initial data creation part * /
    }
}

User.java

package com.example.samplelist;
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 = "addresslist")
public class User {
    @Id
    @GeneratedValue (strategy = GenerationType.AUTO)
    @Column
    private int id;
    @Column (name = "name")
    private String name;
    @Columnprivate String address;
    @Column
    private String tel;
    @Column
    private String zip;
    @Column
    private String area;
    // getter setter part omitted
}


UserRepository.java

package com.example.samplelist;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Long>{
    public User findById (int id);
    public void deleteById (int id);
}


SearchService.java

package com.example.samplelist;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class SearchService {
    @Autowired
    UserRepository repos;
    SearchRepositoryCustom repositoryCustom;
    public List<User>search (String name, String address, String tel, String zip, String area) {
        List<User>lists;
        if ("". equals (name)) {
            lists = repos.findAll ();
        } else {
            lists = SearchRepositoryCustom.search (name, address, tel, zip, area);
        }
        return lists;
    }
}


SearchRepositoryCustom.java

package com.example.samplelist;
import java.io.Serializable;
import java.util.List;
// DAO class
public interface SearchRepositoryCustom extends Serializable {
    public static List<User>search (String name, String address, String tel, String zip, String area) {
        return null;
    }
}


SearchRepositoryCustomImp.java

package com.example.samplelist;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.springframework.beans.factory.annotation.Autowired;
@SuppressWarnings ("serial")
public class SearchRepositoryCustomImp implements SearchRepositoryCustom {
    @Autowired
    EntityManager manager;
    @SuppressWarnings ("unchecked")
    public List<User>search (String name, String address, String tel, String zip, String area) {
        StringBuilder sql = new StringBuilder ();
        sql.append ("SELECT g From User g WHERE");
        boolean andflg = false;
        boolean nameflg = false;
        boolean addressflg = false;
        boolean telflg = false;
        boolean zipflg = false;
        if (! "". Equals (name)&&name! = null) {if (andflg) sql.append ("AND");
            sql.append ("g.name LIKE%: name%");
            nameflg = true;
            andflg = true;
        }
        Query query = manager.createQuery (sql.toString ());
        if (nameflg) query.setParameter ("name", "%" + name + "%");

        return query.getResultList ();
    }
}

list.html

<! DOCTYPE html><html xmlns: th = "http://www.thymeleaf.org"><head><style>//abridgement</style></head><body>Address book<form action = "/ search" method = "post"><input type = "text" name = "name" th: value = "${name}" size = "30"
           maxlength = "15" placeholder = "search ..." /><input type = "submit" value = "search" /></form><table border = "1"><thead><tr>    <th scope = "col">name</th>    <th scope = "col">Zip code</th>    <th scope = "col">address</th>    <th scope = "col">phone number</th>    <th scope = "col">Regional</th>    <th></th>    <th></th></tr></header><tr th: each = "obj: ${lists}" th: object = "${obj}">    <td th: text = "* {name}"></td>    <td th: text = "* {zip}"></td>    <td th: text = "* {address}"></td>    <td th: text = "* {tel}"></td>    <td th: text = "* {area}"></td>    <td>        <form action = "/ edit" method = "get">            <input type = "submit" value = "edit">            <input type = "hidden" name = "id" th: value = "${obj.id}">        </form>    </td>    <td>        <form action = "/" method = "post">            <input type = "submit" value = "delete" onclick = "return confirm ('Are you sure I want to delete?')">            <input type = "hidden" name = "id" th: value = "${obj.id}">        </form>    </td></tr></table><hr><form action = "/ add"><input type = "submit" value = "add new" /></form></body></html>
What I tried

I searched the search engine with the Spring search function etc. and referred to the hit sites as soon as possible.

  • Answer # 1

    It doesn't seem to be a Spring issue.
    I think that it will be helpful if you check it with the abstract method static etc.

    SearchRepositoryCustom.java

    public interface SearchRepositoryCustom extends Serializable {
        public List<User>search (String name, String address, String tel, String zip, String area);
    }

    SearchService.java

    public class SearchService {
        @Autowired
        UserRepository repos;
        @Autowired
        SearchRepositoryCustom repositoryCustom;
        public List<User>search (String name, String address, String tel, String zip, String area) {
            List<User>lists;
            if ("". equals (name)) {
                lists = repos.findAll ();
            } else {
                lists = repositoryCustom.search (name, address, tel, zip, area);
            }
            return lists;
        }
    }