13주차 과제
2020. 12. 10. 01:17ㆍfront-end
1.maven 프로젝트를 생성하고 web.xml을 바꿉니다.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets
and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<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>
</web-app>
2.pom.xml에 dependency를 추가해줍니다.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycom</groupId>
<artifactId>myapp</artifactId>
<name>CRUDBoard</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>
<properties>
<java-version>1.6</java-version>
<org.springframework-version>3.1.1.RELEASE</org.springframework-version>
<org.aspectj-version>1.6.10</org.aspectj-version>
<org.slf4j-version>1.6.6</org.slf4j-version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>
<!-- @Inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<additionalProjectnatures>
<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
</additionalProjectnatures>
<additionalBuildcommands>
<buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
</additionalBuildcommands>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>org.test.int1.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
3.root-context를 수정합니다.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://db4free.net:3306/lvher1?useSSL=false" />
<property name="username" value="lvher1" />
<property name="password" value="imgmin8689" />
</bean>
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
4.BoardVO를 생성합니다.
package com.mycom.myapp.board;
import java.util.Date;
public class BoardVO {
private int seq;
private String category;
private String title;
private String writer;
private String content;
private Date regdate;
private int cnt;
public int getSeq() {
return seq;
}
public void setSeq(int seq) {
this.seq = seq;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getRegdate() {
return regdate;
}
public void setRegdate(Date regdate) {
this.regdate = regdate;
}
public int getCnt() {
return cnt;
}
public void setCnt(int cnt) {
this.cnt = cnt;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
}
5. BoardDAO 클래스파일을 생성합니다.
package com.mycom.myapp.board;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
@Repository
public class BoardDAO {
@Autowired
JdbcTemplate jdbcTemplate;
public int insertBoard(BoardVO vo) {
String sql = "insert into BOARD (title, writer, content, category) values (" + "'" + vo.getTitle() + "','"
+ vo.getWriter() + "','" + vo.getContent() + "','" + vo.getCategory() + "')";
return jdbcTemplate.update(sql);
}
// 글 삭제
public int deleteBoard(int seq) {
String sql = "delete from BOARD where seq = " + seq;
return jdbcTemplate.update(sql);
}
public int updateBoard(BoardVO vo) {
String sql = "update BOARD set title='" + vo.getTitle() + "', writer='" + vo.getWriter() + "', content='"
+ vo.getContent() + "', category='" + vo.getCategory() + "'where seq=" + vo.getSeq();
return jdbcTemplate.update(sql);
}
class BoardRowMapper implements RowMapper<BoardVO> {
@Override
public BoardVO mapRow(ResultSet rs, int rowNum) throws SQLException {
BoardVO vo = new BoardVO();
vo.setSeq(rs.getInt("seq"));
vo.setTitle(rs.getString("title"));
vo.setContent(rs.getString("content"));
vo.setWriter(rs.getString("writer"));
vo.setCategory(rs.getString("category"));
vo.setRegdate(rs.getDate("regdate"));
return vo;
}
}
public BoardVO getBoard(int seq) {
String sql = "select * from BOARD where seq=" + seq;
return jdbcTemplate.queryForObject(sql, new BoardRowMapper());
}
public List<BoardVO> getBoardList() {
String sql = "select * from BOARD order by regdate desc";
return jdbcTemplate.query(sql, new BoardRowMapper());
}
}
5.BoardService interface 파일을 생성합니다.
package com.mycom.myapp.board;
import java.util.List;
public interface BoardService {
public int insertBoard(BoardVO vo);
public int deleteBoard(int seq);
public int updateBoard(BoardVO vo);
public BoardVO getBoard(int seq);
public List<BoardVO> getBoardList();
}
6.BoardServiceImpl 클래스 파일을 생성합니다.
package com.mycom.myapp.board;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class BoardServiceImpl implements BoardService {
@Autowired
BoardDAO boardDAO;
@Override
public int insertBoard(BoardVO vo) {
return boardDAO.insertBoard(vo);
}
@Override
public int deleteBoard(int seq) {
return boardDAO.deleteBoard(seq);
}
@Override
public int updateBoard(BoardVO vo) {
return boardDAO.updateBoard(vo);
}
@Override
public BoardVO getBoard(int seq) {
return boardDAO.getBoard(seq);
}
@Override
public List<BoardVO> getBoardList() {
return boardDAO.getBoardList();
}
}
7.BoardController를 생성합니다.
package com.mycom.myapp.board;
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;
@Controller
@RequestMapping(value = "/board")
public class BoardController {
@Autowired
BoardServiceImpl boardService;
@RequestMapping(value = "/list", method = RequestMethod.GET)
public String boardlist(Model model) {
model.addAttribute("list", boardService.getBoardList());
return "list";
}
@RequestMapping(value = "/add", method = RequestMethod.GET)
public String addPost() {
return "addpostform";
}
@RequestMapping(value = "/addok", method = RequestMethod.POST)
public String addPostOK(BoardVO vo) {
if (boardService.insertBoard(vo) == 0) {
System.out.println("데이터 추가 실패");
} else {
System.out.println("데이터 추가 성공!!");
}
return "redirect:list";
}
@RequestMapping(value = "/editform/{id}", method = RequestMethod.GET)
public String editPost(@PathVariable("id") int id, Model model) {
BoardVO boardVO = boardService.getBoard(id);
model.addAttribute("u", boardVO);
return "editform";
}
@RequestMapping(value = "/editok", method = RequestMethod.POST)
public String editPostOk(BoardVO vo) {
if (boardService.updateBoard(vo) == 0)
System.out.println("데이터 수정 실패");
else
System.out.println("데이터 수정 성공!!!");
return "redirect:list";
}
@RequestMapping(value = "/deleteok/{id}", method = RequestMethod.GET)
public String deletePostOk(@PathVariable("id") int id) {
if (boardService.deleteBoard(id) == 0)
System.out.println("데이터 삭제 실패");
else
System.out.println("데이터 삭제 성공!!");
return "redirect:../list";
}
}
8.jsp 페이지들을 생성합니다.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>free board</title>
<style>
#list {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%;
}
#list td, #list th {
border: 1px solid #ddd;
padding: 8px;
text-align: center;
}
#list tr:nth-child(even) {
background-color: #f2f2f2;
}
#list tr:hover {
background-color: #ddd;
}
#list th {
padding-top: 12px;
padding-bottom: 12px;
text-align: center;
background-color: #006bb3;
color: white;
}
</style>
<script>
function delete_ok(id) {
var a = confirm("정말로 삭제하겠습니까?");
if (a)
location.href = "deleteok/${u.seq()}" + id;
}
</script>
</head>
<body>
<h1>자유게시판</h1>
<table id="list" width="90%">
<tr>
<th>Id</th>
<th>Category</th>
<th>Title</th>
<th>Writer</th>
<th>Content</th>
<th>Regdate</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<c:forEach items="${list}" var="u">
<tr>
<td>${u.seq}</td>
<td>${u.category}</td>
<td>${u.title}</td>
<td>${u.writer}</td>
<td>${u.content}</td>
<td>${u.regdate}</td>
<td><a href="editform/${u.seq}">Edit</a></td>
<td><a href="deleteok/${u.seq}">Delete</a></td>
</tr>
</c:forEach>
</table>
<br />
<a href="add">Add New Post</a>
</body>
</html>
<list.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>Add New Post</h1>
<form action="addok" method="post">
<table>
<tr>
<td>Category:</td>
<td><input type="text" name="category" /></td>
</tr>
<tr>
<td>Title:</td>
<td><input type="text" name="title" /></td>
</tr>
<tr>
<td>Writer:</td>
<td><input type="text" name="writer" /></td>
</tr>
<tr>
<td>Content:</td>
<td><textarea cols="50" rows="5" name="content"></textarea></td>
</tr>
<tr>
<td><a href="list">View All Records</a></td>
<td align="right"><input type="submit" value="addpost" /></td>
</tr>
</table>
</form>
</body>
</html>
<addpostform.jsp>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page
import="com.mycom.myapp.board.BoardDAO, com.mycom.myapp.board.BoardVO"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Edit Form</title>
</head>
<body>
<h1>Edit Form</h1>
<form:form action="../editok" method="POST">
<input type="hidden" name="seq" value="${u.getSeq()}" />
<table>
<tr>
<td>Category:</td>
<td><input type="text" name="category"
value="${u.getCategory()}" /></td>
</tr>
<tr>
<td>Title:</td>
<td><input type="text" name="title" value="${u.getTitle()} " /></td>
</tr>
<tr>
<td>Writer:</td>
<td><input type="text" name="writer" value="${u.getWriter()} " /></td>
</tr>
<tr>
<td>Content:</td>
<td><textarea cols="50" rows="5" name="content">${u.getContent()}</textarea></td>
</tr>
<tr>
<td><input type="submit" value="Edit Post" /></td>
<td><input type="button" value="Cancel"
onclick="history.back()" /></td>
</tr>
</table>
</form:form>
</body>
</html>
'front-end' 카테고리의 다른 글
14주차 개념정리 (0) | 2020.12.13 |
---|---|
13주차 내용 정리 (1) | 2020.12.11 |
13주차 개념 정리 (0) | 2020.11.22 |
13주차 실습 (0) | 2020.11.22 |
11주차 실습 (0) | 2020.11.15 |