728x90
★ 파일 생성
com.test.service > BoardService.java(I)
> BoardServiceImpl.java(C)
com.test.mapper > BoardMapper.java(I)
com.test.domain > BoardDTO.java
src/main/resource > com > test > mapper > BoardMapper.xml
src/test/java > com.test.board > BoardTest.java
■ DB 생성
create table tblBoard (
seq number primary key,
subject varchar2(1000) not null,
content varchar2(2000) not null,
regdate date default sysdate not null,
id varchar2(30) not null references tblMember(id)
);
★ 코드 작성 (CRUD 기능)
- BoardService.java -> 코드 작성
@Service
public class BoardServiceImpl implements BoardService {
@Autowired
private BoardMapper mapper;
//글쓰기
int add(BoardDTO dto);
//목록 보기
List<BoardDTO> list();
//상세 보기
BoardDTO get(String seq);
//수정 하기
int edit(BoardDTO dto);
//삭제 하기
int del(String seq);
}
- BoardServiceImpl.java -> 코드 작성
@Service
public class BoardServiceImpl implements BoardService {
@Autowired
private BoardMapper mapper;
//글쓰기
public int add(BoardDTO dto) {
return mapper.add(dto);
}
//목록보기
@Override
public List<BoardDTO> list() {
return mapper.list();
}
//상세보기
@Override
public BoardDTO get(String seq) {
return mapper.get(seq);
}
//수정하기
@Override
public int edit(BoardDTO dto) {
return mapper.edit(dto);
}
//삭제하기
@Override
public int del(String seq) {
// TODO Auto-generated method stub
return mapper.del(seq);
}
}
- BoardTest.java -> 코드 작성 및 실행 -> 테스트
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"file:src/main/webapp/WEB-INF/spring/root-context.xml", "file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml"})
@Log4j
public class BoardTest {
@Autowired
private BoardService service;
/*
@Test
public void testAdd() {
BoardDTO dto = new BoardDTO();
dto.setId("cat");
dto.setSubject("나는 고양이다");
dto.setContent("냐옹냐옹");
int result = service.add(dto);
log.info(result); //1
}
*/
/*
@Test
public void testList() {
List<BoardDTO> list = service.list();
for(BoardDTO dto : list) {
log.info(dto);
}
}
*/
/*
@Test
public void testGet() {
BoardDTO dto = service.get("1");
System.out.println(dto);
}
*/
/*
@Test
public void testEdit() {
BoardDTO dto = new BoardDTO();
dto.setSeq("1");
dto.setSubject("수정한 제목입니다.");
dto.setContent("수정한 내용입니다.");
log.info(service.edit(dto));
log.info(service.get("1"));
}
*/
@Test
public void testDel() {
log.info(service.del("301"));
log.info(service.list());
}
}
- BoardMapper.java
public interface BoardMapper {
int add(BoardDTO dto);
List<BoardDTO> list();
BoardDTO get(String seq);
int edit(BoardDTO dto);
int del(String seq);
}
- BoardMapper.xml
<mapper namespace="com.test.mapper.BoardMapper">
<!-- add 기능 -->
<insert id="add">
insert into tblBoard (seq, subject, content, regdate, id)
values (seqBoard.nextVal, #{subject}, #{content}, default, #{id})
</insert>
<!-- list 기능 -->
<select id="list" resultType="com.test.domain.BoardDTO">
select
seq, subject, regdate, id
from tblBoard
order by seq desc
</select>
<!-- detail 기능 -->
<select id="get" resultType="com.test.domain.BoardDTO">
select
*
from tblBoard
where seq = #{seq}
</select>
<!-- update 기능 -->
<update id="edit">
update tblBoard set
subject = #{subject},
content = #{content}
where seq = #{seq}
</update>
<!-- delete 기능 -->
<delete id="del">
delete from tblBoard
where seq = #{seq}
</delete>
</mapper>
★ 실제 작업
- BoardController.java
@Controller
public class BoardController {
@Autowired
private BoardService service;
@GetMapping("/board/list.do")
public String list(Model model) {
model.addAttribute("list", service.list());
return "board/list";
}
@GetMapping("/board/add.do")
public String add() {
return "board/add";
}
@PostMapping("/board/addok.do")
public String addok() {
return "redirect:/board/list.do";
}
@GetMapping("/board/view.do")
public String view() {
return "board/view";
}
@GetMapping("/board/edit.do")
public String edit() {
return "board/edit";
}
@PostMapping("/board/editok.do")
public String editok() {
return "redirect:/board/list.do";
}
@GetMapping("/board/del.do")
public String del() {
return "board/del";
}
@PostMapping("/board/delok.do")
public String delok() {
return "redirect:/board/delok.do";
}
}
- list.jsp -> 리스트들을 띄우는 페이지
<!-- list.jsp -->
<%@ include file="/WEB-INF/views/inc/header.jsp" %>
<h2>Board <small>제목</small></h2>
<table>
<tr>
<th>번호</th>
<th>제목</th>
<th>날짜</th>
<th>아이디</th>
</tr>
<c:forEach items="${list}" var="dto">
<tr>
<td>${dto.seq}</td>
<td><a href="/board/view.do?seq=${dto.seq}">${dto.subject}</td>
<td>${dto.regdate}</td>
<td>${dto.id}</td>
</tr>
</c:forEach>
</table>
<div>
<button type="button" class="add" onclick="location.href='/board/add.do';">글쓰기</button>
</div>
- add.jsp -> 추가하는 페이지
<!-- add.jsp -->
<%@ include file="/WEB-INF/views/inc/header.jsp" %>
<h2>Board <small>add</small></h2>
<form method="POST" action="/board/addok.do">
<table class="vertical">
<tr>
<th>제목</th>
<td><input type="text" name="subject" class="full" required></td>
</tr>
<tr>
<th>내용</th>
<td><textarea name="content" class="full" required></textarea></td>
</tr>
</table>
<div>
<button type="button" class="back" onclick="location.href='/board/list.do';">돌아가기</button>
<button type="submit" class="add">글쓰기</button>
</div>
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}">
<input type="hidden" name="id" value='<sec:authentication property="principal.username"/>'>
</form>
- view.jsp -> 상세보기
<!-- view.jsp -->
<%@ include file="/WEB-INF/views/inc/header.jsp" %>
<h2>Board <small>add</small></h2>
<table class="vertical">
<tr>
<th>번호</th>
<td>${dto.seq}</td>
</tr>
<tr>
<th>아이디</th>
<td>${dto.id}</td>
</tr>
<tr>
<th>제목</th>
<td>${dto.subject}</td>
</tr>
<tr>
<th>내용</th>
<td>${dto.content}</td>
</tr>
<tr>
<th>날짜</th>
<td>${dto.regdate}</td>
</tr>
</table>
<div>
<button type="button" class="back" onclick="location.href='/board/list.do';">돌아가기</button>
<sec:authentication property="principal" var="pinfo"/>
<sec:authorize access="isAuthenticated()">
<c:if test="${pinfo.username == dto.id}">
<button type="button" class="edit" onclick="location.href='/board/edit.do?seq=${dto.seq}';">수정하기</button>
<button type="button" class="del" onclick="location.href='/board/del.do?seq=${dto.seq}';"> 삭제하기</button>
</c:if>
</sec:authorize>
</div>
- edit.jsp -> 수정하기
<!-- edit.jsp -->
<%@ include file="/WEB-INF/views/inc/header.jsp" %>
<h2>Board <small>edit</small></h2>
<form method="POST" action="/board/editok.do">
<table class="vertical">
<tr>
<th>제목</th>
<td><input type="text" name="subject" class="full" required value="${dto.subject}"></td>
</tr>
<tr>
<th>내용</th>
<td><textarea name="content" class="full" required >${dto.content}</textarea></td>
</tr>
</table>
<div>
<button type="button" class="back" onclick="location.href='/board/view.do?seq=${dto.seq}';">돌아가기</button>
<button type="submit" class="edit">수정하기</button>
</div>
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}">
<input type="hidden" name="id" value='<sec:authentication property="principal.username"/>'>
<input type="hidden" name="seq" value="${dto.seq}"/>
</form>
- del.jsp -> 삭제 기능
<!-- del.jsp -->
<%@ include file="/WEB-INF/views/inc/header.jsp" %>
<h2>Board <small>del</small></h2>
<form method="POST" action="/board/delok.do">
<div>
<button type="button" class="back" onclick="location.href='/board/view.do?seq=${seq}';">돌아가기</button>
<button type="submit" class="del">삭제하기</button>
</div>
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}">
<input type="hidden" name="id" value='<sec:authentication property="principal.username"/>'>
<input type="hidden" name="seq" value="${seq}"/>
</form>
■ 권한 체크 : 접근 할 수 없는 페이지 막기
---------------- 인증 사용자만 접근 가능 -------------
- add.do, addok.do
- edit.do, editok.do
- del.do, delok.do
- security-context.xml -> 작성 : 하지만, 일일히 해줘야 해는 노가다 작업임..
방법 1
<!-- <security:intercept-url pattern="/board/add.do" access="isAuthenticated()" /> -->
방법 2
<!-- <security:intercept-url pattern="/board/add.do" access="hasRole('ROLE_MEMBER')" />-->
- servlet-context.xml -> namespace -> security체크 -> 코드작성
<security:global-method-security pre-post-annotations="enabled"/>
- BoardController.java -> 추가
감추고자 하는 요소에
@PreAuthorize("isAuthenticated()")
이것을 추가
@GetMapping("/board/del.do")
public String del(Model model, String seq) {
model.addAttribute("seq", seq);
return "board/del";
}
■ 실행 결과
- home
- Login
- Board -> list
- Board -> view
- Board -> view -> edit
- Board -> view -> del
728x90
'Spring' 카테고리의 다른 글
Spring STEP 15 - WEB SOCKET (2) | 2023.06.23 |
---|---|
Spring STEP 14 - RESTful Service (0) | 2023.06.22 |
Spring STEP 13 - Board (0) | 2023.06.21 |
Spring STEP 12 - Spring Security 2 (0) | 2023.06.21 |
Spring STEP 11 - Spring Security (0) | 2023.06.20 |