본문 바로가기
스프링

스프링 USER권한 페이지

by 근즈리얼 2021. 10. 8.
728x90

1. UtilController 수정

2. find.html 

 

1. UtilController 

 

UtilController 코드

package com.asdanything.ask.controller;


import com.asdanything.ask.Entity.Member;
import com.asdanything.ask.Entity.Register;
import com.asdanything.ask.dto.RegisterDto;
import com.asdanything.ask.repository.MemberRepository;
import com.asdanything.ask.repository.RegisterRepository;
import com.asdanything.ask.service.RegisterService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.Valid;
import java.util.List;

@Controller
@RequiredArgsConstructor
public class UtilController {

    private final RegisterService registerService;

    private final MemberRepository memberRepository;
    private final RegisterRepository registerRepository;

    @GetMapping("/util/find")
    public String findForm(Model model){
        List<Register> findAll = registerRepository.findAll();
        model.addAttribute("findRegister",findAll);
        String search="";
        model.addAttribute("search", search);
        return "util/find";
    }

    @PostMapping("/util/find")
    public String find(@RequestParam String search, Model model){
        System.out.println(search);
        List<Register> findSearch = registerRepository.findByAbilityContaining(search);
        model.addAttribute("findSearch", findSearch);
        return "util/search";
    }

	~
    ~
    ~
}

- util/find post매핑의 메소드를 추가했습니다.

- @RequestParam을 통해서 검색된 단어를 가져오고 그 단어를 이용해서 registerRepository에서 데이터를 찾아옵니다.

 

2. find.html

 

find.html 코드

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
</head>
<body>
<h2>찾아보기</h2>
<form action="/util/find" method="post">
    <div>
        <label th:for="search">검색</label>
        <input th:type="text" name="search" placeholder="이름을 입력하세요">
    </div>
    <button type="submit">제출하기</button>
</form>

<table border="1">

    <thead>
        <tr>
            <td>순번</td>
            <td>능력</td>
            <td>주소</td>
            <td>핸드폰</td>
            <td>이름</td>
        </tr>
    </thead>

    <tbody>
        <tr th:each="register, status:${findRegister}">
            <td th:text="${status.index}"></td>
            <td th:text="${register.ability}"></td>
            <td th:text="${register.address}"></td>
            <td th:text="${register.phoneNum}"></td>
            <td th:text="${register.member.getName()}"></td>
        </tr>
    </tbody>
</table>


</body>
</html>

 

3. 결과물 및 다음 기능

 

USER 계정의 찾아보기 페이지

 

원하는 단어를 입력하고 얻은 화면 ex) 세탁기

 

이제까지 로그인 회원가입, 등록, 검색을 완성했습니다.

앞으로 기능에 대한 계획은

회원정보 수정, 등록 수정, 댓글 기능을 계획하고 있습니다.

728x90

댓글