-
MVC : MVC의 기본작용방법Spring 2021. 6. 18. 22:29
먼저 tagdemo에서 사용했던 jstl을 사용하기 위해서 lib안의 내용을 복사해서 servletdemo에 붙여주자.
그다음 controller servlet을 만들자.
doget 을 다음과 같이 오버라이딩 해준다.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //Step 0: Add data String[] students = {"Susan", "Anil", "Trupti"}; request.setAttribute("student_list", students); //Step 1: get request dispatcher RequestDispatcher dispatcher = request.getRequestDispatcher("/view_students.jsp"); //Step 2: forward the request to JSP dispatcher.forward(request, response); }
view_student.jsp파일을 생성해준다.
<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix="c" %> <html> <body> <c:forEach var="tempStudent" items = "${student_list }"> ${tempStudent }<br/> </c:forEach> </body> </html>
여태까지는 servlet에서 학생 리스트(model)을 만들어서 했는데,
이번에는 model을 따로 작성해서 학생object를 따로 정리하도록 하자.
먼저 src안에 com.yunhalee.servletdemo.mvctwo라는 패키지를 만들고 Student클래스를 만들어준다.
package com.yunhalee.servletdemo.mvctwo; public class Student { private String firstName; private String lastName; private String email; public Student(String firstName, String lastName, String email) { super(); this.firstName = firstName; this.lastName = lastName; this.email = email; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
이제 StudentDataUtil 자바클래스를 만들어보자.
이건, model에 access하도록 도와주는 클래스다.
package com.yunhalee.servletdemo.mvctwo; import java.util.ArrayList; import java.util.List; public class StudentDataUtil { public static List<Student> getStudents(){ //create an empty list List<Student> students = new ArrayList<>(); //add sample data students.add(new Student("Mary", "Public", "mary@luv2code.com")); students.add(new Student("John", "Doe", "john@luv2code.com")); students.add(new Student("Ajay", "Rao", "ajay@luv2code.com")); //return the list return students; } }
이제 MVC servlet을 만들어주자.
doget을 오버라이딩 해준다.
(MvcDemoServletTwo)
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //Step 1: get the student data from helper class(model) List<Student> students = StudentDataUtil.getStudents(); //Step 2: add students to request object request.setAttribute("student_list", students); //Step 3: get request dispatcher RequestDispatcher dispatcher = request.getRequestDispatcher("view_students_two.jsp"); //Step 4: now forward to JSP dispatcher.forward(request, response); }
view_students_two.jsp
<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix="c" %> <html> <body> <h2>Student Table Demo</h2> <hr/> <br/> <table border="1"> <tr> <th>First Name</th> <th>Last Name</th> <th>Email</th> </tr> <c:forEach var="tempStudent" items = "${student_list }"> <tr> <td>${tempStudent.firstName }</td> <td>${tempStudent.lastName }</td> <td>${tempStudent.email }</td> </tr> </c:forEach> </table> </body> </html>
'Spring' 카테고리의 다른 글
Database App with JDBC : 학생을 추가하는 기능 만들기 (Create) (0) 2021.06.19 Database App with JDBC : 학생 리스트 DB에서 가져오기 (Read) (0) 2021.06.18 Servlets : 기본 사용 방법 (0) 2021.06.18 JSP : Standard JSP Tag Library (JSTL) - I18N Tags (Multi-Lingual App) (0) 2021.06.18 JSP : Standard JSP Tag Library (JSTL) - Functional Tags (0) 2021.06.18