Webwork Study Notes

Source: Internet
Author: User

Webwork Study Notes
------------------------------
Author: Kingfish (vc--@sohu.com)
Keywords: webwork1.4, tomcat5
--------------------------------------
I recently learned about webwork and found very few materials (if you have any, please send me a copy. Thank you first ),
As it is an open-source project, I have a rough look at the source code. Below is a note I have prepared after reading it,
It is relatively simple, hoping to provide some help to users.

The level is limited, and errors are inevitable. Please forgive me + correct me!

Environment Description:
Use tomcat5.0 + webwork1.4
Unpack the downloaded webwork. War in webwork under Tomcat's webapps as the webwork directory.

1. a servlet: webwork. Dispatcher. servletdispatcher

1. Introduction
Webwork1.4 all the actions in the built-in examples are submitted to this servlet for processing. The importance is self-evident.
Open Web. xml under webwork/WEB-INF and you will see the following configuration

...
<Servlet>
<Servlet-Name> action </servlet-Name>
<Servlet-class> webwork. Dispatcher. servletdispatcher </servlet-class>
<Load-on-startup> 1 </load-on-startup>
</Servlet>
...
<Servlet-mapping>
<Servlet-Name> action </servlet-Name>
<URL-pattern> *. Action </url-pattern>
</Servlet-mapping>
...


2 features:
1. Process Action requests
2. Find the corresponding action class to generate an instance.
3. Run dovalidation () to verify the input parameters;
4. If the 3 verification result is valid, execute doexecute ();
5. Search for the views configuration based on the execution result string ("success", "input", "error") and go to the corresponding view.

2. Write a simple example

1. helloaction. Java source code

Import webwork. Action. actionsupport;
Import webwork. action. Action;

Public class helloaction
Extends actionsupport {
Private string name;
Public helloaction (){
}

Public void setname (string name ){
This. Name = Name;
}

Public String getname (){
Return this. Name;
}

Public void dovalidation (){
Else if (name = NULL | Name. Length () <1 ){
Adderror ("name", "can't be null or empty! ");
}
}

// If no other processing is performed, You can do not override this method. Success is returned by default.
Public String doexecute (){
Return success;
}
}
Compile the compiled class file into webwork/WEB-INF/classes.

2. Configuration
Two configuration methods are available:
(1) Configure views. properties under webwork/WEB-INF/classes)
Hello. Action = helloaction
Hello. Input = helloinput. jsp
Hello. Success = hellosuccess. jsp
Hello. Error = helloerror. jsp

(2) Configure actions. XML (under webwork/WEB-INF/classes)
Add the following action on the basis of the original
<Action name = "helloaction" alias = "hello">
<View name = "success"> hellosuccess. jsp </View>
<View name = "error"> helloerror. jsp </View>
<View name = "input"> helloinput. jsp </View>
</Action>

3. Related JSP files
Create the following four JSP files in the webwork directory.
(1) index. jsp
<HTML>
<Head>
<Title> test webwork </title>
</Head>
<Body>
<A href = "helloinput. jsp"> example </a>
</Body>
</Html>
(2) helloinput. jsp
<HTML>
<Head>
<Title> test webwork </title>
</Head>
<Body>
<Form action = "Hello. Action" method = "Post">
<Input type = "text" name = "name" value = ""/>
<Input type = "Submit" value = "Submit"/>
</Form>
</Body>
</Html>
(3) hellosuccess. jsp
<HTML>
<Head>
<Title> test </title>
</Head>
<Body>
Hello world!
</Body>
</Html>
(4) helloerror. jsp
Same as above. just remove the string.
In this example, this page is used only for configuration and is not used during running.

4. Run
(1) start Tomcat
(2) Enter http: // localhost: 8080/webwork/index. jsp in IE.
(3) Submit without entering any characters. The page is still in helloinput. jsp
Enter the character and submit it. Go to hellosuccess. jsp

5. process description
(1) initialize the servlet (servletdispatcher) after starting tomcat)
(2) On the input page (helloinput. jsp), submit the input character (submit to hello. Action)
(3) According to the configuration in Web. XML, this request is processed by servletdispather.
(4) servlet searches for configuration based on actionname ("hello") (configuration content: Hello. Action = helloaction)
(5) generate a helloaction instance
(6) servlet calls the dovalidation () of helloaction to verify the input parameters.
(7) after the verification is passed, the servlet calls the doexecute () of helloaction and returns the success ("success ").
(8) According to the Views configuration, we found: Hello. Success = hellosuccess. jsp (actionaliasname. viewname = pagename)
(9) forward to hellosuccess. jsp in Servlet
(10) Output Hello world !, Finished.

If you submit without entering, the steps below are slightly different
(7) If the verification fails, an error message is generated. If an error message is detected in other methods, "input" is returned by default"
(8) According to the Views configuration, we found: Hello. Input = helloinput. jsp.
(9) forward to helloinput. jsp in Servlet
(10) completed.

6. Supplement
The call steps of helloaction are:
Servletdispatcher-> genericdispatcher-> call executeaction ()-> execute ()-> call the corresponding helloaction method of actionsupport.
The description in section 5 is for simplicity.
If you are interested in this, you can look at the servletdispatcher, genericdispatcher, actionsupport, action and other source code.

Iii. Brief Introduction of the Bank app in the webwork built-in example
1. Environment
Bank app under JSP in http: // localhost: 8080/webwork
Related class: webwork. Examples. Bank. Transfer in webapps/webwork/WEB-INF/lib/webwork-examples.jar
Related JSP: webapps/webwork/examples/Bank/index. jsp, transfer. jsp, confirm. jsp
Related configuration: webapps/webwork/-WEB-INF/classes/views. Properties
# Bank example (struts example adaptation)
Bank. Transfer. Action = Bank. Transfer (webwork. Examples. Bank. Transfer)
Bank. Transfer. Input = transfer. jsp
Bank. Transfer. Success = confirm. jsp
Bank. Transfer. Error = transfer. jsp
Because webwork. properties is configured
Webwork. Action. packages = webwork. Examples, webwork. Examples. VXML
Therefore, the configuration of Bank. Transfer. Action = Bank. Transfer omitted webwork. Examples.

2. index. jsp
<JSP: Forward page = "bank. Transfer! Default. Action "/>
Equivalent
<Form action = "bank. Transfer! Default. Action "method =" Post ">
<Input type = "Submit"/>
</Form>
Automatic submission.
(1) submit it to the servlet and the servlet processes the bank. Transfer! Default. Action (Composition: actionaliasname +! Commandname +. Action)
Because the action contains command (command: default), you can use reflect to find docommand (dodefault ),
Run dodefault () to return "input"
(2) The view corresponding to the input is transfer. jsp. Go to this page.
(3) input the parameter and submit it. Execute bank. Transfer. Action. The other process is similar to the preceding section 5th.

 

========================================================== ========================================================== ===
Kingfish 2004.3
If you post a post, please keep the document complete.

Welcome!

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.