CN108334322B - Data request method and system of MVC framework - Google Patents
Data request method and system of MVC framework Download PDFInfo
- Publication number
- CN108334322B CN108334322B CN201711489175.3A CN201711489175A CN108334322B CN 108334322 B CN108334322 B CN 108334322B CN 201711489175 A CN201711489175 A CN 201711489175A CN 108334322 B CN108334322 B CN 108334322B
- Authority
- CN
- China
- Prior art keywords
- request
- class
- data
- mvc framework
- result
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Active
Links
Images
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F8/00—Arrangements for software engineering
- G06F8/30—Creation or generation of source code
- G06F8/34—Graphical or visual programming
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F8/00—Arrangements for software engineering
- G06F8/70—Software maintenance or management
- G06F8/72—Code refactoring
Landscapes
- Engineering & Computer Science (AREA)
- Software Systems (AREA)
- General Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- Physics & Mathematics (AREA)
- General Physics & Mathematics (AREA)
- Information Retrieval, Db Structures And Fs Structures Therefor (AREA)
Abstract
The invention provides a data request method and a data request system of an MVC framework, wherein the method comprises the following steps: constructing a request class in a model layer in an MVC framework; the request class is used for encapsulating the requested data into a target format; receiving a network access request, calling the request class in a controller, and calling back a request result; when the request result is successful, displaying the data encapsulated into the target format in the view. The method provided by the invention places the processing logic of the data in the model layer, and if other controllers need to be called, the data can be quickly obtained, so that excessive and repeated codes in the controller layer are avoided.
Description
Technical Field
The invention relates to the technical field of computers, in particular to a data request method and a data request system of an MVC framework.
Background
The MVC is named as Model View Controller, is an abbreviation of Model (Model) -View (View) -Controller (Controller), is a software design paradigm, organizes codes by a method of separating business logic, data and interface display, and aggregates the business logic into one component, so that the business logic does not need to be rewritten while improving and personalizing a customized interface and user interaction. MVC was uniquely developed to map traditional input, processing, and output functions into a logical graphical user interface structure.
In a conventional MVC structure, after a data layer is changed, a view layer is notified to perform corresponding processing, and the view layer can directly access the data layer. In iOS, however, communication between M and V is disabled and changes between M and V must be coordinated by the C controller layer.
At present, because the rise time of front-end development is short, most developers can understand the front-end development in an old mode, so that the front-end development code management logic is disordered and is not easy to maintain and reconstruct, the development cost is increased, the refreshing efficiency and the data storage performance of the developed application program are low, and the user experience of the application program is poor.
Disclosure of Invention
Aiming at the defects in the prior art, the invention provides a data request method and a data request system of an MVC framework.
According to a first aspect of the present invention, there is provided a data request method of an MVC framework, comprising:
s1, constructing a request class in the model layer in the MVC framework; the request class is used for encapsulating the requested data into a target format;
s2, receiving a network access request, calling the request class in the controller, and calling back a request result;
and S3, when the request result is successful, displaying the data packaged into the target format in the view.
Wherein, after the step S3, the method further includes: and when the request result is failure, displaying a prompt of network request failure in the view.
Wherein the network access request content comprises: request address, enumeration type, request parameters, encoding type, header information, caching policy, and request callback results.
Wherein, after the step S1, the method further includes: and constructing a first singleton class for setting the header parameters required by the request class.
Wherein the header parameters include: necessary parameter information and optional parameter information.
Wherein the optional parameter information includes: system language, device type, application version, screen height, and screen width.
Wherein, after the step S1, the method further includes: and constructing a second singleton class for setting the cache policy of the request.
Wherein the caching strategy at least comprises caching duration.
According to a second aspect of the present invention, there is provided a data request system of an MVC framework, comprising:
the request class construction unit is used for constructing a request class in a model layer in an MVC framework; the request class is used for encapsulating the requested data into a target format;
a request receiving unit, configured to receive a network access request, call the request class in the controller, and call back a request result;
and the feedback unit is used for displaying the data packaged into the target format in the view when the request result is successful.
According to a third aspect of the present invention, there is provided a computer readable storage medium having stored thereon a computer program for execution by a processor of a method provided by the various possible implementations of the first aspect described above.
According to a fourth aspect of the present invention, there is provided a data requesting apparatus of an MVC framework, comprising:
at least one processor; and
at least one memory coupled with the processor, wherein:
the memory stores program instructions executable by the processor, the processor invoking the program instructions to perform operations comprising: constructing a request class in a model layer in an MVC framework; the request class is used for encapsulating the requested data into a target format; receiving a network access request, calling the request class in a controller, and calling back a request result; when the request result is successful, displaying the data encapsulated into the target format in the view.
The method provided by the invention places the processing logic of the data in the model layer, and if other controllers need to be called, the data can be quickly obtained, so that excessive and repeated codes in the controller layer are avoided.
Drawings
Fig. 1 is a flowchart of an MVC framework data request method based on swift language according to an embodiment of the present invention;
FIG. 2 is a block diagram of a data request system of an MVC framework according to another embodiment of the present invention;
fig. 3 is a block diagram of a data requesting device of an MVC framework according to another embodiment of the present invention.
Detailed Description
The following detailed description of embodiments of the present invention is provided in connection with the accompanying drawings and examples. The following examples are intended to illustrate the invention but are not intended to limit the scope of the invention.
Referring to fig. 1, fig. 1 is a flowchart of an MVC framework data request method based on swift language according to an embodiment of the present invention, where the method includes:
s1, constructing a request class in the model layer in the MVC framework; wherein the request class is used for encapsulating the requested data into a target format.
S2, receiving a network access request, calling the request class in the controller, and calling back a request result;
s3, when the request result is successful, displaying the data packaged into the target format in the view
Specifically, taking the example of obtaining transaction records in the network transaction platform, a class of TradeRequestModel is first constructed, a network request is made in the class, and the request data is encapsulated into a format that we want. In a Controller (Controller) of an MVC framework, calling an instance method of the type, calling a request result in a block mode commonly used in the iOS, wherein the request result comprises a successful compleBlock and a failed Block, and in the Controller, if the compleBlock is triggered, namely the request result is successful, displaying the requested data in a view layer.
The advantage of establishing the request class is to put the processing logic of the data in the model layer, and if other controllers need to be called, the data can be quickly obtained, and excessive and repeated codes in the controller layer are avoided.
On the basis of the above embodiment, the step S3 is further followed by: and when the request result is failure, displaying a prompt of network request failure in the view.
Specifically, when the failedBlock is triggered, it is indicated that the request data fails, and a prompt indicating that the network request fails is sent and then displayed in the display layer.
On the basis of the above embodiment, the network access request content includes: request address, enumeration type, request parameters, encoding type, header information, caching policy, and request callback results.
Specifically, in a request method, the content to be requested may be customized, where the requesting content may include: url String, request address, httpMethod, enumeration type, transmission request type, parameters, request parameters, encoding type, headers, header information, cachePolicy, cache policy: information and failure requested to be successfully returned: the information returned by the failure is requested.
On the basis of the above embodiment, the step S1 is further followed by: and constructing a first singleton class for setting the header parameters required by the request class.
Wherein the header parameters include: necessary parameter information and optional parameter information.
Wherein the optional parameter information includes: system language, device type, application version, screen height, and screen width.
Specifically, a single instance class is added, the name is called HTTPClient, a method is defined in the established TradeRequestModel class, the name is called getTradeRecordListForCommand, in the method, an object of the HTTPClient is obtained by using the single instance method of HTTPClient. In this embodiment, the entire network access request type is get. The same method can be applied to other types of network access requests, such as post requests. The header information in a single instance type http client can be understood as a universal header, and is carried when a network request is sent each time, the header parameter information is divided into two types, the first type is parameter information that must be transmitted when the network request is sent each time, for example, a response server is a content type that can be received by a specified request, such as application/json ', "text/javascript'," application/x-protocol ", and the like; or specifies the request to submit the Content Type "Content-Type": "application/x-www-form-url encoded".
The second type is necessary header parameter information, for example, a software has 6 interfaces, a client needs to transmit the current user mobile phone system language to a server for judging whether Japanese content or Chinese content is needed to be returned, at this time, because the field of the interface needs to be frequent, the newly added interface can be used later, and the universality is higher, so that a systemlangiage field is defined, the current language in the user system is put into an http header, and the server can conveniently obtain the language information of the user at any time after receiving a request. Optional parameter information in the header parameter information includes one or more of a system language, a device type, an application version, a screen height, and a screen width. The optional header information can be selected according to the type of the requested data and the requested device condition, so that the requested data can be completely displayed on the view layer.
On the basis of the above embodiment, the step S1 is further followed by: and constructing a second singleton class for setting the cache policy of the request. Wherein the caching strategy at least comprises caching duration.
Specifically, a singleton class CachePolicy is constructed to process the poetry that whether caching is needed or not, wherein for an interface with a not high real-time requirement, the requested data can be cached by setting a singleton class, and if the same data is requested again within a certain preset time period, that is, within a caching duration, the data is not requested again from the server, but the data in the local cache is called for loading, so that the loading speed of the repeatedly requested data is increased.
In another embodiment of the present invention, referring to fig. 2, fig. 2 is a block diagram of a data request system of an MVC framework according to another embodiment of the present invention, where the system includes: a request class construction unit 21, a request receiving unit 22 and a feedback unit 23.
The request class construction unit 21 is configured to construct a request class in a model layer in the MVC framework; the request class is used for encapsulating the requested data into a target format;
the request receiving unit 22 is configured to receive a network access request, call the request class in the controller, and call back a request result;
wherein, the feedback unit 23 is configured to display the data encapsulated into the target format in the view when the request result is successful.
Specifically, taking the example of obtaining transaction records in the network transaction platform, a class of TradeRequestModel is first constructed, a network request is made in the class, and the request data is encapsulated into a format that we want. In a Controller (Controller) of an MVC framework, calling an instance method of the type, calling a request result in a block mode commonly used in the iOS, wherein the request result comprises a successful compleBlock and a failed Block, and in the Controller, if the compleBlock is triggered, namely the request result is successful, displaying the requested data in a view layer.
The advantage of establishing the request class is to put the processing logic of the data in the model layer, and if other controllers need to be called, the data can be quickly obtained, and excessive and repeated codes in the controller layer are avoided.
On the basis of the above embodiment, the feedback unit is further configured to: and when the request result is failure, displaying a prompt of network request failure in the view.
Specifically, when the failedBlock is triggered, it is indicated that the request data fails, and a prompt indicating that the network request fails is sent and then displayed in the display layer.
On the basis of the above embodiment, the request class constructing unit 21 further includes a singleton class constructing subunit, configured to construct a first singleton class, and configured to set a header parameter required by the request class.
Wherein the header parameters include: necessary parameter information and optional parameter information.
Wherein the optional parameter information includes: system language, device type, application version, screen height, and screen width.
Specifically, a single instance class is added, the name is called HTTPClient, a method is defined in the established TradeRequestModel class, the name is called getTradeRecordListForCommand, in the method, an object of the HTTPClient is obtained by using the single instance method of HTTPClient. In this embodiment, the entire network access request type is get. The same method can be applied to other types of network access requests, such as post requests. The header information in a single instance type http client can be understood as a universal header, and is carried when a network request is sent each time, the header parameter information is divided into two types, the first type is parameter information that must be transmitted when the network request is sent each time, for example, a response server is a content type that can be received by a specified request, such as application/json ', "text/javascript'," application/x-protocol ", and the like; or specifies the request to submit the Content Type "Content-Type": "application/x-www-form-url encoded".
The second type is necessary header parameter information, for example, a software has 6 interfaces, a client needs to transmit the current user mobile phone system language to a server for judging whether Japanese content or Chinese content is needed to be returned, at this time, because the field of the interface needs to be frequent, the newly added interface can be used later, and the universality is higher, so that a systemlangiage field is defined, the current language in the user system is put into an http header, and the server can conveniently obtain the language information of the user at any time after receiving a request. Optional parameter information in the header parameter information includes one or more of a system language, a device type, an application version, a screen height, and a screen width. The optional header information can be selected according to the type of the requested data and the requested device condition, so that the requested data can be completely displayed on the view layer.
On the basis of the foregoing embodiments, the request class constructing unit 21 further includes a second singleton class constructing subunit, configured to construct a second singleton class, and configured to set a cache policy of the request. Wherein the caching strategy at least comprises caching duration.
Specifically, a singleton class CachePolicy is constructed to process the poetry which needs to be cached, in 0, for an interface with a not high real-time requirement, a singleton class can be set to cache the requested data, and if the same data is requested again within a certain preset time period, the data is not requested again from the server, but the data in the local cache is called to be loaded, so that the loading speed of the repeatedly requested data is increased.
Referring to fig. 3, fig. 3 is a block diagram of a data requesting device of an MVC framework according to another embodiment of the present invention. The apparatus comprises: a processor 301, a memory 302, and a bus 303.
The processor 301 is configured to call program instructions in the memory 302 to perform the methods provided by the above-described method embodiments, including, for example, constructing a request class at a model layer in an MVC framework; the request class is used for encapsulating the requested data into a target format; receiving a network access request, calling the request class in a controller, and calling back a request result; when the request result is successful, displaying the data encapsulated into the target format in the view.
Through the above description of the embodiments, those skilled in the art will clearly understand that each embodiment can be implemented by software plus a necessary general hardware platform, and certainly can also be implemented by hardware. With this understanding in mind, the above-described technical solutions may be embodied in the form of a software product, which can be stored in a computer-readable storage medium such as ROM/RAM, magnetic disk, optical disk, etc., and includes instructions for causing a computer device (which may be a personal computer, a server, or a network device, etc.) to execute the methods described in the embodiments or some parts of the embodiments.
Finally, the method of the present application is only a preferred embodiment and is not intended to limit the scope of the present invention. Any modification, equivalent replacement, or improvement made within the spirit and principle of the present invention should be included in the protection scope of the present invention.
Claims (10)
1. A data request method of an MVC framework, comprising:
s1, constructing a request class in the model layer in the MVC framework; the request class is used for encapsulating the requested data into a target format;
s2, receiving a network access request, calling the request class in the controller, and calling back a request result;
s3, when the request result is successful, displaying the data packaged into the target format in the view; and when the request result is failure, displaying a prompt of network request failure in the view.
2. The method of claim 1, wherein the network access request content comprises: request address, enumeration type, request parameters, encoding type, header information, caching policy, and request callback results.
3. The method according to claim 2, wherein the step S1 is followed by further comprising: and constructing a first singleton class for setting the header parameters required by the request class.
4. The method of claim 3, wherein the header parameters comprise: necessary parameter information and optional parameter information.
5. The method of claim 4, wherein the optional parameter information comprises: system language, device type, application version, screen height, and screen width.
6. The method according to claim 3, wherein the step S1 is further followed by: and constructing a second singleton class for setting the caching strategy of the request class.
7. The method of claim 6, wherein the caching policy comprises at least a caching duration.
8. A data request system of an MVC framework, comprising:
the request class construction unit is used for constructing a request class in a model layer in an MVC framework; the request class is used for encapsulating the requested data into a target format;
a request receiving unit, configured to receive a network access request, call the request class in the controller, and call back a request result;
the feedback unit is used for displaying the data encapsulated into the target format in the view when the request result is successful; and when the request result is failure, displaying a prompt of network request failure in the view.
9. A computer-readable storage medium, on which a computer program is stored which, when being executed by a processor, carries out the steps of the method according to any one of claims 1 to 7.
10. A data requesting device of an MVC framework, comprising:
at least one processor;
and at least one memory coupled to the processor, wherein:
the memory stores program instructions executable by the processor, the processor invoking the program instructions to perform the method of any of claims 1 to 7.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201711489175.3A CN108334322B (en) | 2017-12-30 | 2017-12-30 | Data request method and system of MVC framework |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201711489175.3A CN108334322B (en) | 2017-12-30 | 2017-12-30 | Data request method and system of MVC framework |
Publications (2)
Publication Number | Publication Date |
---|---|
CN108334322A CN108334322A (en) | 2018-07-27 |
CN108334322B true CN108334322B (en) | 2021-07-13 |
Family
ID=62923885
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN201711489175.3A Active CN108334322B (en) | 2017-12-30 | 2017-12-30 | Data request method and system of MVC framework |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN108334322B (en) |
Families Citing this family (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN109862076B (en) * | 2018-12-30 | 2022-05-03 | 贝壳技术有限公司 | Service data interaction method, device and system |
CN110990000B (en) * | 2019-11-11 | 2023-06-02 | 山东中创软件工程股份有限公司 | Data request processing method, device and equipment of MVC pattern design model layer |
CN115914337A (en) * | 2022-10-10 | 2023-04-04 | 深圳优美创新科技有限公司 | APP network connection failure processing method |
Citations (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
JP2000132385A (en) * | 1998-10-22 | 2000-05-12 | Ntt Communicationware Corp | Class design method for gui application development |
CN104281441A (en) * | 2013-07-09 | 2015-01-14 | 腾讯科技(深圳)有限公司 | Data extraction method and device based on MVC |
CN107479891A (en) * | 2017-08-17 | 2017-12-15 | 北京理工大学 | One kind is based on MVC model datas processing system and method |
-
2017
- 2017-12-30 CN CN201711489175.3A patent/CN108334322B/en active Active
Patent Citations (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
JP2000132385A (en) * | 1998-10-22 | 2000-05-12 | Ntt Communicationware Corp | Class design method for gui application development |
CN104281441A (en) * | 2013-07-09 | 2015-01-14 | 腾讯科技(深圳)有限公司 | Data extraction method and device based on MVC |
CN107479891A (en) * | 2017-08-17 | 2017-12-15 | 北京理工大学 | One kind is based on MVC model datas processing system and method |
Also Published As
Publication number | Publication date |
---|---|
CN108334322A (en) | 2018-07-27 |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
US11023263B2 (en) | Hybrid server-side and client-side portal aggregation and rendering | |
EP2806357A1 (en) | Method and communication device for updating a web application | |
CN103942225A (en) | Method and system for invoking resources of Hybrid App client and client | |
CN108334322B (en) | Data request method and system of MVC framework | |
CN106874519B (en) | Page display method and device | |
CN108733720B (en) | On-line checking method and checking system for SQL (structured query language) query of multiple database types | |
EP3813326B1 (en) | Method and apparatus for processing webpage, device, and storage medium | |
US20140245124A1 (en) | System and method thereof for browser agnostic extension models | |
WO2017143747A1 (en) | Network request method and system for network terminal | |
CN107517188A (en) | A kind of data processing method and device based on Android system | |
CN111294377B (en) | Dependency network request sending method, terminal device and storage medium | |
CN107438097B (en) | Network request processing method and device | |
CN112559172A (en) | Method, system, equipment and storage medium for optimizing memory of web browser | |
CN108319420B (en) | Picture loading method and device | |
US20160357715A1 (en) | Mechanisms to support multi-service hyperlink pipelines in web browser | |
US20220229858A1 (en) | Multi-cloud object store access | |
US10268536B2 (en) | Secure debugging with an encrypted token | |
US20160092590A1 (en) | Web service framework | |
CN113486116A (en) | Data synchronization method and device, electronic equipment and computer readable medium | |
US20170048344A1 (en) | Webpage Loading Method and Apparatus | |
CN104679786B (en) | Form processing method and device | |
CN103838839B (en) | A kind of the page-downloading control method and system of IE kernel browser | |
CN114422166B (en) | Webpage access method and device, electronic equipment, medium and product | |
WO2023230797A1 (en) | Cross-system test method and apparatus | |
US10909209B2 (en) | Dynamic insertion of content into webpages |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination | ||
GR01 | Patent grant | ||
GR01 | Patent grant |