Spring

Spring 일지 #6 (20210914) 화면 구현(insert, select)

uni5948 2021. 9. 14. 17:35

6. 화면 구현

-select, insert 생성

 *controller 생성(BoardController.java)

package com.example.controller;

 

import com.example.entity.Board;

import com.example.repository.BoardRepository;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.Sort;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.ModelAttribute;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

 

@Controller

@RequestMapping(value = "/board")

public class BoardController {

 

    // 127.0.0.1:8080/ROOT/board/select

    @RequestMapping(value = "/select"method = RequestMethod.GET)

    public String select() {

        return "board_select";

    }

 

    // 127.0.0.1:8080/ROOT/board/insert

    @RequestMapping(value = "/insert"method = RequestMethod.GET)

    public String insertGet() {

        return "board_insert";

    }

}

 

 -등록하기

 *templates 폴더에 board_insert.jsp 생성

 **id, date는 자동으로 등록 되므로 넣지 않는다. hit는 board.java 에서 1로 등록해서 등록하지 않는다.

<!DOCTYPE html>

<html lang="ko" xmlns:th="http://www.thymeleaf.org">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>게시판 글쓰기</title>

</head>

<body>

    

    <form th:action="@{/board/insert}" method="post">

        제목 <input type="text" name="title" /><br />

        내용 <input type="text" name="content" /><br />

        작성자 <input type="text" name="writer" /><br />

        <input type="submit" value="글쓰기" />

    </form>

</body>

</html>

 

 *페이지 확인(http://127.0.0.1:8080/ROOT/board/insert)

insert 페이지 확인

 *오라클에 정보 등록

 **BoardController.java에서 insertPost 추가

 **저장 후 글쓰기

@RequestMapping(value = "/insert"method = RequestMethod.POST)

    public String insertPost(@ModelAttribute Board board) {

        System.out.println(board.toString());

        // 127.0.0.1:8080/ROOT/board/ (insert <= select)

        return "redirect:select";

    }

 

 *출력값 확인

글쓰기
출력값 확인

 -오라클에 등록하기

 *저장소 변수 등록(BoardController.java)

 ... ...

// 저장소 객체 생성

    @Autowired

    private BoardRepository bRepository;

... ...

@RequestMapping(value = "/insert"method = RequestMethod.POST)

    public String insertPost(@ModelAttribute Board board) {

        System.out.println(board.toString());

        bRepository.save(board); <-- 추가

        // 127.0.0.1:8080/ROOT/board/ (insert <= select)

        return "redirect:select"; // 완료 후 select 화면으로 이동

    }

 

 *저장 후 다시 글쓰기 완료 후 오라클 확인

글쓰기
오라클 등록 확인

-테이블 형식으로 출력

 *select 수정(BoardController.java)

... ...

   public String select(Model model) {

        // 정렬 X

        List<Boardlist = bRepository.findAll();

        // 글번호 최신순으로

        List<Boardlist1 = bRepository.findAll(Sort.by(Sort.Direction.DESC"no"));

 

        model.addAttribute("list"list);

        model.addAttribute("list1"list1);

 

        return "board_select";

    }

... ...

 

*화면 출력(board_select.jsp)

***templates 폴더에 board_select.jsp 생성

<!DOCTYPE html>

<html lang="ko" xmlns:th="http://www.thymeleaf.org">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

</head>

<body>

    정렬 x 테이블

    <table border="1">

        <!--<td th:text="${idx}"></td> 로 idx 구조 확인가능-->

        <tr th:each="brd, idx : ${list}">

            <td th:text="${idx.count}"></td>

            <td th:text="${brd.no}"></td>

            <td th:text="${brd.title}"></td>

            <td th:text="${brd.content}"></td>

            <td th:text="${brd.writer}"></td>

            <td th:text="${brd.hit}"></td>

            <td th:text="${brd.regdate}"></td>

        </tr>

    </table>

    <hr />

    최신순 테이블

    <table border="1">

        <!--<td th:text="${idx}"></td> 로 idx 구조 확인가능-->

        <tr th:each="brd, idx : ${list1}">

            <td th:text="${idx.count}"></td>

            <td th:text="${brd.no}"></td>

            <td th:text="${brd.title}"></td>

            <td th:text="${brd.content}"></td>

            <td th:text="${brd.writer}"></td>

            <td th:text="${brd.hit}"></td>

            <td th:text="${brd.regdate}"></td>

        </tr>

    </table>

    

</body>

</html>

 

 *화면 확인(http://127.0.0.1:8080/ROOT/board/select)

 **insertPost에서 return "redirect:select"로 입력해서 글쓰기 완료하면 자동으로 select 페이지로 이동한다.

select 화면 확인