1) create a webproject
2) Add the jar package required for struts Development (8 in total) to the WEB-INF/lib
3) copy the prepared raw web. xml and struts-config.xml files to the WEB-INF directory
Content of the web. xml file:
<? XML version = "1.0" encoding = "UTF-8"?>
<Web-app version = "2.5"
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_2_5.xsd>
<Welcome-file-List>
<Welcome-File> index. jsp </welcome-File>
</Welcome-file-List>
<! -- Standard action servlet configuration (with debugging) -->
<Servlet>
<Servlet-Name> action </servlet-Name> // servlet name
<Servlet-class> org. Apache. Struts. Action. actionservlet </servlet-class> // complete Servlet Path
<Init-param>
<Param-Name> config </param-Name>
<Param-value>/WEB-INF/struts-config.xml </param-value> // configure the name of the struts configuration file, called the struts-config.xml, which needs to be modified here when another name is called (configurable, this name is used by default.
Characters)
</Init-param>
<Init-param>
<Param-Name> debug </param-Name>
<Param-value> 2 </param-value> // configure the servlet output Log Level (optional)
</Init-param>
<Init-param>
<Param-Name> detail </param-Name>
<Param-value> 2 </param-value>
</Init-param>
<Load-on-startup> 2 </load-on-startup> // servlet initialization time
</Servlet>
<! -- Standard action Servlet Mapping -->
<Servlet-mapping>
<Servlet-Name> action </servlet-Name>
<URL-pattern> *. DO </url-pattern> // defines the matching mode. All *. Do requests are intercepted by this servlet.
</Servlet-mapping>
</Web-app>
Struts-config.xml File Content:
<? XML version = "1.0" encoding = "ISO-8859-1"?>
<! Doctype Struts-config public
"-// Apache Software Foundation // DTD struts configuration 1.2 // en"
Http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd>
<Struts-config>
</Struts-config>
The above work is to configure the basic struts environment.
4) Create an actionform class for data collection. The created actionform must inherit from the org. Apache. Struts. Action. actionform class and provide the attributes to be collected in the actionform class. The name must be the same as the name in the form.
When the actionform collects data, Struts will automatically initialize this class. After the new method is complete, he calls the Set Method to collect the attributes to be collected on the form. (Actionform is a JavaBean that complies with the JavaBean rules.
Fan)
5) create an action class for logical processing. The created action must inherit from the org. Apache. Struts. action. Action class. Rewrite the execute (four parameters) method. In the execute method, he has used actionform as
When the parameter is passed, you only need to forcibly convert it to get the data on the form. Eg: loginactionform LAF = (loginactionform) form;
Then, use LAF. GetUserName (); LAF. GetPassword (); to obtain the data in the form, and then call the model layer for further processing. After processing, an actionforward object is returned.
Actionforward object Method: Using actionmapping object, actionmapping object corresponds to the configuration information of the struts-config.xml, with the struts-config.xml, the corresponding configuration information (path, class,
Success) is stored in the actionmapping object, so an actionmapping instance corresponds to an action tag in a struts-config.xml. We use Return Mapping. findforward ("success ");
You can return an actionforward object, and the following is the configuration of action in the struts-config.xml.
6) Configure struts-config.xml: here you need to configure actionform and action, and you need to configure actionform first (determined by DTD ). The actionform configuration is as follows:
<Form-beans>
<Form-bean name = "loginform" type = "com. Liu. Struts. loginactionform"/> // The name is the name given to the actionform. Type is equivalent to class, that is, the complete path of this actionform
Path. After configuration, the Struts framework has this actionform.
</Form-beans>
The action configuration is as follows:
<Action-mappings>
<Action Path = "/login" // here defines the path for page access, through the action in Form
Type = "com. Liu. Struts. loginaction" // The type here is the same as the class, that is, the complete path of the class.
Name = "loginform" // set the actionform that matches the action. The name is the name defined for this actionform.
Scope = "request "//
>
<Forward name = "success" Path = "/login_success.jsp"> </forward> // defines the page to which success is redirected.
<Forward name = "error" Path = "/login_error.jsp"> </forward>
</Action>
</Action-mappings>