1. Create a javaweb project and copy the struts jar package to WebRoot/WEB-INF/lib.
2. The jar package cannot be copied. the user's request must reach the ActionServlet before processing the request through struts. Therefore, the ActionServlet must be configured in the web. xml file.
[Java]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Web-app version = "2.4"
Xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://java.sun.com/xml/ns/j2ee
Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd>
<Servlet>
<Servlet-name> action </servlet-name>
<Servlet-class> org. apache. struts. action. ActionServlet </servlet-class>
<Load-on-startup> 1 </load-on-startup>
</Servlet>
<Servlet-mapping>
<Servlet-name> action </servlet-name>
<Url-pattern> *. do </url-pattern>
</Servlet-mapping>
</Web-app>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Web-app version = "2.4"
Xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://java.sun.com/xml/ns/j2ee
Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd>
<Servlet>
<Servlet-name> action </servlet-name>
<Servlet-class> org. apache. struts. action. ActionServlet </servlet-class>
<Load-on-startup> 1 </load-on-startup>
</Servlet>
<Servlet-mapping>
<Servlet-name> action </servlet-name>
<Url-pattern> *. do </url-pattern>
</Servlet-mapping>
</Web-app>
3. Create an actionForm. the username and password on the form must be consistent with the half-end of the get/set Method of the actionForm attribute.
[Java]
Package com. xxjstgb. struts;
Import org. apache. struts. action. ActionForm;
/**
* Log on to ActionForm to collect data from the form.
* @ Author liuzhengquan
*
* // @ SuppressWarnings ("serial ")
Public class LoginActionForm extends ActionForm {
Private String userName;
Private String password;
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;
}
}
Package com. xxjstgb. struts;
Import org. apache. struts. action. ActionForm;
/**
* Log on to ActionForm to collect data from the form.
* @ Author liuzhengquan
*
* // @ SuppressWarnings ("serial ")
Public class LoginActionForm extends ActionForm {
Private String userName;
Private String password;
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;
}
}
4. Create a LoginAction. here we need to inherit the struts Action class. Process the logic of the Model layer and return the redirection information.
[Java]
Package com. xxjstgb. struts;
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;
/**
* Logon Action
* Obtains form data, calls business logic, and returns redirection information.
* @ Author liuzhengquan
*
*/
Public class LoginAction extends Action {
@ Override
Public ActionForward execute (ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
Throws Exception {
LoginActionForm laf = (LoginActionForm) form;
String userName = laf. getUserName ();
String password = laf. getPassword ();
UserManager userManager = new UserManager ();
Try {
UserManager. login (userName, password );
Return mapping. findForward ("success ");
} Catch (UserNoFoundException e ){
E. printStackTrace ();
Request. setAttribute ("msg", "user cannot be found. User Name = [" + userName + "]");
} Catch (PasswordErrorException e ){
E. printStackTrace ();
Request. setAttribute ("msg", "Incorrect password ");
}
Return mapping. findForward ("error ");
}
}
Package com. xxjstgb. struts;
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;
/**
* Logon Action
* Obtains form data, calls business logic, and returns redirection information.
* @ Author liuzhengquan
*
*/
Public class LoginAction extends Action {
@ Override
Public ActionForward execute (ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
Throws Exception {
LoginActionForm laf = (LoginActionForm) form;
String userName = laf. getUserName ();
String password = laf. getPassword ();
UserManager userManager = new UserManager ();
Try {
UserManager. login (userName, password );
Return mapping. findForward ("success ");
} Catch (UserNoFoundException e ){
E. printStackTrace ();
Request. setAttribute ("msg", "user cannot be found. User Name = [" + userName + "]");
} Catch (PasswordErrorException e ){
E. printStackTrace ();
Request. setAttribute ("msg", "Incorrect password ");
}
Return mapping. findForward ("error ");
}
}
5. Configure the struts-config.xml file. In struts (1), we use if... Else determines the intercepted URL and calls the corresponding Action based on the corresponding URL. In the struts framework has been a simple encapsulation of this, we only need to configure the action and actionForm in the struts-config.xml file. By ing their relationships, the matching work is achieved, replacing the original if... Else statement.
[Java]
<? Xml version = "1.0" encoding = "ISO-8859-1"?>
<! DOCTYPE struts-config PUBLIC
"-// Apache Software Foundation // DTD Struts Configuration 1.2 // EN"
Http://struts.apache.org/dtds/struts-config_1_2.dtd>
<Struts-config>
<Form-beans>
<Form-bean name = "LoginForm" type = "com. xxjstgb. struts. LoginActionForm"> </form-bean>
</Form-beans>
<Action-mappings>
<Action path = "/login"
Type = "com. xxjstgb. struts. LoginAction"
Name = "LoginForm"
Scope = "request">
<Forward name = "success" path = "/login_success.jsp"> </forward>
<Forward name = "error" path = "/login_failed.jsp"> </forward>
</Action>
</Action-mappings>
</Struts-config>
<? Xml version = "1.0" encoding = "ISO-8859-1"?>
<! DOCTYPE struts-config PUBLIC
"-// Apache Software Foundation // DTD Struts Configuration 1.2 // EN"
Http://struts.apache.org/dtds/struts-config_1_2.dtd>
<Struts-config>
<Form-beans>
<Form-bean name = "LoginForm" type = "com. xxjstgb. struts. LoginActionForm"> </form-bean>
</Form-beans>
<Action-mappings>
<Action path = "/login"
Type = "com. xxjstgb. struts. LoginAction"
Name = "LoginForm"
Scope = "request">
<Forward name = "success" path = "/login_success.jsp"> </forward>
<Forward name = "error" path = "/login_failed.jsp"> </forward>
</Action>
</Action-mappings>
</Struts-config>
6. Access
[Html]
<Body>
<Form action = "login. do" method = "post">
UserName: <input type = "text" name = "userName"> <br>
Password: <input type = "password" name = "password"> <br>
<Input type = "submit" value = "login">
</Form>
</Body>
<Body>
<Form action = "login. do" method = "post">
UserName: <input type = "text" name = "userName"> <br>
Password: <input type = "password" name = "password"> <br>
<Input type = "submit" value = "login">
</Form>
</Body>
So far, the login instance has been completed through the struts framework.