JSP - °Ô½ÃÆÇ ¸¸µé±â ¿¬½À
JSPÄÚµå, ¼ºí¸´(ÀÚ¹Ù¾ð¾î), MyBatis ÇÁ·¹ÀÓ¿÷À» È°¿ëÇÑ ÀÎÅÍÆäÀ̽º¸¦ ±¸ÇöÇÑ ¼Ò½º ¹× ½ÇÇàȸé ĸÃÄ
1. ´Ù¿î·Îµå ¹ÞÀº À¯Æ¿µéÀ» Æú´õ¿¡ ³Ö¾îÁØ´Ù.
2. ·Î±×¹® Ãâ·ÂÀ» µµ¿ÍÁÙ log4j.properties ÆÄÀÏÀ» ¸¸µç´Ù
# Root logger option
# Log4j Setting file
log4j.rootLogger=DEBUG, console, R
# root ¼³Á¤
# Daily file log
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=J:\\logs/member.log
log4j.appender.R.DatePattern='.'yyyy-MM-dd
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=[%d{HH:mm:ss}][%-5p](%F:%L)-%m%n
# Console log (ÄÜ¼Ö ·Î±×)
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%d{HH:mm:ss}][%-5p](%F:%L)-%m%n
log4j.appender.console.ImmediateFlush=true
log4j.appender.R.File= ½ÇÁ¦ Æú´õ°¡ ÀÖ´Â ¸µÅ©·Î °æ·ÎÁöÁ¤
3. MyBatis ÇÁ·¹ÀÓ¿÷ »ç¿ëÀ» À§ÇÑ ÆÐÅ°Áö¸¦ ¸¸µé¾î ÁØ´Ù.
4. com.mvctest.bean ÆÐÅ°Áö ¾È¿¡ MvcBoard.java ÆÄÀÏÀ» ¸¸µé¾îÁØ´Ù
package com.mvctest.bean;
import java.sql.Timestamp;
public class MvcBoard {
private long bId; // ÀÚµ¿Áõ°¡
private String mId; // °Ô½ÃÀÚ ¾ÆÀ̵ð
private String bTitle;// °Ô½Ã±ÛÁ¦¸ñ
private String bContent;// °Ô½Ã±Û ³»¿ë
private Timestamp bDate; // °Ô½ÃÀϽÃ
private int bHit; // °Ô½Ã±ÛÀÐÀº Ƚ¼ö
private int bGroup;// ¼Ò¼Ó º»±Û bId
private int bStep;// ´ñ±ÛÀÇ °Ô½Ã¼ø¼
private int bIndent;// ¸®½ºÆ®¿¡ ´ñ±ÛÀÇ ´ñ±Û µî µé¿©¾²±â
//bean ÆÄÀÏÀº ±âº»»ý¼ºÀÚ°¡ ÀÖ¾î¾ßÇÑ´Ù.
public MvcBoard() { }
public long getbId() {
return bId;
}
public void setbId(long bId) {
this.bId = bId;
}
public String getmId() {
return mId;
}
public void setmId(String mId) {
this.mId = mId;
}
public String getbTitle() {
return bTitle;
}
public void setbTitle(String bTitle) {
this.bTitle = bTitle;
}
public String getbContent() {
return bContent;
}
public void setbContent(String bContent) {
this.bContent = bContent;
}
public Timestamp getbDate() {
return bDate;
}
public void setbDate(Timestamp bDate) {
this.bDate = bDate;
}
public int getbHit() {
return bHit;
}
public void setbHit(int bHit) {
this.bHit = bHit;
}
public int getbGroup() {
return bGroup;
}
public void setbGroup(int bGroup) {
this.bGroup = bGroup;
}
public int getbStep() {
return bStep;
}
public void setbStep(int bStep) {
this.bStep = bStep;
}
public int getbIndent() {
return bIndent;
}
public void setbIndent(int bIndent) {
this.bIndent = bIndent;
}
@Override
public String toString() {
return "MvcBoard [bId=" + bId + ", mId=" + mId + ", bTitle=" + bTitle + ", bContent=" + bContent + ", bDate="
+ bDate + ", bHit=" + bHit + ", bGroup=" + bGroup + ", bStep=" + bStep + ", bIndent=" + bIndent + "]";
}
}
bean ÆÄÀÏÀº ¹Ì¸® »ý¼ºÇØ ³õÀº MVC_BOARD Å×À̺íÀ» Åä´ë ¸¸µé¾ú´Ù
5. com.mvctest.dao ÆÐÅ°Áö ¾È¿¡ MvcBoardDAO.java ÀÎÅÍÆäÀ̽º¸¦ ¸¸µé¾î ÁØ´Ù.
¸¸µé¾îÁØ ÀÎÅÍÆäÀ̽º ¾È¿¡´Â À¥ ÆäÀÌÁö¿¡¼ »ç¿ëÇÒ ±â´ÉµéÀ» »ý°¢Çϸç ÀÛ¼ºÇØÁØ´Ù.
public interface MvcBoardDAO {
// À¥ÆäÀÌÁö¿¡¼ »ç¿ëÇÒ ±â´ÉÀ» ƯÈ
public void write(MvcBoard bean);
public List<MvcBoard> list();
public MvcBoard contentView(long bId);
public void modify(MvcBoard bean);
public void delete(long bId);
public MvcBoard reply_view(long bId);
public void reply(MvcBoard bean);
public void replyShape(HashMap<String, Integer> map);
public void upHit(long bId);
}
6. com.mvctest.common ÆÐÅ°Áö¿¡ SMC.xmlÀ» ¸¸µé¾î ÁØ´Ù.
<?xml version="1.0" encoding="UTF-8"?>
<!-- ¾à¼Óó·³ ¿Ü¿ö¾ßÇÏ´Â ºÎºÐ -->
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD config 3.0//EN"
"http://mybatis.apache.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias type="com.mvctest.bean.MvcBoard" alias="board" />
</typeAliases>
<environments default="development">
<environment id ="development">
<transactionManager type="JDBC" />
<dataSource type = "UNPOOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:xe"/>
<property name="username" value="mvcboard"/>
<property name="password" value="mvcboard"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource = "com/mvctest/xml/MvcBoard.xml" />
</mappers>
</configuration>
»ç¿ëÇÑ ¸ðµ¨¸í = "com.mvctest.bean.MvcBoard" ÀÌ ³Ê¹« ±æ±â ¶§¹®¿¡ º°Äª board¸¦ ¸¸µé¾î ÁØ´Ù.
<typeAlias type="com.mvctest.bean.MvcBoard" alias="board" />
<enviroment>À» ÀÌ¿ëÇØ ¾î´À DB¿¡ Á¢¼Ó ÇÒ °ÍÀÎÁö ¼³Á¤ÇØÁØ´Ù.
<mapper>À» ÀÌ¿ëÇØ ¾î¶² SQLµéÀ» »ç¿ëÇÒÁö ºÒ·¯¿ÍÁØ´Ù.
7. mybatis¸¦ ºÎ¸±?? »ç¿ëÇÒ ¼ö ÀÖ´Â À¯Æ¿¸®Æ¼ Ŭ·¡½º
//mybatis¸¦ ºÎ¸±?? »ç¿ëÇÒ ¼ö ÀÖ´Â À¯Æ¿¸®Æ¼ Ŭ·¡½º
public class MBUtils {
public static SqlSessionFactory getSqlSessionFatory() {
String resource = "com/mvctest/common/SMC.xml" ;
InputStream reader = null;
try {
reader = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
throw new IllegalArgumentException(e);
}
return new SqlSessionFactoryBuilder().build(reader);
}
public static SqlSession getSession() {
return getSqlSessionFatory().openSession(true);
}
}
½Ç»ç¿ëÇÒ XMLÀÇ ¸µÅ©¸¦ Àû¾îÁØ´Ù. À̶§ ±¸ºÐÀº '/'·Î ÇÒ°Í¿¡ À¯ÀÇ
8. ÁúÀǹ®ÀÌ ÀûÈù MvcBoard.xmlÀ» »ý¼ºÇØÁØ´Ù.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace = "com.mvctest.dao.MvcBoardDAO">
<resultMap id = "result" type = "board">
<result property = "bId" column="BID"/>
<result property = "mId" column="MID"/>
<result property = "bTitle" column="BTITLE"/>
<result property = "bContent" column="BCONTENT"/>
<result property = "bDate" column="BDATE"/>
<result property = "bHit" column="BHIT"/>
<result property = "bGroup" column="BGROUP"/>
<result property = "bStep" column="BSTEP"/>
<result property = "bIndent" column="BINDENT"/>
</resultMap>
<insert id = "write" parameterType="board">
INSERT INTO MVC_BOARD(
BID,MID,BTITLE,BCONTENT,BDATE,BHIT,BGROUP,BSTEP,BINDENT
) VALUES(
MVC_BOARD_SEQ.NEXTVAL,#{mId},#{bTitle},#{bContent},
SYSDATE,0,MVC_BOARD_SEQ.CURRVAL,0,0
)
</insert>
<select id="list" resultMap = "result">
SELECT BID,MID,BTITLE,BCONTENT,BDATE,BHIT,BGROUP,BSTEP,BINDENT
FROM MVC_BOARD
ORDER BY BGROUP DESC, BSTEP ASC
</select>
<select id = "contentView" parameterType="long" resultMap="result">
SELECT * FROM
MVC_BOARD
WHERE BID = #{value}
</select>
<update id = "modify" parameterType="board">
UPDATE MVC_BOARD
SET BTITLE = #{bTitle}, BCONTENT=#{bContent}
WHERE BID = #{bId}
</update>
<delete id="delete" parameterType="long">
DELETE FROM MVC_BOARD WHERE BID = #{value}
</delete>
<select id = "reply_view" parameterType="long" resultMap="result">
SELECT * FROM
MVC_BOARD
WHERE BID = #{value}
</select>
<insert id="reply" parameterType="board">
<![CDATA[
INSERT INTO MVC_BOARD(
BID,MID,BTITLE,BCONTENT,BDATE,BHIT,BGROUP,BSTEP,BINDENT
) VALUES(
MVC_BOARD_SEQ.NEXTVAL, #{mId}, #{bTitle}, #{bContent},
SYSDATE,0,#{bGroup},#{bStep}+1,#{bIndent}+1
)
]]>
</insert>
<update id="replyShape" parameterType="hashMap">
<![CDATA[
UPDATE MVC_BOARD SET BSTEP = #{bStep}+1
WHERE BGROUP = #{bGroup}
AND BSTEP > #{bStep}
]]>
</update>
<update id = "upHit" parameterType = "long">
UPDATE MVC_BOARD SET BHIT = BHIT + 1
WHERE BID = #{value}
</update>
</mapper>
¿©±âÀÇ Å¸ÀÔ¸í board´Â SMC.xml¿¡¼ ¸¸µé¾îÁØ alias="board" ¿Í °°Àº À̸§ÀÌ¿©¾ßÇÑ´Ù.
<resultMap id = "result" type = "board">
<insert id = "write" parameterType="board">
9. ´ÙÀ½Àº ½ÇÁ¦ À¥ »ó¿¡¼ º¸ÀÏ jsp ÆäÀÌÁö¸¦ ¸¸µéÀÚ
°¢°¢ ¾²±â, ¸ñ·Ï, Àбâ, ´ä±Û È®ÀÎÀÌ ÀÖ´Ù.
<body>
<h1>°Ô½Ã±Û ÀÛ¼º</h1>
<form action="write.do" method="post">
<table >
<tr>
<th>°Ô½ÃÀÚid</th>
<td><input type="text" name="mId" size="50"></td>
</tr>
<tr>
<th>Á¦¸ñ</th>
<td><input type="text" name="bTitle" size="50"></td>
</tr>
<tr>
<th>³»¿ë</th>
<td><textarea name="bContent" rows="20" cols="50"></textarea></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="ÀÔ·Â">
<a href="list.do">¸ñ·Ïº¸±â</a></td>
</tr>
</table>
</form>
</body>
</html>
form actionÀ» ÀÌ¿ëÇØ ÀԷ°ªÀ» list.do ¶ó´Â ÆäÀÌÁö¿¡ post ŸÀÔÀ¸·Î ³Ñ°ÜÁØ´Ù.
<h1>°Ô½Ã±Û¸ñ·Ï</h1>
<%
List<MvcBoard> list = (List<MvcBoard>) request.getAttribute("list");
if (list == null) {
SqlSession mysession = MBUtils.getSession();
MvcBoardDAO dao = mysession.getMapper(MvcBoardDAO.class);
list = dao.list();
request.setAttribute("list", list);
mysession.close();
}
%>
<table>
<colgroup>
<col width="5%">
<col width="10%">
<col width="*">
<col width="25%">
<col width="10%">
</colgroup>
<tr>
<th>No</th>
<th>°Ô½ÃÀÚ</th>
<th>Á¦¸ñ</th>
<th>³¯Â¥</th>
<th>Á¶È¸¼ö</th>
</tr>
<%
for (MvcBoard x : list) {
%>
<tr>
<td class="tc"><%=x.getbId() %></td>
<td class="tc"><%=x.getmId() %></td>
<td><a href="content_view.do?bId=<%=x.getbId()%>"><%=x.getbTitle() %></a></td>
<td class="tc"><%=x.getbDate() %></td>
<td class="tc"><%=x.getbHit()%></td>
</tr>
<%
}
%>
</table>
<a class="center" href="write.jsp">±ÛÀÛ¼º</a>
<%
List<MvcBoard> list = (List<MvcBoard>) request.getAttribute("list");
if (list == null) {
SqlSession mysession = MBUtils.getSession();
MvcBoardDAO dao = mysession.getMapper(MvcBoardDAO.class);
list = dao.list();
request.setAttribute("list", list);
mysession.close();
}
%>
ÀÏ´Ü DB¿¡¼ ±Û ¸ñ·ÏÀ» °¡Á®¿Í¾ß ÇÑ´Ù.
±Û ¸ñ·ÏÀÌ ÀúÀåµÉ list¶õ À̸§ÀÇ ¸®½ºÆ®¸¦ ´ã¾Æ ¹ÝȯÇØÁÖ´Â List<MvcBoard>¸¦ »ý¼º
getAtrribute(String name) ¸Þ¼Òµå¸¦ ÀÌ¿ëÇØ listÀÇ °ªÀ» ±¸ÇØÁÖ¸ç
ÁöÁ¤ÇÑ À̸§ÀÇ ¼Ó¼ºÀÌ Á¸ÀçÇÏÁö ¾ÊÀ» °æ¿ì nullÀ» ¸®ÅÏÇÑ´Ù.
getSessionÀ» ÀÌ¿ëÇØ °ªÀ» °¡Á®¿Ã db¿Í ¿¬°áÇÑ´Ù.
¿¬°áµÈ db¸¦ getMapperÀ» ÀÌ¿ëÇØ SQLÁö¹® ÀÎÅÍÆäÀ̽º Ŭ·¡½º¿Í ¿¬°á
setAttribute·Î »ç¿ëÇÒ MvcBoardDAO¿¡¼ À̸§ Á¤ÇÑ list ÁúÀǸ¦ ¼±ÅÃÇØÁØ´Ù.
table·Î html ÄÚµùÀ» ÇØÁØ´Ù.
<%
for (MvcBoard x : list) {
%>
<tr>
<td class="tc"><%=x.getbId() %></td>
<td class="tc"><%=x.getmId() %></td>
<td><a href="content_view.do?bId=<%=x.getbId()%>"><%=x.getbTitle() %></a></td>
<td class="tc"><%=x.getbDate() %></td>
<td class="tc"><%=x.getbHit()%></td>
</tr>
<%
}
%>
ÀÌÁ¦ list¸¦ À§¿¡¼ ¸¸µé¾úÀ¸´Ï for¹®À» ÀÌ¿ëÇØ list°ªÀ» »Ñ·ÁÁØ´Ù.
Á¦¸ñÀº Ŭ¸¯½Ã viewÆäÀÌÁö·Î ³Ñ¾î°¡¾ß ´Ï±î a ű׷Π¸µÅ©¸¦ °É¾îÁØ´Ù.
<h1>±ÛÀбâ</h1>
<form action="modify.do" method="post">
<table>
<colgroup>
<col width="20%">
<col width="*">
</colgroup>
<tr class="hidden">
<td colspan="2">
<input type="hidden" name="bId" value="${content_view.bId }"/>
</td>
</tr>
<tr>
<th>¹øÈ£</th>
<td>${content_view.bId }</td>
</tr>
<tr>
<th>È÷Æ®</th>
<td>${content_view.bHit }</td>
</tr>
<tr>
<th>°Ô½ÃÀÚ</th>
<td>${content_view.mId }</td>
</tr>
<tr>
<th>Á¦¸ñ</th>
<td>
<input class="w90" type="text" name="bTitle" value="${content_view.bTitle }"/>
</td>
</tr>
<tr>
<th>³»¿ë</th>
<td>
<textarea class="w90" name="bContent" >${content_view.bContent }</textarea>
</td>
</tr>
<tr>
<td colspan="2" class="center">
<input type="submit" value="¼öÁ¤"/>
<a href="delete.do?bId=${content_view.bId}">»èÁ¦</a>
<a href="reply_view.do?bId=${content_view.bId}">´ñ±Û´Þ±â</a>
<a href="list.do">¸ñ·Ï</a>
</td>
</tr>
</table>
</form>
<input type="hidden" name="bId" value="${content_view.bId }"/>
bid´Â °ªÀÌ ÀÖ¾î¾ß ÇÏÁö¸¸ ´«À¸·Î º¸ÀÏ ÇÊ¿ä´Â ¾ø¾î¼ hiddenÀ¸·Î °¨ÃçÁØ´Ù.
<tr>
<th>Á¦¸ñ</th>
<td>
<input class="w90" type="text" name="bTitle" value="${content_view.bTitle }"/>
</td>
</tr>
<tr>
<th>³»¿ë</th>
<td>
<textarea class="w90" name="bContent" >${content_view.bContent }</textarea>
</td>
</tr>
value °ªÀ¸·Î ${ } Á¤º¸¸¦ ±âº» °¡Á®¿À¸é¼ ¼öÁ¤µµ ÇÒ ¼ö ÀÖ°Ô ¸¸µé¾ú´Ù.
½ÇÁ¦ ÆäÀÌÁö¿¡¼´Â Àбâ ÆäÀÌÁö¿Í ¼öÁ¤ ÆäÀÌÁö°¡ º°µµ·Î ºÐ¸®µÇ¾î º¼ ¼ö Àִµ¥ À̹øÀº °Ô½ÃÆÇ ÀÛ¼ºÀÇ È帧À» À§Çؼ ÇÕÄ£ °Í°°´Ù.
ºÐ¸®ÇÏ·Á¸é ¼öÁ¤.jsp ÆÄÀÏ ¸¸µé¾î¼ ¿¬°á½ÃÄÑÁÖ¸é µÉµí
<h1>´ñ±Û¾²±â</h1>
<form action="reply.do" method="post">
<table>
<colgroup>
<col width="20%">
<col width="*">
</colgroup>
<tr class="hidden">
<td colspan="2">
<input type="hidden" name="bId" value="${reply_view.bId }"/>
<input type="hidden" name="bGroup" value="${reply_view.bGroup }"/>
<input type="hidden" name="bStep" value="${reply_view.bStep }"/>
<input type="hidden" name="bIndent" value="${reply_view.bIndent }"/>
</td>
</tr>
<tr>
<th>¹øÈ£</th>
<td>${reply_view.bId }</td>
</tr>
<tr>
<th>È÷Æ®</th>
<td>${reply_view.bHit }</td>
</tr>
<tr>
<th>°Ô½ÃÀÚ</th>
<td><input type="text" name="mId" value=""/></td>
</tr>
<tr>
<th>Á¦¸ñ</th>
<td><input class="w90" type="text" name="bTitle" value="[´ä±Û] ${reply_view.bTitle}"/></td>
</tr>
<tr>
<th>³»¿ë</th>
<td>
<textarea name="bContent" > ¡æ ${reply_view.bContent }¿¡ ´ëÇÑ ´ä±Û</textarea>
</td>
</tr>
<tr>
<td colspan="2" class="center">
<input type="submit" value="´äº¯Çϱâ"/>
<a href="list.do">¸ñ·Ï</a>
</td>
</tr>
</table>
</form>
±Û¾²±â ÆÄÀÏ°ú ºñ½ÁÇÏ°Ô ¸¸µé¸é µÈ´Ù.
´ë½Å bGroup, bStep, bIndentÀº º¸¿©ÁÙ ÇÊ¿ä°¡ ¾ø¾î¼ °¨ÃçÁÖ±â
10. ¸¸µç °ÍµéÀ» Á¦¾îÇÒ MVCBoardController.java ¼ºí¸´ ÆÄÀÏÀ» ¸¸µé¾îÁØ´Ù.
@WebServlet(description = "À¥À»ÄÁÆ®·ÑÇÏ´Â ¼ºí¸´", urlPatterns = { "*.do" })
public class MVCBoardController extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public MVCBoardController() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
actionDo(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
actionDo(request, response);
}
private void actionDo(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
// ÁÖÀÇ
String uri = request.getRequestURI(); // ÇÁ·ÎÅäÄÝ+µµ¸ÞÀÎ+±âŸ
String contextPath = request.getContextPath();// ÇÁ·ÎÅäÄÝ+µµ¸ÞÀÎ
String command = uri.substring(contextPath.length());// xxxxxx.do
BCommand bcommand = null;
String viewPage = null;
if (command.equals("/write.do")) {
bcommand = new BWriteCommand();
bcommand.execute(request, response);
viewPage = "list.do";
} else if (command.equals("/list.do")) {
bcommand = new BListCommand();
bcommand.execute(request, response);
viewPage = "list.jsp";
} else if (command.equals("/content_view.do")) {
bcommand = new BContentCommand();
bcommand.execute(request, response);
viewPage = "content_view.jsp";
} else if (command.equals("/delete.do")) {
bcommand = new BDeleteCommand();
bcommand.execute(request, response);
viewPage = "list.do";
} else if (command.equals("/reply_view.do")) {
bcommand = new BReplyViewCommand();
bcommand.execute(request, response);
viewPage = "reply_view.jsp";
}else if (command.equals("/reply.do")) {
bcommand = new BReplyCommand();
bcommand.execute(request, response);
viewPage = "list.do";
}else if (command.equals("/modify.do")) {
bcommand = new BModifyCommand();
bcommand.execute(request, response);
viewPage = "list.do";
}
// ±³Åë¼ø°æ(¹èÂ÷°è)
RequestDispatcher dispatcher = request.getRequestDispatcher(viewPage);
dispatcher.forward(request, response);
System.out.println("°³¹ßÁß ÀÏ´Ü ÀÛ¾÷Á¾·á!");
}
}
actionDo¸¦ ÀÌ¿ëÇØ get°ú post µÑ´Ù °°Àº °÷À¸·Î È£ÃâÇÑ´Ù.
command¸¦ ÀÌ¿ëÇÏ¿© °¢°¢ ÇØ´ç ÆäÀÌÁöÀÇ ÀÛ¾÷À» ¼öÇàÇÒ Å¬·¡½º¿¡°Ô ³»¿ëÀ» ÇÒ´çÇÑ´Ù.
request.getRequestURI() ÇÔ¼ö = ÇÁ·ÎÁ§Æ® + ÆÄÀÏ°æ·Î
ex) http://localhost:8000/jsp08_mvctest/list.jsp
[return] /jsp08_mvctest/list.jsp
request.getContextPath() ÇÔ¼ö = ÇÁ·ÎÁ§Æ® Path¸¸ °¡Á®¿Â´Ù
ex) http://localhost:8000/jsp08_mvctest/list.jsp
[return] /jsp08_mvctest
request.getRequestURL() ÇÔ¼ö = Àüü°æÈ£
ex) http://localhost:8000/jsp08_mvctest/list.jsp
[return] http://localhost:8000/jsp08_mvctest/list.jsp
String.substring(start, length) = ¹®ÀÚ¿À» ÃßÃâÇÏ´Â ¸Þ¼µå
¹®ÀÚ¿¿¡¼ ƯÁ¤ ºÎºÐ¸¸ °ñ¶ó³¾¶§ »ç¿ëÇÏ´Â ¸Þ¼µå
start+length À§Ä¡±îÁö¸¦ Ž»ö±¸°£À¸·Î ¼³Á¤ÇÔ
RequestDispatcher
ƯÁ¤ ÀÚ¿ø¿¡ 󸮸¦ ¿äûÇÏ°í ó¸® °á°ú¸¦ ´Ù¸¥ ÀÚ¿ø¿¡ º¸³»´Â ¿ªÇÒÀ» ÇÏ´Â ÀÎÅÍÆäÀ̽º
request¿¡ ´ã±ä Á¤º¸¸¦ ÀúÀåÇÏ°í ÀÖ´Ù°¡ ±× ´ÙÀ½ ÆäÀÌÁö¿¡µµ °è½ï ÇØ´ç Á¤º¸¸¦ º¼ ¼ö ÀÖ°Ô °è¼Ó ÀúÀåÇÏ´Â ±â´É
ex) a.jsp¿¡ ´ã±ä paramÀ̶ó´Â ÆĶó¹ÌÅ͸¦ b.jsp / c.jsp ¿¡¼µµ ¹Þ¾Æ º¼ ¼ö ÀÖ´Â °Í
*getRequestDispatcher("À̵¿À§Ä¡") ÇÏÁö¸¸ viewPage¶ó´Â º¯¼ö¸¦ ÀÌ¿ëÇؼ ¼Ò½º¸¦ ±ò²ûÇÏ°Ô Á¤¸®
forward()
´Ù¸¥ ÀÚ¿øÀÇ ¼öÇà °á°ú°¡ ´ë½Å Ŭ¶óÀ̾ðÆ®·Î ÀÀ´äµÇµµ·Ï ÇÏ´Â ±â´ÉÀÇ ¸Þ¼Òµå
Áï command¿¡¼ ½ÇÇàÇÑ ÀÛ¾÷À» ÀúÀåÇؼ ÁöÁ¤ÇÑ ÆäÀÌÁö·Î °ªÀ» º¼ ¼ö ÀÖ°Ô ÇØÁØ´Ù.
11. command¸¦ ¿¬°áÇÒ ÀÎÅÍÆäÀ̽º BCommand.java¸¦ ¸¸µé¾îÁØ´Ù.
public interface BCommand {
public void execute(HttpServletRequest request, HttpServletResponse response);
}
12. ½ÇÁ¦ ÀÛ¾÷À» ÇØÁÙ command Ŭ·¡½ºµéÀ» ¸¸µé¾î ÁØ´Ù
public class BWriteCommand implements BCommand {
@Override
public void execute(HttpServletRequest request, HttpServletResponse response) {
String mId = request.getParameter("mId");
String bTitle = request.getParameter("bTitle");
String bContent = request.getParameter("bContent");
// ¸¶À̹ÙƼ½º°¡ ´Ù¿À ºÎ¸®´Â ÀÛ¾÷
SqlSession mysession = MBUtils.getSession();
MvcBoardDAO dao = mysession.getMapper(MvcBoardDAO.class);
MvcBoard bean = new MvcBoard();
bean.setmId(mId);
bean.setbTitle(bTitle);
bean.setbContent(bContent);
dao.write(bean);
mysession.close();
}
}
public class BReplyViewCommand implements BCommand {
@Override
public void execute(HttpServletRequest request, HttpServletResponse response) {
long bId = Long.parseLong(request.getParameter("bId"));
SqlSession mysession = MBUtils.getSession();
MvcBoardDAO dao = mysession.getMapper(MvcBoardDAO.class);
dao.upHit(bId);
MvcBoard bean = dao.reply_view(bId);
request.setAttribute("reply_view", bean);
mysession.close();
}
}
public class BReplyCommand implements BCommand {
@Override
public void execute(HttpServletRequest request, HttpServletResponse response) {
String mId = request.getParameter("mId");
String bTitle = request.getParameter("bTitle");
String bContent = request.getParameter("bContent");
int bGroup = Integer.parseInt(request.getParameter("bGroup"));
int bStep = Integer.parseInt(request.getParameter("bStep"));
int bIndent = Integer.parseInt(request.getParameter("bIndent"));
SqlSession mysession = MBUtils.getSession();
MvcBoardDAO dao = mysession.getMapper(MvcBoardDAO.class);
HashMap<String, Integer> map = new HashMap<>();
map.put("bGroup", new Integer(bGroup));
map.put("bStep", new Integer(bStep));
dao.replyShape(map);
MvcBoard bean = new MvcBoard();
bean.setmId(mId);
bean.setbTitle(bTitle);
bean.setbContent(bContent);
bean.setbGroup(bGroup);
bean.setbStep(bStep);
bean.setbIndent(bIndent);
dao.reply(bean);
}
}
public class BModifyCommand implements BCommand {
@Override
public void execute(HttpServletRequest request, HttpServletResponse response) {
//¼öÁ¤ÇÒ ±Û ¾ÆÀ̵𸦠°¡Á®¿À´Â ÀÛ¾÷
long bId = Long.parseLong(request.getParameter("bId"));
//¼öÁ¤ÇÒ ³»¿ëÀ» °¡Á®¿À´Â ÀÛ¾÷
String bTitle = request.getParameter("bTitle");
String bContent = request.getParameter("bContent");
// ¸¶À̹ÙƼ½º°¡ ´Ù¿À ºÎ¸®´Â ÀÛ¾÷
SqlSession mysession = MBUtils.getSession();
MvcBoardDAO dao = mysession.getMapper(MvcBoardDAO.class);
MvcBoard bean = new MvcBoard();
bean.setbId(bId);
bean.setbTitle(bTitle);
bean.setbContent(bContent);
dao.modify(bean);
mysession.close();
}
}
public class BListCommand implements BCommand {
@Override
public void execute(HttpServletRequest request, HttpServletResponse response) {
SqlSession mysession = MBUtils.getSession();
MvcBoardDAO dao = mysession.getMapper(MvcBoardDAO.class);
List<MvcBoard> beans = dao.list();
request.setAttribute("list", beans);
mysession.close();
}
}
public class BDeleteCommand implements BCommand {
@Override
public void execute(HttpServletRequest request, HttpServletResponse response) {
long bId = Long.parseLong(request.getParameter("bId"));
SqlSession mysession = MBUtils.getSession();
MvcBoardDAO dao = mysession.getMapper(MvcBoardDAO.class);
dao.delete(bId);
mysession.close();
}
}
public class BContentCommand implements BCommand {
@Override
public void execute(HttpServletRequest request, HttpServletResponse response) {
// ³Ñ°Ü¹Þ´Â °ªÀº ¸ðµÎ ¹®ÀÚ¿ À̹ǷΠ°ªÀ» ¼ýÀÚ·Î ¹Ù²ãÁØ´Ù.
long bId = Long.parseLong(request.getParameter("bId"));
SqlSession mysession = MBUtils.getSession();
MvcBoardDAO dao = mysession.getMapper(MvcBoardDAO.class);
dao.upHit(bId);
MvcBoard bean = dao.contentView(bId);
request.setAttribute("content_view", bean);
mysession.close();
}
}
13. ÇØ´ç jsp ÆÄÀÏÀ» ¼¹ö¿¡¼ µ¹¸®¸é ÀÌ·± °á°úµéÀÌ ³ª¿Â´Ù.