Controller.java
☞ 모든 Controller의 최상위 Type으로 모든 Controller의 실행 메소드인 execute()를 선언
package board.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import board.dto.ForwardDTO;
public interface Controller {
public ForwardDTO execute(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException;
}
BoardFrontController.java
☞ Client의 모든 요청을 받는 서블릿 클래스. 요청에 대한 처리는 Action들이 처리한다.
package board.controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import board.dto.ForwardDTO;
public class BoardFrontController extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1. 공통 사전 작업 - 한글처리
request.setCharacterEncoding("utf-8");
//2. Controller 로직 처리 - Controller객체.execute() 호출
String command = request.getParameter("command");
Controller ctr = ControllerCommandMapping.getController(command);
ForwardDTO fdto = ctr.execute(request, response);
//3. 공통 사후 작업
if(fdto.isRedirect()) {
response.sendRedirect(fdto.getUrl());
} else {
RequestDispatcher rdp = request.getRequestDispatcher(fdto.getUrl());
rdp.forward(request, response);
}
}
}
ControllerCommandMapping.java
☞ Client가 보내온 Command 인자 값에 따라 요청을 처리할 Action객체를 만들어 return하는 Factory 클래스
package board.controller;
public class ControllerCommandMapping {
public static Controller getController(String command) {
Controller ctr = null;
if(command.equals("write")) {
ctr = new WriteContentController();
} else if(command.equals("get")) {
ctr = new GetContentController();
} else if(command.equals("replyform")) {
ctr = new ReplyFormController();
} else if(command.equals("reply")) {
ctr = new ReplyContentController();
} else if(command.equals("modifyform")) {
ctr= new ModifyFormController();
} else if(command.equals("modify")) {
ctr = new ModifyContentController();
} else if(command.equals("boardlist")) {
ctr = new BoardListController();
} else if(command.equals("delete")) {
ctr = new DeleteContentController();
}
return ctr;
}
}
BoardDTO.java
☞ 게시물의 데이터를 저장하는 VO Class
package board.dto;
/**
* @author Evan
*
*/
public class BoardDTO {
private int no;
private String title;
private String writer;
private String content;
private String writedate;
private int viewcount;
private int refamily;
private int restep;
private int relevel;
public BoardDTO() {}
public BoardDTO(String title, String wirter, String content) {
this.title = title;
this.writer = wirter;
this.content = content;
}
public BoardDTO(int no, String title, String writer, String content,
String writedate, int viewcount, int refamily, int restep,
int relevel) {
this.no = no;
this.title = title;
this.writer = writer;
this.content = content;
this.writedate = writedate;
this.viewcount = viewcount;
this.refamily = refamily;
this.restep = restep;
this.relevel = relevel;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getWritedate() {
return writedate;
}
public void setWritedate(String writedate) {
this.writedate = writedate;
}
public int getViewcount() {
return viewcount;
}
public void setViewcount(int viewcount) {
this.viewcount = viewcount;
}
public int getRefamily() {
return refamily;
}
public void setRefamily(int refamily) {
this.refamily = refamily;
}
public int getRestep() {
return restep;
}
public void setRestep(int restep) {
this.restep = restep;
}
public int getRelevel() {
return relevel;
}
public void setRelevel(int relevel) {
this.relevel = relevel;
}
@Override
public String toString() {
return "boardDTO [no=" + no + ", title=" + title + ", writer=" + writer
+ ", content=" + content + ", writedate=" + writedate
+ ", viewcount=" + viewcount + ", refamily=" + refamily
+ ", restep=" + restep + ", relevel=" + relevel + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((content == null) ? 0 : content.hashCode());
result = prime * result + no;
result = prime * result + refamily;
result = prime * result + relevel;
result = prime * result + restep;
result = prime * result + ((title == null) ? 0 : title.hashCode());
result = prime * result + viewcount;
result = prime * result + ((writer == null) ? 0 : writer.hashCode());
result = prime * result
+ ((writedate == null) ? 0 : writedate.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
BoardDTO other = (BoardDTO) obj;
if (content == null) {
if (other.content != null)
return false;
} else if (!content.equals(other.content))
return false;
if (no != other.no)
return false;
if (refamily != other.refamily)
return false;
if (relevel != other.relevel)
return false;
if (restep != other.restep)
return false;
if (title == null) {
if (other.title != null)
return false;
} else if (!title.equals(other.title))
return false;
if (viewcount != other.viewcount)
return false;
if (writer == null) {
if (other.writer != null)
return false;
} else if (!writer.equals(other.writer))
return false;
if (writedate == null) {
if (other.writedate != null)
return false;
} else if (!writedate.equals(other.writedate))
return false;
return true;
}
}
ForwardDTO.java
☞ Action들이 Business Logic 처리(Model과 연동)를 끝낸 뒤 이동할 페이지와 이동방법을 담는 VO
package board.dto;
public class ForwardDTO {
private String url;
private boolean redirect;
public ForwardDTO() {}
public ForwardDTO(String url, boolean redirect) {
this.url = url;
this.redirect = redirect;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public boolean isRedirect() {
return redirect;
}
public void setRedirect(boolean redirect) {
this.redirect = redirect;
}
@Override
public String toString() {
return "ForwardDTO [url=" + url + ", redirect=" + redirect + "]";
}
}
'Wanna be a Programmer > Servlet & JSP' 카테고리의 다른 글
Servlet & JSP day20 - 게시판(새글 등록) (0) | 2012.05.18 |
---|---|
Servlet & JSP day18 - 게시판(Database, Utillity) (0) | 2012.05.15 |
Servlet & JSP day17 - Front Controller (0) | 2012.05.14 |
Servlet & JSP day17 - MVC 모델을 응용한 회원정보 관리(회원탈퇴) (0) | 2012.05.14 |
Servlet & JSP day17 - MVC 모델을 응용한 회원정보 관리(전체회원정보조회) (0) | 2012.05.14 |