Design a REST-style Java MVC Framework

Source: Internet
Author: User

Are you disappointed with the traditional Java MVC frameworks such as Struts and WebWork? A large number of XML configuration files, *. do ing causes many formbeans to be written to pass page parameters? The forced Action interface makes it inconvenient for a class to process only one request.

 

Popular Web sites have already implemented REST-style URLs, such as Douban URLs, to show a book that is not subject. do? Id = xxx, but a natural class of http://www.douban.com/subject/2129650.

 

You may say that using Apache RewriteMod or writing a UrlRewriteFilter by yourself can also achieve this goal. However, why cannot we implement this pattern from the MVC Framework Design itself?

 

The design goal of the Express-Wind framework is to provide a very natural URL ing method interface that does not limit URL Processing.

 

You can use a simple @ Mapping to tell the framework the URL to be processed by a method:

 

 

Public class MyPage {


@ Mapping ("/hello ")

Public String hello (){

Return "

}


@ Mapping ("/hello/$1 ")

Public String hello (String name ){

Return "

}

}

 

 

The URL parameters are passed through $1, $2 ...... And is automatically passed in to the method parameters.

 

You will get the following display effects, depending on the URL:

 

 

The Return Value of the method can be String, for example, a small HTML section or void, indicating that you have handled the HttpServletResponse object by yourself. As a DEMO, the above Code works very well during demonstration. However, as a practical application, we need a template system to render the page. At this time, a TemplateRenderer object should be returned:

 

@ Mapping ("/")

Public Renderer index (){

Map <String, Object> model = new HashMap <String, Object> ();

Return new TemplateRenderer ("/index. jsp", model );

}

 

You don't need to know how the page is rendered. You just need to input the template file path and a map as the model. Without any configuration, express-wind uses JSP as the template by default. Of course, you can also configure velocity as the template.

 

How to integrate with IOC framework?

 

Currently, almost no projects do not use the IOC framework. Express-wind supports and only supports integration with the IOC framework. That is to say, express-wind cannot be used without the IOC framework. You need to do two things:

 

1. Put the class containing @ mapping in the IOC framework (Spring xml configuration file or guice module)

 

2. tell express-wind whether you are using spring, guice, or other IOC containers. Then express-wind automatically scans all beans in the IOC container and retrieves the bean Methods marked with @ mapping, therefore, the whole URL is 0. If a URL ing does not work, you probably forget to put the bean in the IOC container.

 

For more information about express-wind, see:

 

Http://code.google.com/p/express-me/wiki/ExpressWind

 

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.