WEB-INF/properties/mvc2.properties파일을 생성후
등록
[mvc2.properties]
/jsp/writeForm.se=mvc2.action.board.BoardWriteFormAction
/jsp/writePro.se=mvc2.action.board.BoardWriteProAction
[controller.java]
package mvc2.controller;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import mvc2.action.SuperCommandAction;
public class Controller extends HttpServlet{
private Map instanceMap = new HashMap();
@Override
public void init(ServletConfig config) throws ServletException {
String path = config.getInitParameter("propertiesPath");
//web.xml에서 propertyPath에 해당하는 init-param 의 값을 읽어옴
Properties p = new Properties();
//명령어와 처리클래스의 매핑정보를 저장할 Properties객체 생성
FileInputStream fs = null;
try{
fs = new FileInputStream(path);//mvc2.properties파일의 내용을 읽어옴
p.load(fs);//mvc2.properties파일의 정보를 Properties객체에 저장
}catch(Exception e){
e.printStackTrace();
}finally{
if (fs != null) try { fs.close(); } catch(IOException ex) {}
}
// 지금 받은 String 형태를 객체화 해야한다.
// 객체화
Iterator iter = p.keySet().iterator();//Iterator객체는 Enumeration객체를 확장시킨 개념의 객체
while(iter.hasNext()){//객체를 하나씩 꺼내서 그 객체명으로 Properties객체에 저장된 객체에 접근
String key = (String)iter.next();
String className = p.getProperty(key);
try{
Class loadClass = Class.forName(className);//해당 문자열을 클래스로 만든다.
Object instance = loadClass.newInstance();//해당클래스의 객체를 생성
instanceMap.put(key, instance);// Map객체인 commandMap에 객체 저장
}catch(Exception e){
e.printStackTrace();
}
}
}
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String uri = request.getRequestURI(); //uri뽐음
SuperCommandAction sa = (SuperCommandAction)instanceMap.get(uri);//해당 uri로 객체를 맵에 넣음
String view = sa.executeAction(request, response);
RequestDispatcher rd = request.getRequestDispatcher(view);
rd.forward(request, response);
}
}
[SuperAction.java]
package mvc2.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface SuperAction {
public String executeAction(HttpServletRequest request, HttpServletResponse response);
//예제의 superaction, commandAction과 동일한 기능임
}
[BoardWriteFormAction.java]
package mvc2.action.board;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import bean.MemberDAO;
import bean.MemberDTO;
import mvc2.action.SuperCommandAction;
public class BoardWriteFormAction implements SuperCommandAction{
public String executeAction(HttpServletRequest request, HttpServletResponse response) {
int num=0; //글 고유 번호
int ref=1; //레코드의 그룹 번호
int re_step=0; //같은 그룹 내에서 레코드 간의 위치
int re_level=0; //레코드의 그룹 내에서 레벨
HttpSession session = request.getSession();
String id = (String)session.getAttribute("memId");
MemberDAO dao = MemberDAO.getInstance();
MemberDTO member = dao.getMember(id);
try{
if(request.getParameter("num")!=null){//(답변글이면)
num=Integer.parseInt(request.getParameter("num"));
ref=Integer.parseInt(request.getParameter("ref"));
re_step=Integer.parseInt(request.getParameter("re_step"));
re_level=Integer.parseInt(request.getParameter("re_level"));
}
}catch(Exception e){
e.printStackTrace();
}
//해당 뷰에서 사용할 속성
request.setAttribute("num", new Integer(num));
request.setAttribute("ref", new Integer(ref));
request.setAttribute("re_step", new Integer(re_step));
request.setAttribute("re_level", new Integer(re_level));
request.setAttribute("writer", id);
request.setAttribute("email", member.getEmail());
return "/board/writeForm.jsp";
}
}
[BoardWriteProAction.java]
package mvc2.action.board;
import java.sql.Timestamp;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import bean.BoardDAO;
import bean.BoardDTO;
import mvc2.action.SuperCommandAction;
public class BoardWriteProAction implements SuperCommandAction{
public String executeAction(HttpServletRequest request, HttpServletResponse response) {
try{
request.setCharacterEncoding("euc-kr");
BoardDTO article = new BoardDTO();
article.setNum(Integer.parseInt(request.getParameter("num")));
article.setWriter(request.getParameter("writer"));
article.setEmail(request.getParameter("email"));
article.setSubject(request.getParameter("subject"));
article.setContent(request.getParameter("content"));
article.setPasswd(request.getParameter("passwd"));
article.setRef(Integer.parseInt(request.getParameter("ref")));
article.setRe_step(Integer.parseInt(request.getParameter("re_step")));
article.setRe_level(Integer.parseInt(request.getParameter("re_level")));
article.setReg_date(new Timestamp(System.currentTimeMillis()));
article.setIp(request.getRemoteAddr());
BoardDAO dao = BoardDAO.getInstance();
dao.insertArticle(article);
}catch(Exception e){
e.printStackTrace();
}
return "/board/writePro.jsp";
}
}
[writeForm.jsp]
<%@ page contentType="text/html; charset=euc-kr" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>일반게시판</title>
</head>
<body>
일반게시판글쓰기
<br />
<form method="post" name="writeform" action="/jsp/writePro.se" onsubmit="return writeSave()">
<input type="hidden" name="num" value="${num}">
<input type="hidden" name="ref" value="${ref}">
<input type="hidden" name="re_step" value="${re_step}">
<input type="hidden" name="re_level" value="${re_level}">
<table width="400" border="1" cellspacing="0" cellpadding="0" align="center">
<tr>
<td align="right" colspan="2" >
<a href="/jsp/list.se">글목록</a>
</td>
</tr>
<tr>
<td width="110" align="center">이름</td>
<td width="330">
${writer}
<input type="hidden" name="writer" value="${writer}">
</td>
</tr>
<tr>
<td width="110" align="center" >제목</td>
<td width="330">
<c:if test="${num==0}">
<input type="text" size="40" maxlength="50" name="subject">
</c:if>
<c:if test="${num!=0}">
<input type="text" size="40" maxlength="50" name="subject" value="[re:]">
</c:if>
</td>
</tr>
<tr>
<td width="110" align="center">Email</td>
<td width="330">
${email}
<input type="hidden" name="email" value="${email}">
</td>
</tr>
<tr>
<td width="110" align="center" >내 용</td>
<td width="330" >
<textarea name="content" rows="13" cols="45"></textarea>
</td>
</tr>
<tr>
<td colspan=2 align="center">
<input type="submit" value="글쓰기" >
<input type="reset" value="다시작성">
<input type="button" value="목록보기" OnClick="window.location='/jsp/list.se'">
</td>
</tr>
</table>
</form>
</body>
</html>
[writePro.jsp]
<%@ page contentType="text/html;charset=euc-kr" %>
<meta http-equiv="Refresh" content="0;url=/jsp/list.se" >