716
No
[동영상강의] 보아스 - Jsp22_mybatis ( 방명록 목록, 글쓰기,
수정/삭제를 위한 비밀번호 체크, 수정 )
edit.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="../include/header.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
function gbUpdate(){
document.form1.action="${path}/guestbook_servlet/update.do";
document.form1.submit();
}
function btnDelete(){
if(confirm("삭제 하시겠습니까?")){
document.form1.action="${path}/guestbook_servlet/delete.do";
document.form1.submit();
}
}
function btnList(){
location.href="${path}/guestbook_servlet/list.do";
}
</script>
</head>
<body>
<h2>방명록 수정 및 삭제 </h2>
<form method="post" name="form1">
<table border="1" style="width:600px; margin-bottom:20px;" >
<tr>
<td>이름</td>
<td><input type="text" name="name" value="${dto.name }"></td>
<tr>
<tr>
<td>날짜</td>
<td>${dto.post_date }</td>
</tr>
<tr>
<td>이메일</td>
<td><input type="email" name="email" id="email" value="${dto.email }"></td>
</tr>
<tr>
<td colspan="2" ><textarea cols="100" rows="20" name="content" id="content">${dto.content }</textarea></td>
</tr>
<tr>
<td colspan="4">비밀번호 <input type="password" name="passwd" value="${dto.passwd }">
</tr>
<tr>
<td colspan="2">
<input type="hidden" name="idx" value="${dto.idx }">
<input type="button" value="수정하기" onclick="gbUpdate()" >
<input type="button" value="삭제하기" onclick="btnDelete()">
<input type="button" value="목록가기" onclick="btnList()">
</td>
</tr>
</table>
</form>
</body>
</html>
class GuestBookController
if(url.indexOf("list")!=-1){
String searchKey =request.getParameter("searchKey");
String search=request.getParameter("search");
List<GuestBookDTO> items= dao.getList(searchKey, search);
request.setAttribute("list", items);
// System.out.println("list.do");
String page="/guestbook/list.jsp";
request.setAttribute("searchKey", searchKey);
request.setAttribute("search", search);
RequestDispatcher rd=request.getRequestDispatcher(page);
rd.forward(request, response);
}
else if(url.indexOf("delete.do")!=-1){
int idx=Integer.parseInt(request.getParameter("idx"));
dao.gbDelete(idx);
String page=request.getContextPath()+"/guestbook_servlet/list.do";
response.sendRedirect(page);
}
class GuestBookDAO
public List<GuestBookDTO> getList(String searchKey, String search){
List<GuestBookDTO> list=null;
SqlSession session=null;
try{
session=MybatisManager.getInstance().openSession();
Map<String,Object> map =new HashMap<>();
map.put("searchKey", searchKey);
map.put("search", search);
if(search!=null && !search.equals("")){
if(!searchKey.equals("all")){
if(searchKey.equals("name_content")){
//전체 검색
list=session.selectList("guestbook.gbListAll", map);
}else{
//개별 검색
list=session.selectList("guestbook.gbList", map);
}
}else{
//all
list=session.selectList("guestbook.gbListNull", map);
}
}else{
//null 일 경우
list=session.selectList("guestbook.gbListNull", map);
}
for(GuestBookDTO dto: list){
String content=dto.getContent();
//공백 처리
content=content.replaceAll(" ", " ");
//태그 문자 처리
content=content.replaceAll("<", "<");
content=content.replaceAll(">", ">");
//줄바꿈 처리
content=content.replaceAll("\n", "<br>");
dto.setContent(content);
}
session.commit();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(session!=null)session.close();
}catch(Exception e){
e.printStackTrace();
}
}
return list;
}
//삭제
public void gbDelete(int idx){
SqlSession session=null;
try{
session=MybatisManager.getInstance().openSession();
session.delete("guestbook.gbDelete", idx);
session.commit();
}catch(Exception e){
e.printStackTrace();
}finally{
if(session!=null)session.close();
}
}
guestbook.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- memo.xml -->
<!-- 저장 프로시저 호출 방법 { call 프로시저이름(매개변수) } -->
<mapper namespace="guestbook">
<!-- 네임스페이스 id는 중복이 될 수 없음 -->
<select id="gbList" resultType="guestbook.dto.GuestBookDTO">
select * from guestbook
where ${searchKey} like '%'||#{search}||'%'
order by idx desc
</select>
<select id="gbListAll" resultType="guestbook.dto.GuestBookDTO">
select * from guestbook
where name like '%'||#{search}|| '%' or content like '%'|| #{search}|| '%'
order by idx desc
</select>
<select id="gbListNull" resultType="guestbook.dto.GuestBookDTO">
select * from guestbook order by idx desc
</select>
<insert id="gbInsert">
insert into guestbook
(idx, name, email, passwd, content)
values
(guestbook_seq.nextval, #{name}, #{email}, #{passwd}, #{content})
</insert>
<select id="passwdCheck" resultType="int">
select count(*) from guestbook where idx=#{idx} and passwd=#{passwd}
</select>
<!-- 상세페이지 가기 -->
<select id="gbDetail" resultType="guestbook.dto.GuestBookDTO">
select * from guestbook where idx=#{idx}
</select>
<!-- 수정 -->
<update id="gbUpdate">
update guestbook set name=#{name}, email=#{email},
passwd= #{passwd}, content= #{content} where idx=#{idx}
</update>
<!-- 삭제 -->
<delete id="gbDelete">
delete from guestbook where idx=#{idx}
</delete>
<!-- 동 적SQL -->
<sql id="search">
<choose>
<when test="searchKey =='writer_memo'.toString">
select * from memo
where writer like '%'||#{search}||'%'
union
select * from memo
where memo like '%'||#{search}||'%'
</when>
<otherwise>
select * from memo
where ${searchKey} like '%'||#{search}||'%'
order by idx desc
</otherwise>
</choose>
</sql>
<!-- <select id="findActiveBlogLike"
resultType="Blog">
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
<choose>
<when test="title != null">
AND title like #{title}
</when>
<when test="author != null and author.name != null">
AND author_name like #{author.name}
</when>
<otherwise>
AND featured = 1
</otherwise>
</choose>
</select> -->
<!--
<insert id="insertMemo">
{ call memo_insert(#{writer},#{memo} ) }
</insert> -->
<!-- <select id="listMemo"
resultType="memo.dto.MemoDTO">
select * from
memo order by idx desc
</select> -->
<!-- statementType
CALLABLE(저장프로시저용),
PREPARED(PreparedStatement), STATEMENT(Statement)
mode=OUT 출력매개변수
jdbcType 오라클의 자료형
javaType 자바의 자료형
resultMap 해시맵의 자료형(리턴타입)
Map<String,Object> map=new HashMap<String,Object>();
map.put("result", new ArrayList.....);
-->
<!-- <delete id="deleteMemo">
{ call memo_del( #{idx} ) }
</delete>
-->
</mapper>
댓글 ( 4)
댓글 남기기