Design the rest-style MVC framework

Source: Internet
Author: User
Tags regular expression java web

Java developers must be familiar with the MVC framework, from Struts to Webwork,java MVC frameworks emerge. We've gotten used to dealing with *.do or *.action-style URLs, writing a controller for each URL, and inheriting an action or Controller interface. However, the popular Web trend is to use a much simpler, restful URL for users and search engines to be more user-friendly. For example, the link from a book in watercress is http://www.douban.com/subject/2129650/, not http://www.douban.com/subject.do?id=2129650.

Experienced Java Web developers use URL rewriting to implement similar URLs, such as configuring the Mod_rewrite module for the front-end Apache server, and sequentially writing a regular expression for each address that needs to implement URL rewriting, or a custom Rewritefilter to implement URL rewriting using the Filter and request Forwarding (Forward) features provided by the Java Web server, however, you still need to write regular expressions for each address.

Since URL rewriting is so cumbersome, why not directly design an MVC framework that natively supports the REST style?

It is not difficult to design and implement such an MVC framework, and then, from scratch, we examine how to implement restful URL mappings and integrate with common IoC containers such as the Spring framework. This new MVC framework is tentatively named Webwind.

Terms

Mvc:model-view-controller, a common UI architecture pattern, makes it easier to implement an extensible UI by separating the model (model), view (view), and Controller (Controller). In a WEB application, Model refers to the data returned in the background; View refers to a page that needs to be rendered, usually a JSP or other template page, and the rendered result is usually HTML; Controller refers to a controller written by a web developer that handles different URLs (in Struts And the MVC framework itself has a predecessor controller that receives all URL requests and distributes them to the Controller written by the Web developer based on the URL address.

Ioc:invertion-of-control, control reversal, is the current popular container for managing all component lifecycles and complex dependencies, such as the Spring container.

Template: Template, by rendering, the variables in the template will be replaced by the actual data of Model, and then the generated content is the HTML that the user sees in the browser. A template can also implement simple logic such as judgment and loop. In essence, a JSP page is also a template. In addition, there are many Third-party template engines, such as Velocity,freemarker.

Design Objectives

Unlike the traditional MVC framework such as struts, in order to support restful URLs, we don't map a URL to a Controller class (or Struts Action), but instead map a URL directly to a method so that the Web Developers can put multiple functions similar to a Controller, and Controller does not enforce a requirement that an interface must be implemented. A Controller usually has multiple methods, each of which is responsible for handling a URL. For example, a Controller for managing blogs is defined as shown in Listing 1.

Listing 1. Controller Definition of Management Blog

public class Blog  {
   @Mapping("/create/$1")
   Public void create(int userId) { ... }

   @Mapping("/display/$1/$2")
   Public void display(int userId, int postId) { ... }

   @Mapping("/edit/$1/$2")
   Public void edit(int userId, int postId) { ... }

   @Mapping("/delete/$1/$2")
   Public String delete(int userId, int postId) { ... }
}

The @Mapping () annotation indicates that this is a method for handling URL mappings, in which the parameters in the URL are $ $ ... is passed as a method parameter. For a "/blog/1234/5678" URL, the corresponding method will automatically get the parameters userid=1234 and postid=5678. At the same time, you do not need any XML configuration files that are related to URL mapping.

Use $, $ ... To define a variable parameter in a URL that is simpler than a regular expression, we need to convert it to a regular expression within the MVC framework to match the URL.

In addition, there is no mandatory requirement for method return values.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.