스프링

 

 

1. qnaboard 테이블에 비밀번호 상태 컬럼 추가


비밀글일 경우 on 비밀글이 아닐경우 null  또는 공백 처리

ALTER TABLE qnaboard ADD password varchar(2) NULL;


 

 

2. Qnaboard  클래스 객체에 비밀번호 필드 추가

import java.util.Date;

import lombok.Data;
import lombok.ToString;

@Data
@ToString
public class Qnaboard {
	private Long board_id;
	
	private Long board_re_ref;
	private Long board_re_lev;
	private Long board_re_seq;

	private String subject;
	private String content;
	private Date regdate;
	private String status;
	private String member_id;
	private String password;
}

 

 

 

 

3.qnaReRegist.jsp 에 서 비밀번호 체크박스 추가

 

					<div class="form-group">
						<label>비밀글:</label> <input  autocomplete="off" type="checkbox" id="password" name="password"
						style="position: relative;top: 3px;margin-left: 5px;"
						  />
					</div>

 

 

 

 

 

 

4.   BoardMapper.xml 에 insertQnaBoard  와 updateQnaBoard 수정

password  추가 등록 및 수정 처리

    <!-- qna 작성 -->
    <insert id="insertQnaBoard" parameterType="com.sam.coffeeshop.domain.Qnaboard">         
        <selectKey resultType="Long" keyProperty="board_re_ref" order="BEFORE">
            select ifnull(max(board_re_ref), board_id) + 1 from qnaboard
        </selectKey>
        
        insert into qnaboard (board_re_ref, board_re_lev, board_re_seq, member_id, subject, content, password)
        values(#{board_re_ref}, 0, 0, #{member_id}, #{subject}, #{content}, #{password})
    </insert>


    <!-- qna 수정 -->
    <update id="updateQnaBoard">
    	update qnaboard set subject=#{subject}, content=#{content} , password=#{password}  where board_id=#{board_id}
    </update>
    

 

 

 

 

 

 

5.  qnaBoard.jsp 에서 다음과 같이 변경

1)

						<td>
						<c:if test="${q.board_re_lev > 0}">
	                        <c:forEach begin="1" end="${q.board_re_lev}">
	                            &nbsp;&emsp;
	                        </c:forEach>
                    	</c:if>
                    	                    	
                    	
                    	<c:if test="${not empty q.password}">
                    		<i class="fa fa-lock"></i>&nbsp;                   	
                    	</c:if>                    	
                    	
                    	
                    	
                    	<!--1. 익명유저 : 로그인 안한 유저는 다음을 실행한다. -->	     
	      				<sec:authorize access="isAnonymous()"> 					
	      					<c:choose>
	      						<c:when test="${not empty q.password}">
	      							<!--1-1 비밀글일경우 다음을 실행 - 비밀번호 컬럼이 비어 있지 않을 경우 즉, on 이 들어 있을 경우  -->
	      							<a href="#" onclick="alert('작성자와 관리자만 접근 가능합니다.')">${q.subject}</a>
	      						</c:when>
	      						<c:otherwise>
	      						   <!--1-1 비밀글이 아닐 경우 다음을 실행    -->
	      							 <a href="board/qna/qnaDetail?board_id=${q.board_id}&pageNum=${page.currentPage}&field=${field}&word=${word}"
							 			>${q.subject}</a>
	      						</c:otherwise>
	      					</c:choose>	                    			      					
		      			</sec:authorize>
		      			
		      			
		      			<!--2.로그인한 유저는 다음을 실행한다. -->	  
		       			<sec:authorize access="isAuthenticated()">	
		       			
		       			  <!-- 2-1 ROLE_MEMBER 유저 실행 :  일반 유저일 경우 다음을 실행 -->
		       			  <sec:authorize access="hasRole('ROLE_MEMBER')"><!-- 일반유저  -->						      						      	
								<c:choose>
		      						<c:when test="${not empty q.password}">
		      						<!--2-1-1 비밀글일경우 다음을 실행 - 비밀번호 컬럼이 비어 있지 않을 경우 즉, on 이 들어 있을 경우
		      						 ★	boardConfirm 함수 호출 코드 추가 : 왜냐하면은 자기 자신의 글인지 아닌지 확인처리를 위해 
		      						 ★ onclick="boardConfirm(this, '${q.board_id}' ); return false;"
		      						  -->
		      							  	<a href="#"  onclick="boardConfirm(this, '${q.board_id}' ); return false;"
											data-href="board/qna/qnaDetail?board_id=${q.board_id}&pageNum=${page.currentPage}&field=${field}&word=${word}"
											>${q.subject}</a>
		      						</c:when>
		      						<c:otherwise>
		      						<!--2-1-2 비밀글이 아닐 경우 다음을 실행    -->
		      							 <a href="board/qna/qnaDetail?board_id=${q.board_id}&pageNum=${page.currentPage}&field=${field}&word=${word}"
								 			>${q.subject}</a>
		      						</c:otherwise>
		      					</c:choose>	    														
						  </sec:authorize>	
		       			
		       			
		       			
		       			 <!-- 2-2 ROLE_ADMIN 유저 실행 : 관리자일 경우 다음을 실행 -->
		       				<sec:authorize access="hasRole('ROLE_ADMIN')">
		       					<!-- 관리자  -->		       				
								<a href="board/qna/qnaDetail?board_id=${q.board_id}&pageNum=${page.currentPage}&field=${field}&word=${word}"
								 >${q.subject}</a>
							</sec:authorize>					       				
		       			
		       			</sec:authorize>
          			
						
						</td>

 

2)

<script>
function boardConfirm(e, board_id){
	$.ajax({
		url:"/board/qna/boardConfirm",
		type:"post",
		data:{
			board_id			
		},
		success:function(res){
			console.log(res);	
			if(res){//반환값이 true 이면 
				//다음 값 가져와서 페이지 이동 처리
				//data-href="board/qna/qnaDetail?board_id=${q.board_id}&pageNum=${page.currentPage}&field=${field}&word=${word}" 
				location.href=$(e).attr("data-href");
			}else{
				alert('작성자와 관리자만 접근 가능합니다.');
			}
		},
		error:function(error){
			console.log("에러 :", error);
		}		
	})
}
</script>

 

 

 

 

 

6.BoardController 클래스에서 boardConfirm 메소드 추가

 

	/**
	 * 최상의 작성자 확인 회원 아이디 값 반환처리 
	 * @param qnaboard
	 * @param session
	 * @param model
	 * @return
	 */
	@PostMapping("qna/boardConfirm")
	public ResponseEntity<?> boardConfirm(Qnaboard qnaboard, Authentication authentication,  Model model) {				
		
		//만약에 board_re_ref 파라미터 값을 받았다면 다음 1번은 실행하지 않아도 된다. 
		//1.board_id 아이디값으로 Board_re_ref 값 가져오기		
		Qnaboard getQnaboard =boardService.getQnaBoardDetail(qnaboard.getBoard_id());
		
		//2. board_re_ref 값으로 최상위 글작성자인 board_re_lev 이 0 인 조건으로   member_id 값 가져오기
		String member_id=boardService.getQnaBoardParentMemeberId(getQnaboard.getBoard_re_ref());
		
		log.info( "비밀게시글이면 작성자 확인  : {}, {}" , member_id , authentication.getName());		
		boolean result=false;//초기값을 false 설정
		
		//최상의 글작성자가 0 인 member_id 값과 현재 로그인한 유저아이디와 비교
		if(member_id.equals(authentication.getName())){
			result=true;
		}
		return ResponseEntity.status(HttpStatus.OK).body(result);
	}

 

 

 

cf ) 테이블 참조

 

 

 

 

7. BoardMapper.xml   에서 getQnaBoardParentMemeberId  추가

 

board_re_ref 값으로 최상위 글작성자인 board_re_lev 이 0 인 조건으로   member_id 값 가져오기

    <select id="getQnaBoardParentMemeberId" resultType="String">
    	SELECT member_id FROM qnaboard where board_re_ref =#{board_re_ref} and board_re_lev =0
    </select>
    
    

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

about author

PHRASE

Level 60  라이트

The apple in the neighbor's garden are sweetest. (남의 떡이 커 보인다.)

댓글 ( 4)

댓글 남기기

작성

스프링 목록    more