Tiny Quick Start Control layer development

Source: Internet
Author: User

Front Tiny Quick start service development and tiny Quick start web Interface Rapid development Practice, that is, the development of services and interface is not a problem, it is obvious to do a complete system is not enough, so there must be a control layer, before the entire process can be opened.

The control layer acts as a collaborative role between the interface and the service. The controller receives the user input information from the interface layer, then invokes the service to perform the specific processing, and passes the final result back to the interface layer for rendering.

The control layer in the tiny framework can be written in many ways:

    • Integrated struts or other solutions to complete
    • Follow the tiny framework's requirements code to complete
    • Complete by Tiny page flow

The integration of struts and other scenarios is no longer described here, only in the way that is achieved through the tiny framework:

I. Writing control layer logic through code 1. code example
  1. @Controller ()
  2. public class Helloaction implements webcontextaware{

  3. Private Webcontext Webcontext;

  4. public void SetContext (Webcontext webcontext) {
  5. This.webcontext = Webcontext;
  6. }

  7. @RequestMapping (value={"/hellobymvc.do"})
  8. @View (value= "/helloworld/helloresult.page")
  9. public void Sayhellomethod (String name) {
  10. if (name = = null) {
  11. name = "World";
  12. }
  13. Webcontext.put ("Result", String.Format ("Hello,%s", name));
  14. }

  15. }
Copy Code

Explain

@Controller () indicates that the class is a class of control layers.
ImplementsWebcontextaware says the class is going to be injected into the webThe context object.
@RequestMapping (value={"/hellobymvc.do"}) represents a response to a/hellobymvc.do request, note that this can have multiple values.

The @View (value= "/helloworld/helloresult.page") indicates that the execution is complete and then goes to the/helloworld/helloresult.page page for rendering.
Webcontext, the web context, is an important class in the tiny web framework for placing various kinds of data on it.
You can think of it as an image map, and you can keep stuffing it in and out of it, which is visible in the presentation layer.

Of course, for the above so simple class, actually do not implement the Webcontextaware interface, this time the wording is as follows:

    1. @Controller ()
    2. public class Helloaction {
    3. @ResultKey (value = "Result")
    4. @RequestMapping (value={"/hellobymvc.do"})
    5. @View (value= "/helloworld/helloresult.page")
    6. public string Sayhellomethod (string name) {
    7. if (name = = null) {
    8. name = "World";
    9. }
    10. Return String.Format ("Hello,%s", name);
    11. }
    12. }
Copy Code
Relatively speaking, it is simpler than the one above, but the only limitation is that only one object is returned.
    1. @ResultKey (value= "users")
    2. @RequestMapping (value={"/crud/service/tinydb/list.page"})
    3. Public list<bean> queryusers (@ServiceParameter (name = "TUser") Bean TUser) throws Exception {
    4. bean[] Beans = Operator.getbeans (TUser);
    5. if (beans! = null) {
    6. return arrays.aslist (beans);
    7. }
    8. return null;
    9. }
Copy Code
of course, the incoming parameter does not have to be a simple type, and the object is also allowed. 2. Summary

Through annotations, it is convenient to define the processing logic of the control layer in the common Java class, the steps:

    • add on Class @Controller Annotations
    • Adding @requestmapping Annotations to methods
    • If there is a return value, it can be returned by injecting an Webcontext object, or it can be done by @resultkey annotations, but @resultkey can only put the return value of the method into the context with the specified key.

Question: What if the control layer processing method requires an object?

    • If you conform to the naming conventions of the tiny framework, everything is done automatically by the framework.
    • If you do not conform to the naming conventions of the tiny framework, please inject webcontext objects and assemble them yourself.
Second, write control layer through page Flow 1. Control layer Complete all functions

Of course, the HelloWorld function is too simple, it can even be done directly in the control layer.

104025_4n0j_1245989.jpg (3.57 KB, download number: 0)

Download attachments

2015-5-27 21:51 Upload

While this is not a logical problem for the computer, it also gives the correct results, but in practice it is problematic because the business logic should not be done at the control level.

Therefore, the above approach is wrong and is not recommended.

2. Business flow and page flow separation

So the right thing to do is write a business flow:

104909_ikvk_1245989.jpg (23.04 KB, download number: 0)

Download attachments

2015-5-27 21:51 Upload


The process is written, and then you can write a page flow:

130035_23u4_1245989.jpg (26.72 KB, download number: 0)

Download attachments

2015-5-27 21:51 Upload

Of course, it is not necessarily a line, it can also be complex, such as:

134906_htao_1245989.jpg (11.13 KB, download number: 0)

Download attachments

2015-5-27 21:51 Upload

Page flow as long as a service is invoked, the result of the service is automatically placed in the context because the service is finished, so no additional processing is required by default.

3. Summary

Through the way of page flow, there is a very clear page processing logic display, easier to understand the business process. Because you don't have to write a program, you don't have to worry about the programmer adding some dangerous code in it (which is strictly prevented in some particular industry), the process is documentation, and the document can be quickly generated.

There are already many page flow components in the tiny framework, and of course support for the passionate customization of your add-ons, which involves the development of process components and is not explained in detail in this section.

4. Document Generation

First step: Select a wizard

131500_3z8f_1245989.jpg (36.22 KB, download number: 0)

Download attachments

2015-5-27 21:51 Upload


Step two Select the document to be generated

131516_edzv_1245989.jpg (41.57 KB, download number: 0)

Download attachments

2015-5-27 21:51 Upload

Then, just look at the generated document.

131527_d88u_1245989.jpg (60.34 KB, download number: 0)

Download attachments

2015-5-27 21:51 Upload

The technical manager can fully understand the results of the program execution and the programmer's completion by looking at the document.

It seems a bit overreaching oh, next time focus on the process orchestration related things.

Summary

The control layer of the tiny framework provides flexible and diverse writing methods, such as automatic assembly of objects, data validation, code writing control layer, and programming control layer of process arrangement. Really frees programmers from the intricacies of their work and only does what they need to do.

At this point, the main four sections of the tiny QuickStart have been discussed in the past, and the following is:

Tiny Quick Start service development

Tiny Quick Start web Interface Rapid development Practice

Tiny Quick Start Control layer development

Tomorrow will be the preparation of tiny quick Start process development , please look forward to

Tiny development of control layer for quick start

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.