MVC Web site (Spring MVC) Writing Experience Summary

Source: Internet
Author: User

The single site uses the MVC architecture Pattern Experience Summary, contains the specific website package classification, the package class's writing, the attention matter and so on. I think, according to the following package classification and writing method, has satisfied a simple ordinary website (mainly for Java) writing requirements.

MVC main Level

    • Database persistence layer (model)

Common applications include Hibernate, MyBatis, and other persistent layer frameworks.

1. DAO (repository)

The classes contained in this package are basic operations for different databases.
For relational databases, it is a common crud (which may include paging, fuzzy query, and sort) operations, and currently forms the JPA specification in Java EE.
For non-relational databases, Key-value.

Generally for different database types, under the DAO package, build a child package, for example: DAO.JPA.

2. Domain

For relational databases, it is common to use ORM Mapping technology to map database tables into Java class objects.

    • Business Logic Layer (service)

Invoke the method in DAO to implement the specific business logic.
1. Domain.page
Combine the results from a database query to generate the type required for the page. The corresponding class is placed under the Domain.page package.

    • Control Layer (Controller)

This layer is mainly for the interoperability of pages. Accept the page to pass the data, verify and process it, and then pass the parameters to the service. Gets the returned result, passing the corresponding result to the page.
1. Controller
Controller.screen, the implementation of common page jumps, the internal does not contain specific business logic, the function is based on the URL, jump to the page.
Controller, you can implement an interface (RESTAPI), and you can implement calls to business logic.


Other

    • Other Packages:

1. config
Used for some configuration classes.
2. Common
Common objects, record global variables, enumerate classes, and so on.
3. Utils
Tool kits, placement of tool methods such as parameter checking.

    • Interface method Implementation policy:
//interface is returned, it must be noted that it is generally divided into parameter errors, exception returns, method error return (NULL), correct return, four ways. //Choose how to handle it (using enumerations to increase readability)://1. Use the error code to associate the error message with the error code (class HTTP). //Pros: Classify different anomalies in a simple way. //Disadvantages: Too many kinds, too many error codes, difficult to identify. //2. Direct the error message, using the wrapper, to return to the previous layer. //Advantages: Simple and direct. //Cons: Not easy to identify the conversion, if the error message is in English, difficult to convert into Chinese display. //Adoption: Error code (main, pass up) + error message (auxiliary, log display) + wrapper PublicObject MyApi (...) {//parameter checking is necessary, and attention should be written as if block to prevent multi-layer//If-else nested, easy to expand. if(check (param1)) {returnError1;}if(check (param2)) {returnError2;} ......Try{...}Catch(Exception e) {returnerrorexception;}}

MVC Web site (Spring MVC) Writing Experience Summary

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.