728x90
POST 방식 후 처리
- 등록, 수정, 삭제의 최종 처리는 POST 방식을 이용
- 브라우저에는 어떤 결과를 보여 줄 것인가?
1) 별도의 결과 페이지를 만들어서 보여주는 방식(ex, 회원가입 완료 페이지)
2) 목록 페이지로 이동하는 방식(ex, 목록 페이지에서 알림 메시지를 보여주는 방식) - POST 방식 후에는 'redirect:/...'를 고려
RedirectAttribute 사용 시기
- 상황
GET 방식으로 입력페이지 /board/register
POST 방식으로 입력 처리 /board/register
처리가 끝난 후 화면 이동을 해도 브라우저의 URL은 POST 방식 처리 URL 그대로
만일 브라우저를 새로고침 한다면?
=> 도배를 방지하기 위해 - 'redirect:/...'를 이용해서 브라우저와 연결을 한 번 종료
- 브라우저는 새롭게 특정 URL을 요구
package org.zerock.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.servlet.mvc.support.RedirectAttributes;
import org.zerock.domain.BoardVO;
import org.zerock.service.BoardService;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j;
@Controller
@RequestMapping("/board/*")
@RequiredArgsConstructor
@Log4j
public class BoardController {
private final BoardService boardService;
(...생략...)
@PostMapping("/register")
public String register(BoardVO boardVO, RedirectAttributes redirectAttributes) {
log.info("BoardController register()....................");
log.info("register : " + boardVO);
redirectAttributes.addFlashAttribute("result : " + boardVO.getBno()); // addFlashAttribute 잠깐 결과를 보내는 방식
// ※ redirectAttributes.addAttribute("result : " + boardVO.getBno()) // 브라우저의 링크와 같이 연결, list?bno= 이런 형식으로 할 때 쓰임
return "redirect:/board/list";
}
}
게시물의 수정
@PostMapping("/modify")
public String modify(BoardVO boardVO, RedirectAttributes redirectAttributes) {
log.info("BoardController modify()......................");
int count = boardService.modify(boardVO);
if(count == 1) {
redirectAttributes.addFlashAttribute("result", "success");
}
return "redirect:/board/list";
}
게시물의 삭제
- BoardMapper 인터페이스에 메서드 추가 파라미터는 PK
@PostMapping("/remove")
public String remove(@RequestParam("bno") Long bno, RedirectAttributes redirectAttributes) {
log.info("BoardController remove()......................");
int count = boardService.remove(bno);
if(count == 1) {
redirectAttributes.addFlashAttribute("result", "success");
}
return "redirect:/board/list";
}
화면 처리
startbootstrap-sb-admin-2-gh-pages.zip
1.48MB
board/list에 pages 소스 붙여 넣기 후 링크 파일 넣어주기 및 수정
resources에 data, dist, js, less, pages, vendor 붙여 넣기 후 list.jsp를 아래와 파일과 같이 수정
include 적용
header.jsp, foot
header.jsp
0.02MB
footer.jsp
0.00MB
er.jsp 추가 및 수정
list.jsp 잘라낸 후
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@include file="../include/header.jsp" %>
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Tables</h1>
</div>
<!-- /.col-lg-12 -->
</div>
<!-- /.row -->
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">
DataTables Advanced Tables
</div>
<!-- /.panel-heading -->
<div class="panel-body">
<table width="100%" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>Rendering engine</th>
<th>Browser</th>
<th>Platform(s)</th>
<th>Engine version</th>
<th>CSS grade</th>
</tr>
</thead>
<tbody>
<tr class="odd gradeX">
<td>Trident</td>
<td>Internet Explorer 4.0</td>
<td>Win 95+</td>
<td class="center">4</td>
<td class="center">X</td>
</tr>
</tbody>
</table>
<!-- /.table-responsive -->
</div>
<!-- /.panel-body -->
</div>
<!-- /.panel -->
</div>
<!-- /.col-lg-12 -->
</div>
<%@include file="../include/footer.jsp" %>
목록 화면의 처리
list.jsp에서 taglib추가
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
수정
(...생략...)
<table width="100%" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>#번호</th>
<th>제목</th>
<th>작성자</th>
<th>작성일</th>
<th>수정일</th>
</tr>
</thead>
<tbody>
<c:forEach items="${list}" var="boardVO">
<tr class="odd gradeX">
<td><c:out value="${boardVO.bno}" /></td>
<td><c:out value="${boardVO.title}" /></td>
<td><c:out value="${boardVO.writer}" /></td>
<td><fmt:formatDate value="${boardVO.regdate }" pattern="yyyy-MM-dd"/> </td>
<td><fmt:formatDate value="${boardVO.updateDate }" pattern="yyyy-MM-dd" /></td>
</tr>
</c:forEach>
</tbody>
</table>
(...생략...)
jQuery를 header.jsp로 옮기기
등록 입력 페이지와 등록
- GET 방식으로 게시물 등록 화면 제공
- POST 방식으로 실제 게시물 등록처리
- 이후 목록 페이지로 이동
Controller
package org.zerock.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.zerock.domain.BoardVO;
import org.zerock.service.BoardService;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j;
@Controller
@RequestMapping("/board/*")
@RequiredArgsConstructor
@Log4j
public class BoardController {
private final BoardService boardService;
@GetMapping("/list")
public void list(Model model) { // Model 전달 당시에는 없는데 화면에 추가적인 데이터가 필요한 경우 사용
log.info("BoardController list()................");
model.addAttribute("list", boardService.getList());
}
@GetMapping("/register")
public void registerGet() {
log.info("BoardController regsterGet()............");
}
}
board/register.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@include file="../include/header.jsp" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Board Register</h1>
</div>
<!-- /.col-lg-12 -->
</div>
<!-- /.row -->
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">
DataTables Advanced Tables
</div>
<!-- /.panel-heading -->
<div class="panel-body">
<form action="/board/register" method="post">
<div class="form-group">
<label>Title</label> <input name="title" class="form-control">
</div>
<div class="form-group">
<label>Content</label> <textarea rows="5" cols="50" class="form-control" name="content"></textarea>
</div>
<div class="form-group">
<label>Writer</label> <input name="writer" class="form-control">
</div>
<button type="submit" class="btn btn-default">Submit Button</button>
<button type="reset" class="btn btn-default">Reset Button</button>
</form>
</div>
<!-- /.panel-body -->
</div>
<!-- /.panel -->
</div>
<!-- /.col-lg-12 -->
</div>
<%@include file="../include/footer.jsp" %>
input 태그와 textare 태그에 글을 작성하면 bno값이 null로 뜸. 왜 그런가를 찾아보니 Controller에서 Service를 설정해주지 않음
package org.zerock.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.zerock.domain.BoardVO;
import org.zerock.service.BoardService;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j;
@Controller
@RequestMapping("/board/*")
@RequiredArgsConstructor
@Log4j
public class BoardController {
(...생략...)
@PostMapping("/register")
public String register(BoardVO boardVO, RedirectAttributes redirectAttributes) {
log.info("BoardController register()....................");
boardService.register(boardVO);
log.info("register : " + boardVO);
redirectAttributes.addFlashAttribute("result : " + boardVO.getBno()); // addFlashAttribute 잠깐 결과를 보내는 방식
// ※ redirectAttributes.addAttribute("result : " + boardVO.getBno()) // 브라우저의 링크와 같이 연결, list?bno= 이런 형식으로 할 때 쓰임
return "redirect:/board/list";
}
(...생략...)
}
이후 한글로 글을 작성하면
한글이 깨짐
※ 테스트는 한글로 하는 것이 좋음
한글 문제와 UTF-8 필터 처리
web.xml 필터 추가
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
한글이 정상적으로 처리된 결
728x90
'organize > 스프링' 카테고리의 다른 글
스프링 웹 프로젝트 13 (0) | 2025.01.12 |
---|---|
스프링 웹 프로젝트 12 (0) | 2025.01.11 |
스프링 웹 프로젝트 10 (0) | 2025.01.08 |
스프링 웹 프로젝트 9 (0) | 2025.01.07 |
스프링 웹 프로젝트 8 (0) | 2025.01.06 |