The relationship between the Struts program
The relationship between the "How to achieve the simplest Struts program" example in each file and the source code analysis are described.
[Related Links]
"JavaWebstudio Series Development Tools Visaul Struts Version" http://dev.9cbs.net/develop/Article/28/28871.shtm
"How to achieve the simplest Struts program" http://dev.9cbs.net/develop/Article/28/28946.shtm
"The implementation of the Struts drop-down box" http://dev.9cbs.net/develop/Article/28/28956.shtm
"The implementation of the Sruts radio box" http://dev.9cbs.net/develop/Article/29/29042.shtm
"Struts check box" http://dev.9cbs.net/develop/Article/29/29043.shtm
Development Environment: Java Web Studio Series Development Environment Visual Strutst Version.
The latest JavaWebStudio download address: ftp: //210.36.64.79/kui
(The relationship between the two Struts programs are connected (1))
4 Action file
The Hello program's action file implementation of the form of the form submitted by the input file, the file name is HelloAction.java, the file code is as follows:
(Omit)
Public Final Class HelloWordAction Extends Action
{
// Variable definitions:
Private log log = logfactory.getlog ("org.apache.struts.Webapp.example");
// Function definition:
Public ActionForward Execute (ActionMapping Mapping,
Actionform Form,
HTTPSERVLETREQUEST REQUEST,
Httpservletresponse response
Throws Exception
{
// Extract Attributes and Parameters WE WILL NEED
Locale locale = getLocale (request);
MessageResources Messages = GetResources (Request);
HttpSession session = request.getations ();
HelloWordform myform = (hellowordform) Form;
String variable1 = myform.getMyBeanvariable1 ();
Try
{
IF (variable1.trim (). Equals (""))
{
Return New ActionForward (mapping.getinput ());
}
// Other code
Mapping.Findforward ("Success"));
}
Catch (Exception E)
{
//log.error ("error", e);
//log.trace ("error");
Throw new runtimeException (E.GetMessage ());
}
}
}
First define the processing function execute (), you can understand that when the Hello input file form is submitted to the ActionServlet, the ActionServlet calls an Execute () function to implement business logic processing, and forward the results into the output file after processing. The execute () function defines a Helloform instance "MyForm" and reads the data from the submitted input ActionForm object instance Form, assigns the value of the MyBeanVariable1 variable to the new string variable variable1: helloform myform = (helloform) form;
String variable1 = myform.getMyBeanvariable1 ();
Next is an error handling, the try statement is used to combine an exception block, which makes us telling Java to pay attention to the code segment that may have an exception; Catch: Once you turn an exception, you need to capture this exception, catch statement The programmer defines a capture; Finally: The program may need to make sure that a code block is always executed, such as what happens, we may need to ensure a specific file; ExcePTLON : A class for information discovered in the code about a "exception" or "error"; throws: When some things need to pay attention, we "throw" an exception. This exception is "grasped" by a special code we have created.涣涣 涣涣, by throwing an exception, we tell the code: an error, you need to call some special logic dealing problems. The following is complete, its complete end:
Try
{
// Normal code
}
Catch (Exception E)
{
// error handling code
Throw new runtimeException (E.GetMessage ());
}
Finally
{
// Finally, the processing code must be performed.
}
Whether the variables we conducted in the TRY block is empty, if you are empty, forward to the input page, otherwise forward to the output page defined as Success:
IF (variable1.trim (). Equals (""))
{
Return New ActionForward (mapping.getinput ());
}
// Other code
Mapping.Findforward ("Success"));
The mapping.getinput () function is used to get the URL value of the input page, which corresponds to the INPUT value value of the
5 Struts Program Application Profile Struts-Config.xml File:
For convenience of explanation, here we call the web.xml file as an application's "project profile", refer to other configuration files such as struts-config.xml, struts-configation.xml as an application's "Struts program application Profile ".
Xml Version = "1.0" encoding = "ISO-8859-1"?>
"- // Apache Software Foundation // DTD Struts Configuration 1.0 // En"
"http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd">
TYPE = "EMPTYPRJ.HELLOWORM" /> A bean is defined above, named "HelloWordForm", the corresponding class is HelloWordForm. In addition to saving data, it is also used to implement the authentication of Bean data before the server ActionServlet calls HelloWordAction. form-beans> TYPE = "EMPTYPRJ.HELLOWORDACTION" name = "HelloWordform" Scope = "session" Input = "/ helloword.jsp"> action> The above defines an Action configuration, where PAT is equal to the forwarded path name, used for the action attribute value in the form in the jsp file; Type is equal to the class processing class, meaning when the user submits the form, the server ActionServlet calls the HelloWordAction implementation form Processing; Name is equal to the bean name, corresponds to the Name property value in the form in the jsp file; session is equal to submitting data storage scope; Input input page; action-mappings> Struts-Config>