Model 2
Model 1 is a program structure that uses Java classes in JSP. Model 2 is a structure in which a controller is added to Model 1. The controller acts as a bridge between views (JSPs) and model (Java classes) responsible for business logic. The process of Model 2 is summarized as follows.
- The controller receives all requests.
- The controller delegates all tasks to models. The model is responsible for business logic. When the controller delegates tasks, it passes the request object to the model.
- The model completes the task and passes the results to the controller. The model does this by storing the results in the request object.
- The controller passes the result to JSP, which eventually serves the client's request. The controller uses the forward() method to perform this task.
The following servlet gives us hints about controllers.
ControllerServlet.java
package net.java_school.board; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class ControllerServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String uri = req.getRequestURI(); String contextPath = req.getContextPath(); String command = null; command = uri.substring(contextPath.length()); PrintWriter out = resp.getWriter(); out.println(command); out.close(); } }
Add the following to web.xml:
<servlet> <servlet-name>Controller</servlet-name> <servlet-class>net.java_school.board.ControllerServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Controller</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
Restart Tomcat. Visit the following addresses:
- http://localhost:8080/list.do
- http://localhost:8080/board/list.do
- http://localhost:8080/board/view.do
- http://localhost:8080/board/write_form.do
- http://localhost:8080/board/modify_form.do
You can see that the request string is printed on the web browser screen. Unlike before, the request string does not represent a specific component of the server. The controller will respond to the user's request by determining the model and view according to the request string.
Create Models
When you create a model class, let the class name end with Action. (This is the naming convention used by the Struts 2 framework) From now on, We will modify the model 1 bulletin board to the model 2 bulletin board. Let's change the list page first. For a list, the model should provide:
- The list item number on the list page
- The result set to be displayed on the list page
- The page number to [<] link
- The page number to [>] link
- The first page number
- The last page number
The execute() method of action classes returns the data that the action should provide. (This is also the naming convention used in Struts 2)
ListAction.java
package net.java_school.board; import javax.servlet.http.HttpServletRequest; public class ListAction { public void execute(HttpServletRequest request) { //TODO } }
Complete the execute() method as follows:
ListAction.java
package net.java_school.board; import java.io.UnsupportedEncodingException; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; public class ListAction { public void execute(HttpServletRequest request) throws UnsupportedEncodingException { request.setCharacterEncoding("UTF-8"); int curPage = request.getParameter("curPage") == null ? 1 : Integer.parseInt(request.getParameter("curPage")); String keyword = request.getParameter("keyword"); if (keyword == null) keyword = ""; BoardService service = new BoardService(); int totalRecord = service.getTotalRecord(keyword); Map<String, Integer> numbers = service.getNumbersForPaging(totalRecord, curPage, 10, 5); int startRecord = numbers.get("startRecord"); int endRecord = numbers.get("endRecord"); List<Article> list = service.getBoardList(startRecord, endRecord, keyword); int listItemNo = numbers.get("listItemNo"); int prevPage = numbers.get("prevPage"); int nextPage = numbers.get("nextPage"); int firstPage = numbers.get("firstPage"); int lastPage = numbers.get("lastPage"); request.setAttribute("list", list); request.setAttribute("listItemNo", listItemNo); request.setAttribute("prevPage", prevPage); request.setAttribute("nextPage", nextPage); request.setAttribute("firstPage", firstPage); request.setAttribute("lastPage", lastPage); } }
Let's modify the controller to perform the following code:
request.setCharacterEncoding ("UTF-8");
ControllerServlet.java
package net.java_school.board; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class ControllerServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); String uri = req.getRequestURI(); String contextPath = req.getContextPath(); String command = null; command = uri.substring(contextPath.length()); PrintWriter out = resp.getWriter(); out.println(command); out.close(); } }
ListAction.java
package net.java_school.board; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; public class ListAction { public void execute(HttpServletRequest request) { int curPage = request.getParameter("curPage") == null ? 1 : Integer.parseInt(request.getParameter("curPage")); String keyword = request.getParameter("keyword"); if (keyword == null) keyword = ""; BoardService service = new BoardService(); int totalRecord = service.getTotalRecord(keyword); Map<String, Integer> numbers = service.getNumbersForPaging(totalRecord, curPage, 10, 5); int startRecord = numbers.get("startRecord"); int endRecord = numbers.get("endRecord"); List<Article> list = service.getBoardList(startRecord, endRecord, keyword); int listItemNo = numbers.get("listItemNo"); int prevPage = numbers.get("prevPage"); int nextPage = numbers.get("nextPage"); int firstPage = numbers.get("firstPage"); int lastPage = numbers.get("lastPage"); request.setAttribute("list", list); request.setAttribute("listItemNo", listItemNo); request.setAttribute("prevPage", prevPage); request.setAttribute("nextPage", nextPage); request.setAttribute("firstPage", firstPage); request.setAttribute("lastPage", lastPage); } }
Let's add code to the controller for the list request.
ControllerServlet.java
package net.java_school.board; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class ControllerServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); String uri = req.getRequestURI(); String contextPath = req.getContextPath(); String command = null; command = uri.substring(contextPath.length()); String view = null; //JSP boolean isRedirect = false; //If false, forwarding if (command.equals("/board/list.do")) { ListAction listAction = new ListAction(); listAction.execute(req); view = "/board/list.jsp"; } if (isRedirect == false) { ServletContext sc = this.getServletContext(); RequestDispatcher rd = sc.getRequestDispatcher(view); rd.forward(req, resp); } else { resp.sendRedirect(view); } } }
Modify the list page as shown below.
list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="net.java_school.board.*" %> <%@ page import="java.util.*" %> <% String curPage = request.getParameter("curPage"); if (curPage == null) curPage = "1"; String keyword = request.getParameter("keyword"); if (keyword == null) keyword = ""; int listItemNo = (Integer) request.getAttribute("listItemNo"); List<Article> list = (List<Article>) request.getAttribute("list"); int prevPage = (Integer) request.getAttribute("prevPage"); int nextPage = (Integer) request.getAttribute("nextPage"); int firstPage = (Integer) request.getAttribute("firstPage"); int lastPage = (Integer) request.getAttribute("lastPage"); %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>List</title> </head> <body style="font-size: 11px;"> <h1>List</h1> <% for (int i = 0; i < list.size(); i++) { Article article = list.get(i); int indent = article.getIndent(); for (int j = 0; j < indent; j++) { out.println(" "); } if(indent != 1) { out.println("↳"); } %> <%=listItemNo %> <a href="view.do?no=<%=article.getNo() %>&curPage=<%=curPage %>&keyword=<%=keyword %>"><%=article.getTitle() %></a> <%=article.getWdate() %><br /> <hr /> <% listItemNo--; } if (prevPage != 0) { %> <a href="list.do?curPage=<%=prevPage %>&keyword=<%=keyword %>">[<]</a> <% } for (int i = firstPage; i <= lastPage; i++) { %> <a href="list.do?curPage=<%=i %>&keyword=<%=keyword %>">[<%=i %>]</a> <% } if (nextPage != 0) { %> <a href="list.do?curPage=<%=nextPage %>&keyword=<%=keyword %>">[>]</a> <% } %> <p> <a href="write_form.do?curPage=<%=curPage %>&keyword=<%=keyword %>">New</a> </p> <form method="get"> <input type="text" size="10" maxlength="30" name="keyword" /> <input type="submit" value="Search" /> </form> </body> </html>
Go to http://localhost:8080/board/list.do and test. The list page no longer produces the numbers needed for paging and doesn't even do anything with the database. Instead, it only receives and displays the results produced by the model. But you won't like the next part of the code.
int listItemNo = (Integer) request.getAttribute("listItemNo"); List<Article> list = (List<Article>) request.getAttribute("list"); int prevPage = (Integer) request.getAttribute("prevPage"); int nextPage = (Integer) request.getAttribute("nextPage"); int firstPage = (Integer) request.getAttribute("firstPage"); int lastPage = (Integer) request.getAttribute("lastPage");
The problem with the above code is the tricky conversion. You can avoid this by using the Value Object pattern.
ListVo.java
package net.java_school.board; import java.util.List; public class ListVo { private List<Article> list; private int totalPage; private int listItemNo; private int firstPage; private int lastPage; private int prevPage; private int nextPage; private int startRecord; private int endRecord; public List<Article> getList() { return list; } public void setList(List<Article> list) { this.list = list; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public int getListItemNo() { return listItemNo; } public void setListItemNo(int listItemNo) { this.listItemNo = listItemNo; } public int getPrevPage() { return prevPage; } public void setPrevPage(int prevPage) { this.prevPage = prevPage; } public int getFirstPage() { return firstPage; } public void setFirstPage(int firstPage) { this.firstPage = firstPage; } public int getLastPage() { return lastPage; } public void setLastPage(int lastPage) { this.lastPage = lastPage; } public int getNextPage() { return nextPage; } public void setNextPage(int nextPage) { this.nextPage = nextPage; } public int getStartRecord() { return startRecord; } public void setStartRecord(int startRecord) { this.startRecord = startRecord; } public int getEndRecord() { return endRecord; } public void setEndRecord(int endRecord) { this.endRecord = endRecord; } }
Modify the BoardService and ListAction to use the Value Object.
BoardService.java
package net.java_school.board; import java.util.List; public class BoardService { private BoardDao dao = new BoardDao(); public BoardService() {} public ListVo getNumbersForPaging(int totalRecord, int curPage, int numPerPage, int pagePerBlock) { int totalPage = totalRecord / numPerPage; if (totalRecord % numPerPage != 0) totalPage++; int totalBlock = totalPage / pagePerBlock; if (totalPage % pagePerBlock != 0) totalBlock++; int block = curPage / pagePerBlock; if (curPage % pagePerBlock != 0) block++; int firstPage = (block - 1) * pagePerBlock + 1; int lastPage = block * pagePerBlock; int prevPage = 0; if (block > 1) { prevPage = firstPage - 1; } int nextPage = 0; if (block < totalBlock) { nextPage = lastPage + 1; } if (block >= totalBlock) { lastPage = totalPage; } int listItemNo = totalRecord - (curPage - 1) * numPerPage; int startRecord = (curPage - 1) * numPerPage + 1; int endRecord = curPage * numPerPage; ListVo listVo = new ListVo(); listVo.setTotalPage(totalPage); listVo.setFirstPage(firstPage); listVo.setLastPage(lastPage); listVo.setPrevPage(prevPage); listVo.setNextPage(nextPage); listVo.setListItemNo(listItemNo); listVo.setStartRecord(startRecord); listVo.setEndRecord(endRecord); return listVo; } public List<Article> getBoardList(int startRecord, int endRecord, String keyword) { return dao.getBoardList(startRecord, endRecord, keyword); } public int getTotalRecord(String keyword) { return dao.getTotalRecord(keyword); } public void write(Article article) { dao.insert(article); } public Article getArticle(int no) { return dao.selectOne(no); } public void modify(Article article) { dao.update(article); } public void delete(int no) { dao.delete(no); } public void reply(Article article) { dao.reply(article); } }
ListAction.java
package net.java_school.board; import java.util.List; import javax.servlet.http.HttpServletRequest; public class ListAction { public void execute(HttpServletRequest request) { int curPage = request.getParameter("curPage") == null ? 1 : Integer.parseInt(request.getParameter("curPage")); String keyword = request.getParameter("keyword"); if (keyword == null) keyword = ""; BoardService service = new BoardService(); int totalRecord = service.getTotalRecord(keyword); ListVo vo = service.getNumbersForPaging(totalRecord, curPage, 10, 5); int startRecord = vo.getStartRecord(); int endRecord = vo.getEndRecord(); List<Article> list = service.getBoardList(startRecord, endRecord, keyword); vo.setList(list); request.setAttribute("listVo", vo); } }
Modify the list page file as follows:
list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="net.java_school.board.*" %> <%@ page import="java.util.*" %> <% String curPage = request.getParameter("curPage"); if (curPage == null) curPage = "1"; String keyword = request.getParameter("keyword"); if (keyword == null) keyword = ""; ListVo vo = (ListVo) request.getAttribute("listVo"); %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>List</title> </head> <body style="font-size: 11px;"> <h1>List</h1> <% int listItemNo = vo.getListItemNo(); List<Article> list = vo.getList(); for (int i = 0; i < list.size(); i++) { Article article = list.get(i); int indent = article.getIndent(); for (int j = 0; j < indent; j++) { out.println(" "); } if(indent != 1) { out.println("↳"); } %> <%=listItemNo %> <a href="view.do?no=<%=article.getNo() %>&curPage=<%=curPage %>&keyword=<%=keyword %>"><%=article.getTitle() %></a> <%=article.getWdate() %><br /> <hr /> <% listItemNo--; } int prevPage = vo.getPrevPage(); if (prevPage != 0) { %> <a href="list.do?curPage=<%=prevPage %>&keyword=<%=keyword %>">[<]</a> <% } int firstPage = vo.getFirstPage(); int lastPage = vo.getLastPage(); for (int i = firstPage; i <= lastPage; i++) { %> <a href="list.do?curPage=<%=i %>&keyword=<%=keyword %>">[<%=i %>]</a> <% } int nextPage = vo.getNextPage(); if (nextPage != 0) { %> <a href="list.do?curPage=<%=nextPage %>&keyword=<%=keyword %>">[>]</a> <% } %> <p> <a href="write_form.do?curPage=<%=curPage %>&keyword=<%=keyword %>">New</a> </p> <form action="list.do" method="post"> <input type="text" size="10" maxlength="30" name="keyword" /> <input type="submit" value="Search" /> </form> </body> </html>
More action classes are needed. So when you create an action interface and apply it to an action, the controller's code becomes elegant.
Action.java
package net.java_school.board; import javax.servlet.http.HttpServletRequest; public interface Action { public void execute(HttpServletRequest request); }
Modify the ListAction to implement the action interface.
ListAction.java
package net.java_school.board; import java.util.List; import javax.servlet.http.HttpServletRequest; public class ListAction implements Action { @Override public void execute(HttpServletRequest request) { //.. The code below is omitted because it is the same as before.. } }
Modify the controller as follows.
ControllerServlet.java
package net.java_school.board; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class ControllerServlet extends HttpServlet { private ServletContext sc; @Override public void init() throws ServletException { sc = this.getServletContext(); } @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } @Override public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); String uri = req.getRequestURI(); String contextPath = req.getContextPath(); String command = null; command = uri.substring(contextPath.length()); String view = null; //JSP Action action = null; boolean isRedirect = false; //if fasle, forwarding. if (command.equals("/board/list.do")) { action = new ListAction(); action.execute(req); view = "/board/list.jsp"; } else if (command.equals("/board/write_form.do")) { //TODO 1 } else if (command.equals("/board/write.do")) { //TODO 2 } else if (command.equals("/board/view.do")) { //TODO 3 } else if (command.equals("/board/modify_form.do")) { //TODO 4 } else if (command.equals("/board/modify.do")) { //TODO 5 } else if (command.equals("/board/del.do")) { //TODO 6 } else if (command.equals("/board/reply_form.do")) { //TODO 7 } else if (command.equals("/board/reply.do")) { //TODO 8 } if (isRedirect == false) { RequestDispatcher rd = sc.getRequestDispatcher(view); rd.forward(req, resp); } else { resp.sendRedirect(view); } } }
//TODO 1
view = "/board/write_form.jsp";
No action is required as there is no data to pass to write_form.jsp. Open the write_form.jsp file and change the .jsp in your code to .do. Visit the list and click New to see if you can go to write_form.jsp.
//TODO 2
action = new WriteAction(); action.execute(req); view = "/board/list.do"; isRedirect = true;
WriteAction.java should be created. This model is responsible for the logic that processes the writing. You can create a WriteAction referring to the write.jsp file of Model 1 as follows.
WriteAction.java
package net.java_school.board; import javax.servlet.http.HttpServletRequest; public class WriteAction implements Action{ @Override public void execute(HttpServletRequest request) { String title = request.getParameter("title"); String content = request.getParameter("content"); Article article = new Article(); article.setTitle(title); article.setContent(content); BoardService service= new BoardService(); service.write(article); } }
Test new posting. If the test succeeds, the write.jsp file is not needed, so delete it.
//TODO 3
action = new ViewAction(); action.execute(req); view = "/board/view.jsp";
ViewAction should handle the detailed view logic.
ViewAction.java
package net.java_school.board; import javax.servlet.http.HttpServletRequest; public class ViewAction implements Action { @Override public void execute(HttpServletRequest request) { int no = Integer.parseInt(request.getParameter("no")); BoardService service = new BoardService(); Article article = service.getArticle(no); request.setAttribute("article", article); } }
Modify the detailed view page to show the data that the model has preprocessed and delivered.
view.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="net.java_school.board.*" %> <% int no = Integer.parseInt(request.getParameter("no")); String curPage = request.getParameter("curPage"); String keyword = request.getParameter("keyword"); if (keyword == null) keyword = ""; Article article = (Article) request.getAttribute("article"); %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>View</title> <script type="text/javascript"> function goModify(no,curPage,keyword) { location.href="modify_form.do?no=" + no + "&curPage=" + curPage + "&keyword=" + keyword; } function goDelete(no,curPage,keyword) { var check = confirm("Are you sure you want to delete it?"); if (check) { location.href="del.do?no=" + no + "&curPage=" + curPage + "&keyword=" + keyword; } } </script> </head> <body> <h1>View</h1> <h2>Title: <%=article.getTitle() %>, Date created: <%=article.getWdate() %></h2> <p> <%=article.getHtmlContent() %> </p> <a href="list.do?curPage=<%=curPage %>&keyword=<%=keyword %>">List</a> <input type="button" value="Edit Post" onclick="javascript:goModify('<%=no %>','<%=curPage %>','<%=keyword %>')"> <input type="button" value="Delete" onclick="javascript:goDelete('<%=no %>','<%=curPage %>','<%=keyword %>')"> <a href="reply_form.do?no=<%=no %>&curPage=<%=curPage %>&keyword=<%=keyword %>">Reply</a> </body> </html>
//TODO 4
action = new ModifyFormAction(); action.execute(req); view = "/board/modify_form.jsp";
Create a ModifyFormAction as shown below.
ModifyFormAction.java
package net.java_school.board; import javax.servlet.http.HttpServletRequest; public class ModifyFormAction implements Action { @Override public void execute(HttpServletRequest request) { int no = Integer.parseInt(request.getParameter("no")); BoardService service = new BoardService(); Article article = service.getArticle(no); request.setAttribute("article", article); } }
Modify the modify_form.jsp file as follows.
modify_form.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.sql.*" %> <%@ page import="net.java_school.board.*" %> <% int no = Integer.parseInt(request.getParameter("no")); String curPage = request.getParameter("curPage"); String keyword = request.getParameter("keyword"); Article article = (Article) request.getAttribute("article"); %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Edit Post</title> </head> <body> <h1>Edit Post</h1> <form action="modify.do" method="post"> <input type="hidden" name="no" value="<%=no %>"> <input type="hidden" name="curPage" value="<%=curPage %>"> <input type="hidden" name="keyword" value="<%=keyword %>"> <table> <tr> <td>Title</td> <td><input type="text" name="title" size="50" value="<%=article.getTitle() %>" /></td> </tr> <tr> <td colspan="2"> <textarea name="content" rows="30" cols="100"><%=article.getContent() %></textarea> </td> </tr> <tr> <td colspan="2"> <input type="submit" value="Submit"> <input type="reset" value="Reset"> <a href="view.do?no=<%=no %>&curPage=<%=curPage %>&keyword=<%=keyword %>">View</a> </td> </tr> </table> </form> </body> </html>
//TODO 5
action = new ModifyAction(); action.execute(req); String no = req.getParameter("no"); String curPage = req.getParameter("curPage"); String keyword = req.getParameter("keyword"); if (keyword == null) keyword = ""; keyword = java.net.URLEncoder.encode(keyword, "UTF-8"); view = "/board/view.do?no=" + no + "&curPage=" + curPage + "&keyword=" + keyword; isRedirect = true;
Create a ModifyAction as follows.
ModifyAction.java
package net.java_school.board; import javax.servlet.http.HttpServletRequest; public class ModifyAction implements Action { @Override public void execute(HttpServletRequest request) { int no = Integer.parseInt(request.getParameter("no")); String title = request.getParameter("title"); String content = request.getParameter("content"); Article article = new Article(); article.setNo(no); article.setTitle(title); article.setContent(content); BoardService service= new BoardService(); service.modify(article); } }
The modify.jsp file is needed no longer, so delete it.
//TODO 6
action = new DeleteAction(); action.execute(req); String curPage = req.getParameter("curPage"); String keyword = req.getParameter("keyword"); if (keyword == null) keyword = ""; keyword = java.net.URLEncoder.encode(keyword, "UTF-8"); view = "/board/list.do?curPage=" + curPage + "&keyword=" + keyword; isRedirect = true;
Create a DeleteAction as follows.
DeleteAction.java
package net.java_school.board; import javax.servlet.http.HttpServletRequest; public class DeleteAction implements Action { @Override public void execute(HttpServletRequest request) { int no = Integer.parseInt(request.getParameter("no")); BoardService service= new BoardService(); service.delete(no); } }
The del.jsp file is needed no longer, so delete it.
//TODO 7
action = new ReplyFormAction(); action.execute(req); view = "/board/reply_form.jsp";
Create a ReplyFormAction as follows.
ReplyFormAction.java
package net.java_school.board; import javax.servlet.http.HttpServletRequest; import net.java_school.commons.WebContants; public class ReplyFormAction implements Action { @Override public void execute(HttpServletRequest request) { int no = Integer.parseInt(request.getParameter("no")); BoardService service = new BoardService(); Article article = service.getArticle(no); String content = article.getContent(); //Add > for each line of the parent content. content = content.replaceAll(WebContants.lineSeparator.value , WebContants.lineSeparator.value + ">"); content = WebContants.lineSeparator.value + WebContants.lineSeparator.value + ">" + content; article.setContent(content); request.setAttribute("article", article); } }
Modify the reply_form.jsp file as follows.
reply_form.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="net.java_school.board.*" %> <% int no = Integer.parseInt(request.getParameter("no")); String curPage = request.getParameter("curPage"); String keyword = request.getParameter("keyword"); Article article = (Article) request.getAttribute("article"); %> <html> <head> </head> <body> <h1>Reply</h1> <form action="reply.do" method="post"> <input type="hidden" name="no" value="<%=no %>" /> <input type="hidden" name="curPage" value="<%=curPage %>" /> <input type="hidden" name="keyword" value="<%=keyword %>" /> Title : <input type="text" name="title" size="45" value="<%=article.getTitle() %>" /><br /> <textarea name="content" rows="10" cols="60"><%=article.getContent() %></textarea><br /> <input type="submit" value="Submit" /> <input type="reset" value="Reset" /><br /> </form> <a href="view.do?no=<%=no %>&curPage=<%=curPage %>&keyword=<%=keyword %>">View</a> </body> </html>
//TODO 8
action = new ReplyAction(); action.execute(req); String curPage = req.getParameter("curPage"); String keyword = req.getParameter("keyword"); if (keyword == null) keyword = ""; keyword = java.net.URLEncoder.encode(keyword, "UTF-8"); view = "/board/list.do?curPage=" + curPage + "&keyword=" + keyword; isRedirect = true;
Create a ReplyAction as follows.
ReplyAction.java
package net.java_school.board; import javax.servlet.http.HttpServletRequest; public class ReplyAction implements Action { @Override public void execute(HttpServletRequest request) { int parent = Integer.parseInt(request.getParameter("no")); String title = request.getParameter("title"); String content = request.getParameter("content"); Article article = new Article(); article.setParent(parent); article.setTitle(title); article.setContent(content); BoardService service= new BoardService(); service.reply(article); } }
The reply.jsp file is needed no longer, so delete it.