Spring-5 Eclipse EE [Service, Controller]

Service class 생성

service/board/BoardService.java

<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">@Service
public class BoardService {
	@Autowired
	BoardDAO dao;
	public int insert(Board dto) {
		return dao.insert(dto);
	}
	public int update(Board dto) {
		return dao.update(dto);
	}
	public int delete(Board dto) {
		return dao.delete(dto);
	}
	public Board getOne(Board dto) {
		return dao.getOne(dto);
	}
	public List<Board> getAll(){
		return dao.getAll();
	}
}</pre>

controller/board/BoardController.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import krac.jj.boardprj.dto.board.Board;
import krac.jj.boardprj.service.board.BoardService;
@Controller
public class BoardController {
	@Autowired
	BoardService service;
	@RequestMapping(value="/board",method = RequestMethod.GET)
	public String home(Model model) {
		model.addAttribute("boards", service.getAll());
		return "board_list";
	}
	@RequestMapping(value = "/board/{no}", method = RequestMethod.GET)
	public String detail(@PathVariable int no, Model model) {
		Board vo = new Board();
		vo.set_id(no);
		model.addAttribute("board", service.getOne(vo));
		return "board_detail";
	}
	@RequestMapping(value = "/board/register_form", method = RequestMethod.GET)
	public String insert_form(Model model) {
		return "board_register_form";
	}
	@RequestMapping(value = "/board/register")
	public String insert(Model model,
			@RequestParam("subject") String subject,
			@RequestParam("writer") String writer,
			@RequestParam("content") String content) {
		Board vo = new Board();
		vo.setSubject(subject);
		vo.setWriter(writer);
		vo.setContent(content);
		return "redirect:/board";
	}
}
</pre>

Controller 기본 형태

@RequestMapping(value = “/접근주소”, method = RequestMethod.GET)
public String home(Model model) {
model.addAttribute(“파라미터변수명”, 파라미터 값);
return “jsp명”;
}


view

home.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page session="false" %>
<html>
<head>
	<title>Home</title>
</head>
<body>
<h1>
	첫번째 스프링 프로그램
</h1>
<P>  The time on the server is ${serverTime}. </P>
</body>
</html>

board_list.jsp

단순화 시켜주는 태그 jstl을 사용
자세한 설명은 타 블로그 참조

https://velog.io/@ye050425/JSP-JSTL-%EC%A0%95%EB%A6%AC

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ page session="false" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>게시판 목록</title>
</head>
<body>
	<h1>게시판 목록</h1>
	<h5><a href="${contentPath}/board/register_form">글쓰기</a></h5>
	<table>
		<th>번호</th><th>제목</th><th>작성자</th><th>조회</th>
			<c:forEach items="${boards}" var="board">
				<tr>
					<td>${board._id }</td>
					<td><a href="${contentPath}/board/${board._id}" >${board.subject }</a></td>
					<td>${board.writer }</td>
					<td>${board.hit }</td>
					<td>${board.wdate }</td>
				</tr>
			</c:forEach>
	</table>
</body>
</html>

contextPath -> 루트 주소 적어주는 함수
root를 직접 정해주는방법
Servers -> Tomcat v9.0 Server at localhost – config -> server.xml
른쪽 상단 +버튼 클릭해 다 열어준다
Context -> path 속성 값을 원하는 위치로 바꾸어준다.

board_detail.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<p>번호 : ${board._id}</p>
	<p>제목 : ${board.subject}</p>
	<p>내용 : ${board.content}</p>
	<p>작성자 : ${board.writer}</p>
	<p>조회수 : ${board.hit}</p>
	<p>작성일 : ${board.wdate}</p>
</body>
</html>

board_register_form.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ page session="false" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>게시물 입력</title>
</head>
<body>
	<di class="container">
		<form action="${contextPath }/board/register" method="POST">
			<div>
				<label>제목</label>
				<input type="text" name="=subject"/>
			</div>
			<div>
				<label>작성자</label>
				<input type="text" name="writer"/>
			</div>
			<div>
				<label>내용</label>
				<textarea name="content"> </textarea>
			</div>
		</form>
	</di>
</body>
</html>

Leave a Comment