[ BoardDTO.java ]
package com.edu.board.dto;
import java.sql.Timestamp;
import org.springframework.stereotype.Component;
import lombok.Data;
//-------------------------------------------------------------------------------------
// public class BoardDTO
// @Component : Bean Configuration 파일에 Bean을 따로 등록하지 않아도 사용할 수 있다.
// Bean 등록 자체를 Bean Class 자체에다가 할 수 있다는 의미이다.
//-------------------------------------------------------------------------------------
@Component("boardDTO")
@Data
public class BoardDTO {
private int seq; // 게시글 번호
private String subject; // 제목
private String content; // 내용
private String writer; // 작성자
private Timestamp reg_date; // 작성일자
private int readCount; // 조회수
// public BoardDTO() {}
//
// public BoardDTO(int seq, String subject, String content, String writer) {
// this.seq = seq;
// this.subject = subject;
// this.content = content;
// this.writer = writer;
// }
} // End - public class BoardDTO
[ BoardController.java ]
package com.edu.board.controller;
import javax.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.edu.board.dto.BoardDTO;
import com.edu.board.service.BoardService;
//-------------------------------------------------------------------------------------
// 게시글 관리 컨트롤러
//-------------------------------------------------------------------------------------
@Controller // Bean 대상으로 인식시키기 위해서 servlet-content.xml애 등록한다.
@RequestMapping(value="/boarRegisterForm")
public class BoardController {
private static final Logger logger = LoggerFactory.getLogger(BoardController.class);
@Inject
private BoardService boardService;
//-------------------------------------------------------------------------------------
// 게시글 등록 화면
//-------------------------------------------------------------------------------------
@RequestMapping(value="/boardRegisterForm", method=RequestMethod.GET)
public String boardRegisterForm(Model model) throws Exception {
logger.info("BoardService 게시글 등록 화면 불러오기......");
return "/board/boardRegisterForm";
}
//-------------------------------------------------------------------------------------
// 게시글 등록하기
//-------------------------------------------------------------------------------------
@ResponseBody
@RequestMapping(value="boardRegister", method=RequestMethod.POST)
// request
// 메서드의 파라미터에 객체를 전달하면 자동으로 데이터가 set가 된 상태로 전달하게 된다.
public String boardRegister(BoardDTO boardDTO) throws Exception {
logger.info("BoardController 게시글 등록하기......");
logger.info("BoardDTO 값 : " + boardDTO);
return "N";
}
} // End - public class BoardController
[ board.js ]
/**
* 게시글 관련 함수
*/
//-------------------------------------------------------------------------------------
// 게시글 등록
//-------------------------------------------------------------------------------------
function fn_boardRegister() {
alert("게시글 등록 버튼을 눌렀습니다.");
// 입력한 값을 가져온다.
let subject = $("#subject").val();
let name = $("#name").val();
let content = $("content").val();
alert(subject + ":" + name + ":" + content);
// 제목 항목에 값이 없으면 입력하도록 한다.
if($("#subject").val().equals("") || $("#subject").val() =="") {
alert("제목은 필수 입력 항목입니다.");
$("#subject").focus();
return false;
}
// 글쓴이 항목에 값이 없으면 입력하도록 한다.
if($("#writer").val().equals("") || $("#writer").val() =="") {
alert("제목은 필수 입력 항목입니다.");
$("#writer").focus();
return false;
}
// 내용 항목에 값이 없으면 입력하도록 한다.
if($("#content").val().equals("") || $("#content").val() =="") {
alert("제목은 필수 입력 항목입니다.");
$("#content").focus();
return false;
}
$.ajax({
type: "POST",
url: "/board/boardRegister",
data: {subject:subject, writer:writer, content:content},
success: function(data) {
if(data =="Y") {
alert("게시글을 등록하였습니다.");
// 게시글 등록이 완료되면 게시글 목록 화면으로 이동한다.
location.href ="/board/boardList";
}
},
error: function(data) {
alert("게시글을 등록하는 데 실패하였습니다.");
}
});
} // End - function fn_boardRegister()
[ servlet-context.xml ]
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.edu.main" />
<context:component-scan base-package="com.edu.exam1" />
<context:component-scan base-package="com.edu.exam2" />
<context:component-scan base-package="com.edu.exam3" />
<context:component-scan base-package="com.edu.test" />
<context:component-scan base-package="com.edu.member" />
<context:component-scan base-package="com.edu.util" />
<context:component-scan base-package="com.edu.board" />
<context:component-scan base-package="com.edu.goods" />
<!-- 파일 업로드에 관련한 설정 -->
<!-- 스프링프레임워크에 내장된 CommonMultipartResolver를 등록한다. -->
<beans:bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 업로드할 파일의 최대용량(byte) -->
<beans:property name="maxUploadSize" value="10485740"></beans:property>
</beans:bean>
<!-- 파일 업로드를 위한 경로 설정 -->
<!-- String uploadPath = new String("c:/upload); -->
<beans:bean id="uploadPath" class="java.lang.String">
<beans:constructor-arg value="c:/data/upload"/>
</beans:bean>
</beans:beans>
[ boardRegisterForm.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="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<c:set var="contextPath" value="${pageContext.request.contextPath}"/>
<% request.setCharacterEncoding("UTF-8"); %>
<!DOCTYPE html>
<html>
<head></head>
<meta charset="UTF-8">
<title>회원 등록 화면</title>
<script src="${contextPath}/resources/js/board.js"></script>
</head>
<body>
<!-- 상단 메뉴 -->
<jsp:include page="../common/topMenu.jsp" flush="false"/>
<div class="container">
<form class="form-horizontal" name="boardRegisterForm">
<div class="form-group">
<div class="col-sm-offset-2 col-sm-5">
<h2 align="center">게시글 등록</h2>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">제 목</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="subject" name="subject" maxlength="200" placeholder="게시글 등록">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">글쓴이</label>
<div class="col-sm-3">
<input type="text" class="form-control" id="writer" name="writer" maxlength="20" placeholder="글쓴이 입력">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">내 용</label>
<div class="col-sm-8">
<textarea class="form-control" id="content" name="content" rows="10" cols="160" placeholder="내용"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset left">
<button type="reset" class="btn btn-warning">다시 입력</button>
<button type="button" class="btn btn-primary" onclick="fn_boardRegister();">게시글 등록</button>
</div>
</div>
</form>
</div>
</body>
</html>
728x90
반응형
'프로그래밍 언어 > JSP' 카테고리의 다른 글
JSP_22-11_24 (0) | 2022.11.24 |
---|---|
JSP_22-11-21_관리자 페이지/상품 등록[1] (0) | 2022.11.21 |
JSP_22-11-17 (0) | 2022.11.17 |
JSP_22-11-15 (2) | 2022.11.15 |