Java Web Learning (3) - MVC

MVC 

 

A, MVC pattern

Representative MVC Model-View-Controller (Model - View - Controller) pattern.

  • Model : model represents the DAO (Data Access Object Data Access Objects) or POJO (Plain Ordinary Java Object ordinary JavaBeans). A portion for processing the application logic of the application data.
  • View : The view data visualization.
  • The Controller : part of the user interaction application process. The controller acting on the model and the view. It controls data flow model objects, view and update the data changes. Separating the model view controller.

 

Two, Model 2

Java Web application development, there are two design models, Model 1 and Model 2.

Model 1 is the center of the JSP page, call the business logic from the JSP, the page display, suitable for small application development.
Act as a JSP view and controller, JavaBeans serve as a model.

Model 2 based on MVC mode, almost all modern Web frameworks are all Model 2 implementation.
Acts as a view from the JSP, Servlet Filter, or to act as a controller, JavaBeans acts as the model.


Each HTTP request to the controller, request URI corresponding to the identified action. action on behalf of an application can perform operations. 
The controller will resolve the URI and call the appropriate action, then the view model objects into the area that can be accessed.
Finally, the controller using the method of RequestDispatcher HttpServletResponse.sendRedirect or jump to view. In the JSP page, display data expression language and custom labels.

 

 Three, Servlet controller

@WebServlet(name="ControllerServlet", urlPatterns={"/input-product", "/save-product"})
public class Controllerservlet extends HttpServlet{

    private static final long serialVersionUID=1579L;

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response throws IOException, ServletException {
      process(request, response);
    }

    @Override 
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
      process (Request, Response); 
    } 

    // processed by all of the input method request process 
    Private  void process (the HttpServletRequest Request, the HttpServletResponse Response) throws IOException, ServletException {
      // Get Action 
      String URI = Request.getRequestURI (); // URI = / contextName / resourceName or / the resourceName 
      int the lastIndex = uri.lastIndexOf ( "/" ); 
      String Action = uri.substring (the lastIndex + 1'd ); 

      // forwarding URL 
      String dispatchUrl = null ; 

      // process Action 
      IF ( "Product-INPUT" .equals (Action)) { 
        dispatchUrl= "/jsp/ProductForm.jsp";
      } else if("save-product".equals(action)){
      // 创建模型
        product = new Product(); 
        product.setName(request.getParameter("name"));
        product.setDescription(request.getParameter("description"));
        product.setPrice(Integer.parseInt(request.getParameter("price")));
        // 业务逻辑 保存模型等
        SaveProductAction saveProductAction = new     SaveProductAction(); 
        saveProductAction.save(product);
        //Add the model view to the request for access attributes 
        request.setAttribute ( "Product" , Product); 

        dispatchUrl = "/jsp/ProductDetails.jsp" ; 
      } 
      // forward 
      IF (! DispatchUrl = null ) { 
        the RequestDispatcher RD = request.getRequestDispatcher (dispatchUrl); 
        rd.forward (Request, Response); 
      } 
  } 
}                                                    
<%--ProductForm.jsp--%>
<form method="post" action="save-product">
<h1>Add Product</h1>
<label>
  <span>Product Name:</span>
  <input id="name" type="text" name="name">
</1abel>
<label>
  <span>Description:</span>
  <input id="description" type="text" name="description">
</label>
<label>
  <span>Price:</label>
  <input id="price" name="price" type="number" step="any">
</label>
<label>
  <span>&nbsp:</span>
  <input type="submit">
</label>
</form>

 

Three, Filter distributor

@WebFilter(filterName="DispatcherFilter", urlPatterns={"/*"})
public class DispatcherFilter implements Filter{
  @Override 
  public void init(Filterconfig filterconfig) throws ServletException { }
  @Override 
  public void destroy() { }
  @Override 
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
    HttpServletRequest req = (HttpServletRequest) request; 
    String uri = req.getRequestURI();
    // ... consistent with Servlet Controller 
    IF (dispatchUrl =! Null ) { 
      the RequestDispatcher RD = request.getRequestDispatcher (dispatchUrl); 
      rd.forward (Request, Response); 
    } the else {
      // filter medium comprises a static target, including All URLs if there is no action will continue to pass along 
      the FilterChain.doFilter (Request, the Response); 
    } 
  } 
}

Guess you like

Origin www.cnblogs.com/JL916/p/11831094.html