반응형
스프링 폼 태그로 도서 등록 페이지 만들기
앞서 배운 스프링 폼 태그를 사용해서 신규 도서를 등록할 수 있는
도서 등록 페이지를 구현해보겠습니다.
BookController.java
package com.springmvc.controller;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.MatrixVariable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.springmvc.domain.Book;
import com.springmvc.service.BookService;
@Controller
@RequestMapping("/books")
public class BookController {
@Autowired
private BookService bookService;
@RequestMapping
public String requestBookList(Model model) {
List<Book> list = bookService.getAllBookList();
model.addAttribute("bookList", list);
return "books";
}
@RequestMapping("/all")
public ModelAndView requestAllBooks() {
ModelAndView modelAndView = new ModelAndView();
List<Book> list = bookService.getAllBookList();
modelAndView.addObject("bookList", list);
modelAndView.setViewName("books");
return modelAndView;
}
@RequestMapping("/{category}")
public String requestBooksByCategory(@PathVariable("category") String bookCategory, Model model) {
List<Book> booksByCategory = bookService.getBookListByCategory(bookCategory);
model.addAttribute("bookList", booksByCategory);
return "books";
}
@RequestMapping("/filter/{bookFilter}")
public String requestBooksByFilter(
@MatrixVariable(pathVar="bookFilter") Map<String, List<String>> bookFilter, Model model) {
Set<Book> booksByFilter = bookService.getBookListByFilter(bookFilter);
model.addAttribute("bookList", booksByFilter);
return "books";
}
@RequestMapping("/book")
public String requestBookById (
@RequestParam("id") String bookId, Model model) {
Book bookById = bookService.getBookById(bookId);
model.addAttribute("book", bookById);
return "book";
}
@RequestMapping("/add")
public String requestAddBookForm(Book book) {
return "addBook";
}
}
BookController 클래스에 requestAddBookForm() 메소드를 추가합니다.
requestAddBookForm()메소드는 URL이 /add일 때 처리하는 요청 메소드로
addBook.jsp 파일을 출력합니다.
addBook.jsp
<%@ page contentType="text/html; charset=utf-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<link href="<c:url value="/resources/css/bootstrap.min.css"/>" rel="stylesheet">
<title>도서 등록</title>
</head>
<body>
<nav class="navbar navbar-expand navbar-dark bg-dark">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="./home">Home</a>
</div>
</div>
</nav>
<div class="jumbotron">
<div class="container">
<h1 class="display-3">도서 등록</h1>
</div>
</div>
<div class="container">
<form:form modelAttribute="book" class="form-horizontal">
<fieldset>
<div class="form-group row">
<label class="col-sm-2 control-label">도서ID</label>
<div class="col-sm-3">
<form:input path="bookId" class="form-control"/>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 control-label">도서명</label>
<div class="col-sm-3">
<form:input path="name" class="form-control"/>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 control-label">가격</label>
<div class="col-sm-3">
<form:input path="unitPrice" class="form-control"/>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 control-label">저자</label>
<div class="col-sm-3">
<form:input path="author" class="form-control"/>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 control-label">상세정보</label>
<div class="col-sm-5">
<form:textarea path="description" cols="50" rows="2" class="form-control"/>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 control-label">출판사</label>
<div class="col-sm-3">
<form:input path="publisher" class="form-control"/>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 control-label">분야</label>
<div class="col-sm-3">
<form:input path="category" class="form-control"/>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 control-label">재고수</label>
<div class="col-sm-3">
<form:input path="unitsInStock" class="form-control"/>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 control-label">출판일</label>
<div class="col-sm-3">
<form:input path="releaseDate" class="form-control"/>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 control-label">상태</label>
<div class="col-sm-3">
<form:radiobutton path="condition" value="New"/>New
<form:radiobutton path="condition" value="Old"/>Old
<form:radiobutton path="condition" value="E-Book"/>E-Book
</div>
</div>
<div class="form-group row">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" class="btn btn-primary" value="등록"/>
</div>
</div>
</fieldset>
</form:form>
<hr>
<footer>
<p>&Copy; BookMarket</p>
</footer>
</div>
</body>
</html>
./WEB-INF/views 폴더에 addBook.jsp 파일을 생성하고 다음과 같이 작성해줍니다.
<form:form>태그로 modelAttribute 속성에 참조할 커맨드 객체 book을 설정합니다.
modelAttribute 속성 값은 URL에 매핑되는 컨트롤러 안에 요청 처리 메소드의 매개변수로 선언한
도메인 커맨드 객체 이름 Book과 일치하여야 합니다.
modelAttribute 속성 값의 커맨드 이름 첫 글자는 소문자로 사용합니다.
실행 결과
톰캣 서버 실행 후
'http://localhost/BookMarket/books/add'를 입력합니다.
반응형
'SPRING' 카테고리의 다른 글
[SPRING]#16 도서 쇼핑몰 구현 (데이터 바인딩2) (0) | 2024.01.08 |
---|---|
[SPRING]#15 도서 쇼핑몰 구현 (데이터 바인딩1) (0) | 2024.01.08 |
[SPRING]#13 도서 쇼핑몰 구현 (스프링 폼 태그1) (0) | 2024.01.07 |
[SPRING]#12 도서 쇼핑몰 구현 (요청 파라미터2) (0) | 2024.01.07 |
[SPRING]#11 도서 쇼핑몰 구현 (요청 파라미터1) (0) | 2024.01.07 |