반응형
공지사항 등록을 위한 Controller / View 준비하기.
공지사항 글쓰기 버튼을 누르면
글을 쓸 수 있는 페이지를 전달하며
두 번째는 글을 작성한 뒤 등록을 했을 때 post를 처리하는 로직과
pub칼럼이 추가 됐을 때 처리하는 방식
등록을 위한 RegController
package com.newlec.web.controller.admin.notice;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.newlec.web.entity.Notice;
import com.newlec.web.service.NoticeService;
@WebServlet("/admin/board/notice/reg")
public class RegController extends HttpServlet{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/WEB-INF/VIEW/admin/board/notice/reg.jsp")
.forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String title = request.getParameter("title");
String content= request.getParameter("content");
String isOpen= request.getParameter("open");
boolean pub =false;
if(isOpen!=null)
pub = true;
Notice notice= new Notice();
notice.setTitle(title);
notice.setContent(content);
notice.setPub(pub);
notice.setWriterId("newlec");
NoticeService service = new NoticeService();
service.insertNotice(notice);
response.sendRedirect("list");
}
}
reg.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>코딩 전문가를 만들기 위한 온라인 강의 시스템</title>
<meta charset="UTF-8">
<title>공지사항목록</title>
<link href="/css/customer/layout.css" type="text/css" rel="stylesheet" />
<style>
#visual .content-container {
height: inherit;
display: flex;
align-items: center;
background: url("/images/mypage/visual.png") no-repeat center;
}
</style>
</head>
<body>
<!-- header 부분 -->
<header id="header">
<div class="content-container">
<!-- ---------------------------<header>--------------------------------------- -->
<h1 id="logo">
<a href="/index.html">
<img src="/images/logo.png" alt="뉴렉처 온라인" />
</a>
</h1>
<section>
<h1 class="hidden">헤더</h1>
<nav id="main-menu">
<h1>메인메뉴</h1>
<ul>
<li><a href="/guide">학습가이드</a></li>
<li><a href="/course">강좌선택</a></li>
<li><a href="/answeris/index">AnswerIs</a></li>
</ul>
</nav>
<div class="sub-menu">
<section id="search-form">
<h1>강좌검색 폼</h1>
<form action="/course">
<fieldset>
<legend>과정검색필드</legend>
<label>과정검색</label>
<input type="text" name="q" value="" />
<input type="submit" value="검색" />
</fieldset>
</form>
</section>
<nav id="acount-menu">
<h1 class="hidden">회원메뉴</h1>
<ul>
<li><a href="/index.html">HOME</a></li>
<li>
<form action="/logout" method="post">
<input type="hidden" name="" value="" />
<input type="submit" value="로그아웃"
style="border:none;background: none;vertical-align: middle;font-size: 10px;color:#979797;font-weight: bold;" />
</form>
</li>
<li><a href="/member/agree">회원가입</a></li>
</ul>
</nav>
<nav id="member-menu" class="linear-layout">
<h1 class="hidden">고객메뉴</h1>
<ul class="linear-layout">
<li><a href="/member/home"><img src="/images/txt-mypage.png" alt="마이페이지" /></a></li>
<li><a href="/notice/list.html"><img src="/images/txt-customer.png" alt="고객센터" /></a></li>
</ul>
</nav>
</div>
</section>
</div>
</header>
<!-- --------------------------- <visual> --------------------------------------- -->
<!-- visual 부분 -->
<div id="visual">
<div class="content-container"></div>
</div>
<!-- --------------------------- <body> --------------------------------------- -->
<div id="body">
<div class="content-container clearfix">
<!-- --------------------------- aside --------------------------------------- -->
<!-- aside 부분 -->
<aside class="aside">
<h1>ADMIN PAGE</h1>
<nav class="menu text-menu first margin-top">
<h1>마이페이지</h1>
<ul>
<li><a href="/admin/index.html">관리자홈</a></li>
<li><a href="/teacher/index.html">선생님페이지</a></li>
<li><a href="/student/index.html">수강생페이지</a></li>
</ul>
</nav>
<nav class="menu text-menu">
<h1>알림관리</h1>
<ul>
<li><a href="/admin/board/notice/list.html">공지사항</a></li>
</ul>
</nav>
</aside>
<!-- --------------------------- main --------------------------------------- -->
<main>
<h2 class="main title">공지사항 등록</h2>
<div class="breadcrumb">
<h3 class="hidden">breadlet</h3>
<ul>
<li>home</li>
<li>게시글 관리</li>
<li>공지사항</li>
</ul>
</div>
<form method="post" action="reg";>
<div class="margin-top first">
<h3 class="hidden">공지사항 입력</h3>
<table class="table">
<tbody>
<tr>
<th>제목</th>
<td class="text-align-left text-indent text-strong text-orange" colspan="3">
<input type="text" name="title" />
</td>
</tr>
<tr>
<th>첨부파일</th>
<td colspan="3" class="text-align-left text-indent"><input type="file"
name="file" /> </td>
</tr>
<tr class="content">
<td colspan="4"><textarea class="content" name="content"></textarea></td>
</tr>
<tr>
<td colspan="4" class="text-align-right"><input class="vertical-align" type="checkbox" id="open" name="open" value="true"><label for="open" class="margin-left">바로공개</label> </td>
</tr>
</tbody>
</table>
</div>
<div class="margin-top text-align-center">
<input class="btn-text btn-default" type="submit" value="등록" />
<a class="btn-text btn-cancel" href="list.html">취소</a>
</div>
</form>
</main>
</div>
</div>
<!-- ------------------- <footer> --------------------------------------- -->
<footer id="footer">
<div class="content-container">
<h2 id="footer-logo"><img src="/images/logo-footer.png" alt="회사정보"></h2>
<div id="company-info">
<dl>
<dt>주소:</dt>
<dd>서울특별시 </dd>
<dt>관리자메일:</dt>
<dd>admin@newlecture.com</dd>
</dl>
<dl>
<dt>사업자 등록번호:</dt>
<dd>111-11-11111</dd>
<dt>통신 판매업:</dt>
<dd>신고제 1111 호</dd>
</dl>
<dl>
<dt>상호:</dt>
<dd>뉴렉처</dd>
<dt>대표:</dt>
<dd>홍길동</dd>
<dt>전화번호:</dt>
<dd>111-1111-1111</dd>
</dl>
<div id="copyright" class="margin-top">Copyright ⓒ newlecture.com 2012-2014 All Right Reserved.
Contact admin@newlecture.com for more information</div>
</div>
</div>
</footer>
</body>
</html>
pub칼럼을 추가함으로써 수정된 NoticeService함수
package com.newlec.web.service;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.newlec.web.entity.Notice;
import com.newlec.web.entity.NoticeView;
public class NoticeService {
public int removeNoticeAll(int[] ids){ //몇개가 삭제됬는지
return 0;
}
public int pubNoticeAll(int[] ids){ //몇개가 공개됬는지
return 0;
}
public int insertNotice(Notice notice){ //추가됬으면 1 아니면 0을 반환
int result =0;
String sql = "INSERT INTO NOTICE(TITIE, CONTENT, WRITER_ID, PUB) VALUES(?,?,?,?)";
String url = "jdbc:oracle:thin:@192.168.0.79:1521/xepdb1";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(url, "lec", "111");
PreparedStatement st =con.prepareStatement(sql);
result = st.executeUpdate();
st.setString(1, notice.getTitle());
st.setString(2,notice.getContent());
st.setString(3, notice.getWriterId());
st.setBoolean(4, notice.getPub());
st.close();
con.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
public int deleteNotice(int id){ //삭제됬으면 1 아니면 0
return 0;
}
public int updateNotice(Notice notice){
return 0;
}
public List<Notice> getNoticeNewestList(){
return null;
}
public List<NoticeView> getNoticeList() {
return getNoticeList("title", "", 1);
}
public List<NoticeView> getNoticeList(int page) {
return getNoticeList("title", "", page);
}
public List<NoticeView> getNoticeList(String field, String query, int page) {
List<NoticeView> list = new ArrayList<NoticeView>();
String sql = "SELECT * FROM(" +
" SELCT ROWNUM NUM, N.* "
+ " FROM (SELECT * FROM NOTICE_VIEW WHERE "+field+" LIKE ? ORDER BY REGDATE DESC) N" + ")" +
"WHERE NUM BETWEEN ? AND ? ";
String url = "jdbc:oracle:thin:@192.168.0.79:1521/xepdb1";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(url, "lec", "111");
PreparedStatement st = con.prepareStatement(sql);
st.setString(1,"%"+query +"%"); //String 은 '' 싱글퀘테이션으로 감싸서 들어가진다.
st.setInt(2,1+(page-1)*10 );
st.setInt(3,page*10 );
ResultSet rs = st.executeQuery();
while (rs.next()) {
int id = rs.getInt("ID");
String title = rs.getString("TITLE");
String writerId = rs.getString("WRITER_ID");
//String content = rs.getString("CONTENT");
Date regdate = rs.getDate("REGDATE");
int hit = rs.getInt("HIT");
String files = rs.getString("FILES");
int cmtCount = rs.getInt("CMT_COUNT");
boolean pub = rs.getBoolean("PUB");
NoticeView notice = new NoticeView(
id,
title,
writerId,
//content,
regdate,
hit,
files,
cmtCount,
pub
);
list.add(notice);
}
rs.close();
st.close();
con.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public int getNoticeCount() {
return getNoticeCount("title", "");
}
public int getNoticeCount(String field, String query) {
int count = 0;
String sql = "SELECT COUNT(ID) COUNT FROM(" +
" SELCT ROWNUM NUM, N.* " +
" FROM (SELECT * FROM NOTICE WHERE "+field+" LIKE ? ORDER BY REGDATE DESC) N" +
")";
String url = "jdbc:oracle:thin:@192.168.0.79:1521/xepdb1";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(url, "lec", "111");
PreparedStatement st = con.prepareStatement(sql);
st.setString(1,"%"+query +"%"); //String 은 '' 싱글퀘테이션으로 감싸서 들어가진다.
ResultSet rs = st.executeQuery();
if(rs.next())
count = rs.getInt("count");
rs.close();
st.close();
con.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return count;
}
public Notice getNotice(int id) {
Notice notice = null;
String sql = "SELECT * FROM NOTICE WHERE ID=?";
String url = "jdbc:oracle:thin:@192.168.0.79:1521/xepdb1";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(url, "lec", "111");
PreparedStatement st = con.prepareStatement(sql);
st.setInt(1,id); //String 은 '' 싱글퀘테이션으로 감싸서 들어가진다.
ResultSet rs = st.executeQuery();
if (rs.next()) {
int nid = rs.getInt("ID");
String title = rs.getString("TITLE");
String writerId = rs.getString("WRITER_ID");
String content = rs.getString("CONTENT");
Date regdate = rs.getDate("REGDATE");
int hit = rs.getInt("HIT");
String files = rs.getString("FILES");
boolean pub = rs.getBoolean("PUB");
notice = new Notice(nid, title, writerId, content, regdate, hit, files,pub);
}
rs.close();
st.close();
con.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return notice;
}
public Notice getNextNotice(int id) {
Notice notice = null;
String sql = "SELECT * FROM NOTICE "
+ " WHERE ID = (SELECT ID FROM NOTICE WHERE REGDATE > (SELECT REGDATE FROM NOTICE WHERE ID=?)"
+ "AND ROWNUM = 1 " + ")";
String url = "jdbc:oracle:thin:@192.168.0.79:1521/xepdb1";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(url, "lec", "111");
PreparedStatement st = con.prepareStatement(sql);
st.setInt(1,id); //String 은 '' 싱글퀘테이션으로 감싸서 들어가진다.
ResultSet rs = st.executeQuery();
if (rs.next()) {
int nid = rs.getInt("ID");
String title = rs.getString("TITLE");
String writerId = rs.getString("WRITER_ID");
String content = rs.getString("CONTENT");
Date regdate = rs.getDate("REGDATE");
int hit = rs.getInt("HIT");
String files = rs.getString("FILES");
boolean pub = rs.getBoolean("PUB");
notice = new Notice(nid, title, writerId, content, regdate, hit, files,pub);
}
rs.close();
st.close();
con.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return notice;
}
public Notice getPrevNotice(int id) {
Notice notice = null;
String sql = "SELECT ID FROM (SELECT * FROM NOTICE ORDER BY REGDATE DESC)"
+ " WHERE REGDATE < (SELECT REGDATE FROM NOTICE WHERE ID=?)" + "AND ROWNUM =1";
String url = "jdbc:oracle:thin:@192.168.0.79:1521/xepdb1";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(url, "lec", "111");
PreparedStatement st = con.prepareStatement(sql);
st.setInt(1,id); //String 은 '' 싱글퀘테이션으로 감싸서 들어가진다.
ResultSet rs = st.executeQuery();
if (rs.next()) {
int nid = rs.getInt("ID");
String title = rs.getString("TITLE");
String writerId = rs.getString("WRITER_ID");
String content = rs.getString("CONTENT");
Date regdate = rs.getDate("REGDATE");
int hit = rs.getInt("HIT");
String files = rs.getString("FILES");
boolean pub = rs.getBoolean("PUB");
notice = new Notice(nid, title, writerId, content, regdate, hit, files,pub);
}
rs.close();
st.close();
con.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return notice;
}
public int deleteNoticeAll(int[] ids) {
int result =0;
String params = "";
for(int i =0 ; i <ids.length; i++) {
params += ids[i];
if(i < ids.length-1)
params +=",";
}
String sql = "DELETE NOTICE WHERE ID IN("+params+")";
String url = "jdbc:oracle:thin:@192.168.0.79:1521/xepdb1";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(url, "lec", "111");
Statement st =con.createStatement();
result = st.executeUpdate(sql);
st.close();
con.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
}
반응형
'웹 프로그래밍 기초 > 자바기반의 웹&앱 응용SW 개발자' 카테고리의 다른 글
자바기반의 웹&앱 응용 SW개발자 양성과정 49일차 -78 (0) | 2020.05.05 |
---|---|
자바기반의 웹&앱 응용 SW개발자 양성과정 49일차 -77 (0) | 2020.05.04 |
자바기반의 웹&앱 응용 SW개발자 양성과정 49일차 -75 (0) | 2020.05.04 |
자바기반의 웹&앱 응용 SW개발자 양성과정 48일차 -74 (0) | 2020.05.03 |
자바기반의 웹&앱 응용 SW개발자 양성과정 48일차 -73 (0) | 2020.05.03 |
댓글