소스코드 다운받기

17-1 패키지, 인터페이스, 클래스 제작

  • BController
    • 클라이언트 요청(url)에 따라 전체적인 작업을 지휘
  • BCommand (인터페이스)
    • BContentCommand : 게시글 내용보기
    • BDeleteCommand : 삭제하기
    • BListCommand : 게시글 리스트
    • BModifyCommand : 수정하기
    • BReplyCommand : 답변하기
    • BReplyViewCommand : 답변보기
    • BWriteCommand : 글쓰기
  • BDao
  • BDto

17-2 Controller 제작


@Controller
public class BController {
	
	BCommand command;
	
	@RequestMapping("/list")
	public String list(Model model) {
		System.out.println("list()");
		
		command = new BListCommand();
		command.execute(model);
		
		return "list";
	}
	
	@RequestMapping("/write_view")
	public String write_view(Model model) {
		System.out.println("write_view()");
		
		return "write_view";
	}
	
	@RequestMapping("/write")
	public String write(HttpServletRequest request, Model model) {
		System.out.println("write()");
		
		model.addAttribute("request", request);
		command = new BWriteCommand();
		command.execute(model);
				
		return "redirect:list";
	}
	
	@RequestMapping("content_view")
	public String contect_view(HttpServletRequest request, Model model) {
		System.out.println("content_view()");
		
		model.addAttribute("request", request);
		command = new BContentCommand();
		command.execute(model);
		
		return "content_view";
	}
	
	@RequestMapping(method=RequestMethod.POST, value="/modify")
	public String modify(HttpServletRequest request, Model model) {
		System.out.println("modify()");
		
		model.addAttribute("request", request);
		command = new BModifyCommand();
		command.execute(model);
		
		return "redirect:list";
	}
	
	@RequestMapping("/reply_view")
	public String reply_view(HttpServletRequest request, Model model) {
		System.out.println("reply_view()");
		
		model.addAttribute("request", request);
		command = new BReplyViewCommand();
		command.execute(model);
		
		return "reply_view";
	}
	
	@RequestMapping("/reply")
	public String reply(HttpServletRequest request, Model model) {
		System.out.println("reply_view");
		
		model.addAttribute("request", request);
		command = new BReplyCommand();
		command.execute(model);
		
		return "redirect:list";
	}
	
	@RequestMapping("/delete")
	public String delete(HttpServletRequest request, Model model) {
		System.out.println("delete");
		
		model.addAttribute("request", request);
		command = new BDeleteCommand();
		command.execute(model);
		
		return "redirect:list";
	}
	
}

17-3 리스트 페이지 만들기

  • BController.class ->
@RequestMapping("/list")
public String list(Model model) {
    System.out.println("list()");
    
    command = new BListCommand();
    command.execute(model);
    
    return "list";
}
  • BListCommand.class ->
public class BListCommand implements BCommand {

	@Override
	public void execute(Model model) {
		
		// 데이터베이스에서 dto를 받아서 view로 넘겨준다
		
		BDao dao = new BDao();
		ArrayList<BDto> dtos = dao.list();
		
		model.addAttribute("list", dtos);
	}
}
  • DAO
// BDao.java
public class BDao {
	public ArrayList<BDto> list() {
		// 데이터베이스 -> ArrayList
		ArrayList<BDto> dtos = null;		
		return dtos;
	}
}
  • DTO
// BDto.java

package com.javalec.spring_pjt_board.dto;

import java.sql.Timestamp;

public class BDto {

	int bId;
	String bName;
	String bTitle;
	String bContent;
	Timestamp bDate;
	int bHit;
	int bGroup;
	int bStep;
	int bIndent;
	
	public BDto() {
		
	}
	
	public BDto(int bId, String bName, String bTitle, String bContent, Timestamp bDate, int bHit, int bGroup, int bStep, int bIndent) {
		this.bId = bId;
		this.bName = bName;
		this.bTitle = bTitle;
		this.bContent = bContent;
		this.bDate = bDate;
		this.bHit = bHit;
		this.bGroup = bGroup;
		this.bStep = bStep;
		this.bIndent = bIndent;
	}

	// Setter, Getter 생략
}

참고자료