Struts, Ajax, restful, and Web Service

Source: Internet
Author: User

I admit that there is a bit of a title party, but the several technologies mentioned in the topic do have similarities. I want to know the truth and let me explain them in detail.

At the beginning of the project, I was not satisfied with the huge size of struts, so I wanted to implement a tiny version based on its principle. Later, I thought that I could add some of the above Ajax and restuful web service ideas. After weeks of hard work, we developed a basic web framework named unicorn ). At the beginning of this article, I will introduce this self-written framework in some column, and I plan to open source it, hoping to communicate and improve it together.

First, in order to quickly understand what it is, let's take a look at the configuration file:

 <?  XML version = "1.0" encoding = "UTF-8"  ?> 
< Config >
< Routers >
< Router Class = "Com. Mh. Router. mysessioncheckrouter" > </ Router >
</ Routers >
< Actions >
< Action Class = "Com. Mh. Action. useraction" > </ Action >
< Action Class = "Com. Mh. Action. informationaction" > </ Action >
< Action Class = "Com. Mh. Action. mobileaction" > </ Action >
</ Actions >
</ Config >

The above is the configuration file of the entire project. It can be seen that there are very few items to be configured. You only need to define what the action class and the router class have to be OK. The framework pursues the idea that the rule is greater than the configuration. As for how requests are distributed, this does not require manual configuration, and the framework automatically solves this problem. Here we will introduce two concepts: Action and router. Anyone familiar with Struts must know action. Action replaces Servlet and JSP. All submitted requests are distributed to different actions by struts. The same is true here. Action is the request recipient after framework processing. Let's talk about the router, which literally refers to a router. Anyone who has learned about a computer network knows that IP packets can arrive smoothly on the network because the router determines the transmission path based on the route table. Here, the router is also used to create a distribution policy based on the URL of the Access Server. The router is completely customizable. You can define your own router to define the URL distribution policy, and the custom router has a higher priority than the default router.

2. Do it on the URLArticle

/Useraction/login/username = Admin & Password = Admin

First, we will introduce the processing method of the system methodrouter. The URL above is divided into three parts according to "/". The first part is the request action class, the second part is the method in the class, and the third part is the submitted parameter Param. This is inspired by the URL design in Youku's architecture.

After such a design, we can understand why the above configuration file can be so simple.

Of course, you may not use this method. The framework provides support for custom router. For example, you want to process the URL as follows:/Foreground page/subtopic

To implement the above method, you can define the router by yourself, obtain the above URL in the router, and then perform processing and forward to the corresponding JSP page.

3. Data Transmission Using JSON

Ajax requests can easily process JSON data and can easily interact with the system.

Some people have accused the Web Service of using the XML format of soap for data transmission. Restful mode advocates following the HTTP syntax, and uses URLs in combination with get, post, put, and delete to transmit requests. The results are widely used in the roil camp and are considered as a more elegant way for Web Services. Therefore, this framework also draws on their advantages and can transmit request data through URLs, such as The param section in the preceding URL. However, the HTTP semantics highlighted by restful is not followed. The methods of get and post requests can also be defined as others, which fully depends on your mood, because it does not matter for function implementation. In addition, I think a uniform method is used to avoid the trouble of specifying the request method.

Data is returned in JSON format, which is more lightweight, concise, and elegant than soap, and is directly supported by more platforms. For example, the Android platform supports JSON format processing. If you use Web Service soap, you may need to import a third-party library of kSOAP.

On non-browser clients, you can use the tool class to complete Web service operations,

Public interface iwebservice {public list <liteinformationdto> getinformationsofownerapp (string ownerapp, int start, int limit) throws sockettimeoutexception ;}

After such encapsulation, it is no different from web service and more efficient, because it is easier to process JSON than to process soap xml.

 

First, I will write so much, and then I will continue to introduce it in detail.

Related Article

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.