Spring/이론
게시판
SEOKIHOUSE
2023. 7. 20. 14:03
practice0720_SimpleBbs.zip
0.09MB
list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
tr {
text-align : center;
}
</style>
</head>
<body>
<h1>List Page</h1>
<hr>
<table width="500" cellpadding="0" cellspacing="0" border="1">
<thead>
<tr>
<th>번호</th>
<th>작성자</th>
<th>제목</th>
<th>삭제</th>
</tr>
</thead>
<tbody>
<c:forEach var="dto" items="${list }">
<tr>
<td>${dto.id }</td>
<td>${dto.writer }</td>
<td><a href="view?id=${dto.id }">${dto.title }</a></td>
<td><a href="delete?id=${dto.id }">X</a></td>
</tr>
</c:forEach>
</tbody>
</table>
<hr>
<p><a href="writeForm">글 작성</a></p>
</body>
</html>
view.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
tr {
text-align : center;
}
</style>
</head>
<body>
<h1>상세보기</h1>
<hr>
<table width="500" cellpadding="0" cellspacing="0" border="1">
<tr>
<th>작성자</th>
<th>제목</th>
<th>내용</th>
</tr>
<tr>
<td>${dto.writer }</td>
<td>${dto.title }</td>
<td>${dto.content }</td>
</tr>
</table>
<a href="list">목록가기</a>
</body>
</html>
writeForm.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>
<h1>Write Form</h1>
<hr>
<form action="write" method="get">
작성자:<input type="text" name="writer"><br>
제목:<input type="text" name="title"><br>
내용:<input type="text" name="content"><br>
<input type="submit" value="등록하기">
</form>
</body>
</html>
package com.study.springboot.dao;
import java.util.List;
import com.study.springboot.dto.SimpleBbsDto;
public interface ISimpleBbsDao {
public List<SimpleBbsDto> listDao();
public SimpleBbsDto viewDao(String id);
public int writeDao(String writer, String title, String content);
public int deleteDao(String id);
}
controller
package com.study.springboot;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.study.springboot.dao.ISimpleBbsDao;
@Controller
public class MyController {
//스프링은 ISimpleBbsDao 인터페이스를 구현한 클래스 중에서
//SimpleBbsDao 클래스의 인스턴스를 찾아서 dao 필드에 주입
@Autowired
ISimpleBbsDao dao;
@RequestMapping("/")
public String root() {
// return "SimpleBbs 만들기";
return "redirect:list";
}
@RequestMapping("/list")
public String userlistPage(Model model) {
model.addAttribute("list", dao.listDao());
return "list";
}
@RequestMapping("/view")
public String view(HttpServletRequest request, Model model) {
String sId = request.getParameter("id");
model.addAttribute("dto", dao.viewDao(sId));
return "view";
}
@RequestMapping("/writeForm")
public String writeForm() {
return "writeForm";
}
@RequestMapping("/write")
public String write(HttpServletRequest request, Model model) {
dao.writeDao(
request.getParameter("writer"),
request.getParameter("title"),
request.getParameter("content")
);
return "redirect:list";
}
@RequestMapping("/delete")
public String delete(HttpServletRequest request, Model model) {
dao.deleteDao(request.getParameter("id"));
return "redirect:list";
}
}
package com.study.springboot.dao;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.study.springboot.dto.SimpleBbsDto;
@Repository
public class SimpleBbsDao implements ISimpleBbsDao{
@Autowired
JdbcTemplate jt;
@Override
public List<SimpleBbsDto> listDao() { //목록데이터
System.out.println("listDao()...");
String query = "SELECT * FROM simple_bbs ORDER BY id DESC";
//query 1개이상 (select)
List<SimpleBbsDto> dtos = jt.query(
query, new BeanPropertyRowMapper<SimpleBbsDto>(SimpleBbsDto.class));
return dtos;
}
@Override
public SimpleBbsDto viewDao(String id) { //게시물 하나(상세보기)
System.out.println("viewDao()....");
String query = "SELECT * FROM simple_bbs WHERE id =" + id;
//1개의 데이터는 queryForObject(select)
//-> 하나니까 SimpleBbsDto타입으로
SimpleBbsDto dto = jt.queryForObject(
query, new BeanPropertyRowMapper<SimpleBbsDto>(SimpleBbsDto.class));
return dto;
}
@Override
public int writeDao(String writer, String title, String content) {
System.out.println("writeDao()....");
String query = "INSERT INTO simple_bbs(writer, title, content) ";
query += "VALUES(?,?,?)";
//변경은 update
return jt.update(query, writer, title, content);
}
@Override
public int deleteDao(String id) {
System.out.println("deleteDao()....");
String query = "DELETE FROM simple_bbs where id=?";
return jt.update(query, Integer.parseInt(id));
}
}
package com.study.springboot.dto;
import lombok.Data;
//VO 데이터 저장용
//DTO 데이터 전송
@Data
public class SimpleBbsDto {
private int id;
private String writer;
private String title;
private String content;
}