Front-end specification of Java Web project (using namespace for deep decoupling of js)

Source: Internet
Author: User

A good code architecture is not only easy to develop and maintain, but also an art of management and execution.

Over the past few years, I have gone through many projects, and I am deeply disgusted with the problems of strong coupling between codes, nonstandard writing, and poor maintainability. After careful analysis, I summarized a set of front-end writing specifications for Java Web projects based on my coding habits. I would like to share with you.

Ps: thanks to a hai for his creativity. I will sort it out later (with File Download ):

I. project structure

This is not much different from other projects. I pull out the template to make it easier to analyze and understand:


Explanation: js mainly includes extends (introduce third-party js), module (the project module's own js), and lib (reference package, which can be further split here ), module contains the js in the specific module. Common. js is the js of the core namespace.

Ii. common. js

To explain, Globals serves as a global namespace, each module can define a domain in this namespace (if you do not understand it, you can learn the namespace by yourself, and this file can be written to death, ).

Iii. task. js

This file is the js (example) of the task module in my project in the module. The module-level js is generally used to extract the relatively independent and important methods from the page, we do not recommend that JavaScript be included on all pages. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140320/20140320085130213.jpg" alt = "\">

Explanation: module-level js mainly includes its own private attributes and methods, as well as js attributes and methods exposed externally. Here, we add "_" in front of all internal private items by default, and finally decide which methods and attributes are exposed, which is exposed by constructor.

4. page index. jsp

Now let's take a look at how jsp references page js and module js. It's time to witness decoupling.



Explanation: The common. js and task. js pages need to be introduced and initialized to access the methods and attributes exposed in the task module.

In page js, we recommend that all variables be sent to a variable group for easy maintenance.

We recommend that you bind the onclick and other methods of controls on the page in JavaScript.


Conclusion: Through the above configuration, the biggest advantage is that js files are frequently introduced in projects, eliminating coupling between them, in addition, methods and attributes of the same name between different modules will not affect each other. Of course, the most important thing is to facilitate continuous development and maintenance, and it is also an artistic pleasure.

This article uses the annotation style I used to. Of course, it may vary from person to person. My principle is:

1. Module introduction Code focuses on: module description, owner, related remarks, double star comments

/**

*

*/

2. For the division of large areas under the module, double horizontal lines are used to note:

// ================================================ ================

// Description:

3. For the method annotations in the region, not everyone is willing to write them in detail through previous experience. Therefore, I think it is best to be simple and clear, and to use common Annotations:

// Code Description

// The author's remarks and other information (not limited)

4. For some important methods in the area, or you may want to distinguish between the districts, use a star line.

********** *******************/

If you have good comments, you can discuss them together. If you feel that the article is not helpful to you, you can just laugh at it.

Leave the email address with attachments required ~

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.