The first two articles explain the basic implementation of the Struts framework, and I feel a little boring. Today I will use the logon example to implement it.
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.
<?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.
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.
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; /*** log on to the Action * to obtain the form data, call the business logic, and return the redirection information * @ author liuzhengquan **/public class loginaction extends action {@ overridepublic 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 = new usermanager (); try {usermanager. login (username, password); Return Mapping. findforward ("success");} catch (usernofoundexception e) {e. printstacktrace (); Request. setattribute ("MSG", "user not 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.
<?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
<Body> <form action = "login. do "method =" Post "> User name: <input type =" text "name =" username "> <br> password: <input type = "password" name = "password"> <br> <input type = "Submit" value = "Logon"> </form> </body>
So far, the login instance has been completed through the Struts framework.