MVC concept
image.png从model 到 controller
// 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);
- 从model取得数据,放入request中
- 这里就是从自己写的类里面读了数据出来,使用
setAttribute
放入数据到request
中。
从controller 到 view
- RequestDispatcher
RequestDispatcher dispatcher =
request.getRequestDispatcher("view_students_two.jsp");
这个dispatcher
就是作为一个wrapper
来封装服务器端文件(如servlet, JSP file, or HTML file)。
getRequestDispatcher
的参数用来定位文件位置。
这里是封装了jsp
文件,位于view_students_two.jsp
。
- forward method
dispatcher.forward(request, response);
这个forward
就是起传递作用,将request
从servlet
传递到服务器的其它文件(如servlet, JSP file, or HTML file)。
这里就是将request
传递给了jsp
, 目的达到。
package com.luv2code.servletdemo.mvctwo;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class MvcDemoServletTwo
*/
@WebServlet("/MvcDemoServletTwo")
public class MvcDemoServletTwo extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public MvcDemoServletTwo() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
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);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
package com.luv2code.servletdemo.mvctwo;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class MvcDemoServletTwo
*/
@WebServlet("/MvcDemoServletTwo")
public class MvcDemoServletTwo extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public MvcDemoServletTwo() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
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);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Reference
https://docs.oracle.com/javaee/7/api/javax/servlet/RequestDispatcher.html
网友评论