YJ의 새벽
TodoList 연습 5 , 서버재실행시 체크박스 체크 유지 본문
-- 서버 재실행하면 , 체크박스가 모두 풀린다 . 수정해보자 .
--- window.onload 에 체크박스 N / Y 값 확인하는 ajax 추가.
$.ajax({
url : "checkcheck",
data : { "spanInput1" : span.innerText },
type : "POST",
success: function(result){
if ( result == 1 ){
checkBox.checked=true;
span.style.textDecoration="line-through";
span.style.color="gray";
count--;
updateCount();
}
},
error : function(result){
console.log(result);
}
});
--- 체크가 된 상태라면 ' Y ' , return 1 을 해준다 .
-- " /checkcheck " 서블릿 작성
@WebServlet("/checkcheck")
public class CheckcheckServlet extends HttpServlet{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
TodoService service = new TodoService();
String spanInput = req.getParameter("spanInput1");
int result = service.checkcheck(spanInput);
System.out.println(result);
resp.getWriter().print(result);
}catch(Exception e) {
}
}
}
--- 체크박스 체크하는 Service class
/** 체크박스 체크 확인
* @param spanInput
* @return
* @throws Exception
*/
public int checkcheck(String spanInput) throws Exception {
Connection conn = getConnection();
int result = dao.checkcheck(conn,spanInput);
close(conn);
return result;
}
}
--- 체크박스 체크하는 DAO class
/** 체크박스 체크
* @param conn
* @param spanInput
* @return
* @throws Exception
*/
public int checkcheck(Connection conn, String spanInput) throws Exception {
int result = 0;
try {
String sql = prop.getProperty("checkcheck");
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, spanInput);
result = pstmt.executeUpdate();
}finally {
close(pstmt);
}
return result;
}
}
--- 체크박스 체크하는 SQL 문
<entry key="checkcheck">
SELECT * FROM TODOLIST
WHERE TESTADDTODO = ?
AND TESTCHECK = 'Y'
</entry>
--- DB CHECK 값이 Y 라서 return = 1 을 반환 .
-- 새로고침해도 체크상태로 만듬.
'WebFront_ > TodoList' 카테고리의 다른 글
TodoList 연습 4 , 체크박스 체크시 체크된것만 삭제. (0) | 2023.04.14 |
---|---|
TodoList 연습 3 , 서버실행하면 DB저장된 TodoList 바로출력 (0) | 2023.04.13 |
TodoList 연습 2 , DB 연동 ( html,css,js, ajax ( Servlet ) ) (0) | 2023.04.12 |
TodoList 연습 1( html,css,js ) (0) | 2023.04.11 |
Comments