본문 바로가기
CLASS/SPRING,JSTL

#9-1 / ☔ - 통계차트 수정,삭제 페이지

by hingu 2024. 7. 24.
<!-- #8-3 jsp 예제에 해당 버튼 추가함 -->

<body>
    <input type="button" value="강우량 수정" id="modifydata">
    <input type="button" value="강우량 삭제" id="deldata">
</body>

<script>
    //강우량 수정버튼 : 현재 selected된 날짜값 backend로 던짐
    $("#modifydata").click(function(){
        var $date = $("#select_day").val();
        location.href='./rainfall_modify.do?sdate='+$date;
    })

    //강우량 삭제버튼
    $("#deldata").click(function(){
        location.href='./rainfall_delete.jsp';
    })
</script>

 

/*--------------------- 수정 ----------------------*/

 

1. mapper.xml  - list 출력시 만들어 놓은 일일데이터만 출력하는 mapper 재사용!

<select id="rain_select_ajax" resultType="rainfalldao">
    select * from rainfall where today=#{today};
</select>

 

 

2. Module - list 출력시 만들어 놓은 일일데이터만 출력하는 모듈 재사용!

public rainfall_dao ajax_today(String today) {
    rainfall_dao dao = tm.selectOne("datadb.rain_select_ajax",today);

    return dao;
}

 

3. @Controller - @GetMapping("/rainfall/rainfall_modify.do")

@GetMapping("/rainfall/rainfall_modify.do")
public String rainfall_modify(Model m, String sdate, HttpServletResponse res) throws Exception{
    res.setContentType("text/html;charset=utf-8");
    this.pw = res.getWriter();

    if(sdate == null || sdate.equals("") ) { //=만 없을때, 파라미터 다 없을 때 둘다 체크
        this.pw.write("<script>alert('올바른 접근이 아닙니다.');"
                + "history.go(-1);"
                + "</script>");
        this.pw.close();
    }else {
        rainfall_dao dao = this.rm.ajax_today(sdate);
        m.addAttribute("data",dao);
    }

    return "/rainfall/rainfall_modify";
}

 

4. rainfall_modify.jsp - write 페이지의 body 그대로 가져와서 수정 

<body>
	<!-- name-dao랑 맞춰야함  -->
	<form id="frm">
		등록 일자 : ${data.today}<br>
		서울 : <input type="text" name="area_part1" value="${data.area_part1}" maxlength="3"><br>
		경기도 : <input type="text" name="area_part2" value="${data.area_part2}" maxlength="3"><br>
		세종 : <input type="text" name="area_part3" value="${data.area_part3}" maxlength="3"><br>
		대전 : <input type="text" name="area_part4" value="${data.area_part4}" maxlength="3"><br>
		강원도 :  <input type="text" name="area_part5" value="${data.area_part5}" maxlength="3"><br><br>
		<button type="button" id="btn">강수량 수정</button>
		
		<!-- 수정시 고유값 필요 - hidden값으로 같이 던짐 -->
		<input type="hidden" name="ridx" value="${data.ridx}">
	</form>
</body>

<script>
	document.getElementById("btn").addEventListener("click",function(){
		frm.method="post";
		frm.action="./rainfall_updateok.do";
		frm.submit();
	})
</script>

ajax 필요없음

 

5. Mapper.xml

<update id="rainfall_update" parameterType="Object"> <!--parameterType="String"-->
    update rainfall set 
    area_part1=#{area_part1},
    area_part2=#{area_part2},
    area_part3=#{area_part3},
    area_part4=#{area_part4},
    area_part5=#{area_part5} where ridx=#{ridx}
</update>

 parameterType="String" 세팅시 dao의 모든 data type이 String 이어야함 

섞여있을 경우  parameterType="Object" 나 작성 x 

 

 

6. Module

public int rain_update(rainfall_dao dao) {
    int result = tm.update("datadb.rainfall_update",dao);

    return result;
}

 

5. @Controller - @PostMapping("/rainfall/rainfall_updateok.do") : update 실행하는 controller

@PostMapping("/rainfall/rainfall_updateok.do")
public String rainfall_updateok(@ModelAttribute rainfall_dao dao,HttpServletResponse res) throws Exception {
    res.setContentType("text/html;charset=utf-8");
    this.pw = res.getWriter();

    int callback = this.rm.rain_update(dao);
    System.out.println(callback);
    if(callback > 0) {
        this.pw.write("<script>"
                + "alert('정상적으로 수정 완료 되었습니다.');"
                + "location.href='./rainfall_list.do';"
                + "</script>");
    }else {
        this.pw.write("<script>"
                + "alert('데이터 오류가 발생했습니다');"
                + "location.href='./rainfall_list.do';"
                + "</script>");
    }
    this.pw.close();

    return null;
}

 

 

/*--------------------- 삭제 ----------------------*/

 

@Controller

@GetMapping("/rainfall/rainfall_delete.do")
public String rainfall_delete(String sdate,HttpServletResponse res,@ModelAttribute rainfall_dao dao) throws Exception{
    res.setContentType("text/html;charset=utf-8");
    this.pw = res.getWriter();

    try {
        rainfall_dao num = this.rm.ajax_today(sdate);
        int result = this.rm.rain_del( String.valueOf(num.getRidx()));
        System.out.println(result);
        if(result > 0) {
            this.pw.write("<script>"
                    + "alert('정상적으로 삭제 완료 되었습니다.');"
                    + "location.href='./rainfall_list.do';"
                    + "</script>");
        }else {
            this.pw.write("<script>"
                    + "alert('데이터 오류가 발생했습니다');"
                    + "location.href='./rainfall_list.do';"
                    + "</script>");
        }

    }catch(Exception e) {
        System.out.println(e);
    }finally {
        this.pw.close();
    }


    return null;
}

 

5. mapper

<delete id="rainfall_delete">
    delete from rainfall where ridx=#{del} order by ridx desc
</delete>

 

5. Module

public int rain_del(String ridx) {
    int dao = tm.delete("datadb.rainfall_delete",ridx);

    return dao;
}

 

***

//module
public rainfall_dao ajax_today(String today) {
    rainfall_dao dao = tm.selectOne("datadb.rain_select_ajax",today);

    return dao;
}

//mapper
<select id="rain_select_ajax" resultType="rainfalldao">
    select * from rainfall where today=#{today};
</select>


//요거 사용해서 ridx만 뽑아서 delete모듈로 인자값 전달