I. Build an environment
1. Import the jar package (the downloaded file is struts-1.3.10-all.zip) and import all the jar packages under the lib directory of struts.
2. Create a profile under the WEB-INF: struts-config.xml, as shown below:
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype Struts-config public "-// Apache Software Foundation // DTD struts configuration 1.3 //" http://struts.apache.org/dtds/struts-config_1_3.dtd ">
<Struts-config>
<Form-beans>
<! -- Let struts know about loginform and provide it to action. Name is the reference name of form, and type is the full name of the corresponding form class. -->
<Form-bean name = "loginform" type = "cn. itcast. loginform"> </form-bean>
<Form-bean name = "addstudentform" type = "cn. itcast. addstudentform"> </form-bean>
</Form-beans>
<! -- The <action-mappings> element helps to control the internal process of the Framework. You can send a request URI scheme to the action class and associate the action object with the actionform object. <Action-mappings> you can define multiple <action> child elements -->
<Action-mappings>
<! -- <Action> describes the ing between a specific request path and a corresponding action class. Has the following attributes:
Attribute: sets the attribute key of the form bean associated with the action in the request/session, and returns the form bean instance through the getattribute (attribute) method of the Request/session.
Input: the URL forwarded by the request when the form verification fails. If the form verification fails, the request is redirected to the target module represented by this value -->
<! -- Configure action. If path is actionservlet, you can find the full name of the loginaction class and type is action class. name indicates the form used by actionservlet (that is, the form used to store the value submitted by the form, if no, a null pointer is thrown.) -->
<Action Path = "/login" type = "cn. itcast. loginaction" name = "loginform">
<! -- Forward: Jump, name: Specifies the jump method, and Path: Specifies the jump page, which is equivalent to a key-value pair -->
<Forward name = "loginsuccess" Path = "/loginsuccess. jsp"> </forward>
<Forward name = "loginfailure" Path = "/loginfailure. jsp"> </forward>
</Action>
<Action Path = "/addstudentaction" type = "CN. itcast. addstudentaction "name =" addstudentform "Scope =" request "Validate =" true "attribute =" addstudentform1 "input ="/errorinput. JSP ">
<Forward name = "addstudentsuccess" Path = "/addstudentsuccess. jsp"> </forward>
<Forward name = "addstudentfailure" Path = "/addstudent. jsp"> </forward>
</Action>
<! -- * Indicates the wildcard. The wildcard is kept at the top of the field where {1} appears. This can be used to configure a similar file name. Path and type are mandatory parameters for action. Validate indicates whether verification is required when the form is filled. The default value is true, indicating that verification is required. Input is the page returned after the verification fails. -->
<Action Path = "/edit *"
Type = "org. Apache. Struts. webapp. example. Edit {1} action"
Name = "{1} form"
Scope = "request"
Validate = "false">
<Forward name = "sucess" Path = "/{1}. jsp/">
</Action>
<Action Path = "/Save *"
Type = "org. Apache. Struts. webapp. example. Save {1} action"
Name = "{1} form"
Scope = "request"
<Set-Property = "cancellable" value = "true"/>
</Action>
</Action-mappings>
<! -- Global jump: used when two or more actions are redirected to the same page. -->
<Global-forwards>
<Forward name = "error" Path = "/error. jsp"/>
</Global-forwards>
</Struts-config>
3. register the struts center controller in Web. xml -- actionservlet:
<? XML version = "1.0" encoding = "UTF-8"?>
<Web-app version = "3.0"
Xmlns = "http://java.sun.com/xml/ns/javaee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation = "http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd>
<Display-Name> </display-Name>
<Servlet>
<Servlet-Name> action </servlet-Name>
<Servlet-class> org. Apache. Struts. Action. actionservlet </servlet-class>
<Init-param> <! -- Initialize and read the configuration file -->
<Param-Name> config </param-Name> <! -- Parameter name -->
<Param-value>/WEB-INF/struts-config.xml </param-value> <! -- Parameter value: file name. Multiple configuration files can be created and separated by commas (,). -->
</Init-param>
<Load-on-startup> 0 </load-on-startup> <! -- Instantiate the service as soon as it is started -->
</Servlet>
<Servlet-mapping>
<Servlet-Name> action </servlet-Name>
<URL-pattern> *. DO </url-pattern> <! -- Add all *. Do requests here -->
</Servlet-mapping>
<Welcome-file-List>
<Welcome-File> index. jsp </welcome-File>
</Welcome-file-List>
</Web-app>
2. Create a class that inherits actionform: Package CN. itcast;
Import org. Apache. Struts. Action. actionform;
Public class loginform extends actionform {
Private string username = NULL;
Private string Password = NULL;
Public String GetUserName (){
Return username;
}
Public void setusername (string username ){
This. Username = username;
}
Public String GetPassword (){
Return password;
}
Public void setpassword (string password ){
This. Password = password;
}
}
Register the modify profile struts-config.xml
See.
Iii. Development Action:
1. Create a class that inherits action and overwrites the execute method. Forced transformation is required:
Package CN. itcast;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Import org. Apache. Struts. action. Action;
Import org. Apache. Struts. Action. actionform;
Import org. Apache. Struts. Action. actionforward;
Import org. Apache. Struts. Action. actionmapping;
Public class loginaction extends action {
@ Override
Public actionforward execute (actionmapping mapping, actionform form,
Httpservletrequest request, httpservletresponse response)
Throws exception {
// Inherit the parent class (the parent class will place the value submitted by the form in form), and call loginform to store the value transmitted by the form (that is, convert form to loginfrom)
Loginform = (loginform) form;
// Obtain the value from logginform for business logic processing
String returnurlkeyword = "";
If (loginform. GetUserName (). Equals ("itcast ")){
Returnurlkeyword = "loginsuccess ";
} Else {
Returnurlkeyword = "loginfailure ";
}
// Jump by using the parent class configured by the struts-config.xml (jump first to the actionservlet with the jump method and then select the jump page by the actionservlet)
Return Mapping. findforward (returnurlkeyword );
}
}
Register, modify profile struts-config.xml
See.
Iv. Interface JSP
<Body>
<! -- Request. getcontextpath (): Project context, login. Do is copied from the path in the action in the struts-config.xml -->
<! -- Form submits to login. Do, enters the front-end controller actionservlet (Web. xml: *. Do) and then transfers to the back-end controller loginaction (struts-config.xml: Login) -->
<Form action = "<% = request. getcontextpath () %>/login. Do" method = "Post">
<! -- The name attribute of each form control must be the same as that of the submitted form. Otherwise, a null pointer exception occurs. -->
Username: <input type = "text" name = "username"> <br>
Password: <input type = "password" name = "password"> <br>
<Input type = "Submit" value = "login"> <br>
</Form>
</Body>
5. Notes
1. Action is not secure for a single instance and thread. It is best not to have readable and writable variables in action.
2. What is the <action> dependency in the struts-config.xml file is determined by web. XML, so you should consider the problem that the configuration in Web. XML can be dynamically changed.
3. The path property of the <action> element in the struts-config.xml file must start with '/', and the JSP file path configured in forward must also start with '/'. In short, in a web application, remember that the path always starts with "/", so it cannot be wrong!