Turbine actual combat (below)

zhaozj2021-02-17  169

3.3.2 Layout

Layout is equivalent to the container of Screen, Navigation. Responsible for the layout control of the page.

Template Layout, actually uses Velocity to make page layout control with Velocity as a .vm file as a Layout template.

We have seen a template for a Layout in Section 2.2. It can be seen that in this template file, there is no data containing any display, only some HTML elements used to control the layout. Okay, I believe that you understand what I will say: The Layout Template is equivalent to a box with a lot of plaid. The only thing we need is to put these "plugins" such as Navigation, Screen to these plaids. At this point, Layout is equivalent to the Controller in the presentation layer. Writing here, I can't help but express my heart and gratitude for Turbine developers.

In the previous section, we have made a simple Screen with Velocity. In addition to the need to change the base category to VelocityOnlyLayout [i], the steps written in Layout using Velocity are exactly the same. The Layout Template file is placed in the "templates-path / layouts /" directory.

Now let's take another few main variables in the Layout template in Section 2.2:

l $ screen_PlaceHolder - a very important variable. The place where this variable is placed is where SCREEN will be output. This variable is filled in the SCREEN text information saved in the String object.

l $ navigation - This variable is used to get navigation. Its object type is: org.apache.turbine.util.Template.templatenavigation.

l $ data - don't say more, it is Rundataj.

3.3.3 Navigation

Navigation's Velocity implementation is completely consistent with Screen, Layout. The Navigation Template is placed in the "Templates-path / navigations /" directory. The base class that can be used is: velocitynaVigation.

3.3.4 Action

When the URI entered in the browser contains an action parameter, it is like this:

http://www.server.com/servlet/turbine/template/helloworld.vm/Action/UpdateWorld

Action will be triggered and executed.

At this point, Turbine is performed in the following order:

First, the UpdateWorld Class, located under "Web-INF / CLASS / COM / YOURPAMPANY / App / Modules / Actions /" will be executed; then, execute the HelloWorld Class (if present); finally, execute helloworld.vm.

Action is a module that performs a specific transaction. These transactions may be sent to a Mail, or it is also possible to operate databases, or other possible transactions. No value is returned after the Action execution ends, and it is only possible to place a Message in Rundata via the Rundata.setMessage () method. However, during the Action's transaction process, it is possible to change or interrupt the Template and Screen of Up to be executed.

Velocity also provides support for Action, but is different from the three modules mentioned above (Screen, Layout, Navigation), and Action does not need to write the corresponding template file. The following table is a simple action: package com.yourcompany.app.modules.Actions;

// velocity stuff

Import org.apache.velocity.Context.context;

// Turbine Stuff

Import org.apache.turbine.util.Rundata;

Import org.apache.turbine.modules.Actions.VelocityAction;

Public Class AddUser Extends VelocityAction

{

Public void doperform (Rundata Data, Context Context) Throws Exception

{

IF (Data.GetParameters (). getString ("UserName", NULL) == NULL)

{

Data.setMessage ("UserName Does Not Exist";

SetTemplate (data, "adduser.vm);

Return;

}

// Store User Info INTO DATABASE

Data.setMessage ("Information Stored!");

SetTemplate (Data, "MainMenu.vm");

// stuff unnthing Into the velocity context

Context.put ("Variable", "foo");

}

}

It checks if the submitted data contains "username" variables. If not included, it will switch to the "adduser.vm" template and display an error message. Otherwise, switch to "MainMenu.vm" and display the corresponding prompt information, in addition to this, some data is placed in Velocity Context.

3.3.4.1 Velocity Action Event

The above is just a simple usage of Action, and Turbine also provides a mechanism that is an action event. With the Action Event mechanism, Turbine provides a very convenient way to process the Form Submission, making us more quickly develop Turbine applications.

In order to understand the principles of the Action Event mechanism, let's review Turbine's related content J:

When Turbine accepts a URI containing the Action request, first execute this action, then ... oh, right, that is. However, for those classes inherited from ActionEvent, some wonderful things have occurred when this Action is executed ...

Leave a suspense first, let us look at the code J:

Public Class NewUserfoo Extends VelocityAction

{

Public Void Doadd (Rundata Data, Context Context) Throws Exception

{

// put code here to add the user to the system

Context.put ("UserName", UserName;

Data.setMessage ("User Added!");

Public void doperform (Rundata Data, Context Context) Throws Exception

{

Data.setMessage ("Button Not Found!");

}

}

Then, in our HTML TAG embedded in this code:

...

...

...

This time, please take a look at the execution!

It turns out that when our inheritance starts with the Action of VelocityAction, there is a "event" also passed at the same time [II]. Turbine will automatically perform the appropriate method according to this "event" [III]. If no "event" or the specified "Event" does not have a corresponding processing method, the DOPERFORM () method will be executed.

Ok, I saw such a mechanism to bring us what kind of result? We can write the transactions that need to be done with an Action class to a method of the ActionEvent class! In this way, we don't have to write a corresponding class for every "action", which gives our future program maintenance and document maintenance brings very convenient. Especially in a page, there are many button, we can easily deal with it and prevent us from falling into the strange circle of "if ... else if ...".

However, in order to obtain this magical ability, we have to pay for freedom ... J:

1. Action Event Name must be prefixed as "EventSubmit_"

2, the method of completing the action event must be headed by "do"

3, the first letter after "Do" must be capitalized, the rest must be lowercase

4 summary

This article organizes the method steps for developing simple applications under Turbine, mainly playing a role in introducing entrances. Through this article, the reader should be able to quickly pick up, and then develop a complete Turbine application by some self-practices. It is not difficult.

Note

Velocity Template file lookup method

Screen

Assuming in the Turbine's control process (). GetString ("Template") Returns "/about_us/directions/driving.vm", Turbine will find the corresponding Screen Class as follows.

4, About_us.directions.driving

5, About_us.directions.default

6, About_us.default

7, Default

8, VelocityScreen (ie services.velocityService.default.screen)

If the return value is NULL, VelocityScreen will be executed, and the template file called is "Templates / Screens / INDEX.VM".

Assume any other exception, such as "templates / screens / index.vm" does not exist, or the template file is not legal, or any other exception has occurred, "Templates / Screens / Error.vm" will be loaded.

Layout & Navigation At this time, for Layout and Navigation, you will find the appropriate template file in the order below:

1, / About_us / Directions / Driving.VM

2, / About_us / Directions / Default.vm

3, / about_us / default.vm

4, / Default.vm

Resource

1, http://www.jieesoft.com/resource/jbvelocity/jbvelocity.htm, develop Turbine Apps with JBuilder.

2, http://jakarta.apache.org/builds/jakarta-turbine/tdk/release/, TDK download point.

3, http://jakarta.apache.org/turbine/tdk/tdk-howto.html, TDK Installation Guide.

4, http://jakarta.apache.org/turbine/turbine-2.2.1/howto/context-howto.html, velocity context how-to.

5, http://httpd.apache.org/docs/mod/mod_rewrite.html, how to write the URL in a shorter form.

[i] In the sample program of Turbine 2.2, VelocityECSLayout uses VelocityECSLayout as the default Layout class, but this class does not support Frame. In addition, VelocityDirectLayout, VelocityXSLLayout is also available for developers for developers.

[II] Here, this "Event" is "EventSubmit_Doadd".

[III] At this time, Turbine used the java.lang.Reflect package to complete this magical thing. However, it is worth noting that java.lang.Reflect package can be a "big name" Zhaozuo performance killer.

转载请注明原文地址:https://www.9cbs.com/read-31539.html

New Post(0)