1. Understand Struts
The core components in the Struts2 framework are ACTION, interceptor, etc., and the Struts2 framework uses a package to manage ACTION and interceptors. Each package is a collection of multiple ACTION, multiple interceptors, and multiple interceptors.
Package elements are used to define package configuration in the Struts.xml file, and each package element defines a package configuration. Its common attributes are:
l name: must be filled in the name of the package.
l exccomes: Optional attributes are used to specify the package to inherit other packages. Inheriting other packages, you can inherit the definition of ACTION in other packages, interceptor definitions, etc.
l namespace: Optional attributes are used to specify the name space of the package.
2. Configure Struts
First create a new web project, right -click a project, select Add Struts under MyEclipse
Select Struts2.1 Click Next to choose the package you need in saving
3. Modify the user login verification example to add one more registered user function.
1. Modify the action class:
package org.qiujy.web.struts2.action; Import com.opensymphony.xWork2.ACTIONCONTEXT; Import com.opensymphony.xWork2.ACTIONSUPPORT; jy*@version1.0*/publicClass loginaction extendSupport {Private String username ; Private String Password; Private String msg; // Results information attribute/***@Returnthemsg*/Public String Getmsg () {ReturnMSG;}/** Set */ Publicvoid Setmsg (String msg) {this.msg = msg;} / ** *@Returntheusername * / Public String GetUsername () {ReturnUsername;} / ** *@ParaserNamernametoset * / PUBLICVOID S ETUSERNAME (string username) {this.username = username;} /** *@retarnthepassword * / Public String Getpassword () {ReturnPassword;} / ** *@ParampasswordThepassword * / Publicvoid Setpassword (String Password) {This.Pass word = password;} /***processing login () method required by the user*@Return results navigation string *@Throwsexception */Public String Login () Throws Exception {if ("test" .equals (123) && "test" .equals (123)) {msg = "Login success, welcome" + 123; // Get ActionC ontext instance To visit the service api actionContext Context = ActionContext.getContext (); // See if the user name has been stored in the session, if it is stored: the description has been logged in; // Otherwise, it is the first time that it is successfully logged in (NULLLL ! = Context.getSession (). Get ("Uname")) {msg = this.username + ": You have already logged in !!!";} else {context.getSession (). Put ("uname", this this, this this, this this, this this, this this, this this, this this, this this, this this, this, .Urname);} ReturnThis.Success;} Else {msg = "Login failed, the username or password is wrong"; RetURNTHIS.ERROR;} Public String Regist () Thrown Exception {// Code of the user name, password, password, password Add to the database // ... msg = "registered successfully. "; Returnthis.Success;}} 2. Struts.xml file: No change, configure the same configuration as before
<! Doctype Struts Public "-// Apache Software Foundation // DTD Struts Configuration 2.0 // EN" Http://struts.org/dtruts/2.0.dtd">< Struus ts> <package name = " My "extends =" Struts-Default "namespace ="/Manage "> <!-Define the request URL to login.action-> <action name =" useropt "> <! The mapping relationship with resources-> <result name = "success">/success.jsp </result> <result name = "error."/error.jsp </result> </Package> </strongs>
3. Page:
index.jsp
<%@ page language = "java" pageEncoding = "UTF-8"%> <HTML> <Head> <Title> User Login Page </Title> </Head> <body> User Entry </h2> <hr> <FORM ACTION = "Manage/Useropt! Login.ACTION" Method = "Post"> <TABLE> <TR> <TD> Username: </td> <input type = "text" name = "username"/> </td> </tr> <l> <TD> Password: </td> <td> <input type = "password" name = "password"/> </td> </tr> <tr> <td colorpan = "2"> <input type = "submit" value = "OK"/> </TD> </TABLE> </Form> </Body> </html>
regist.jsp
<%@ page language = "java" pageencoding = "utf-8"%> <html> <head> <Title> User registration page </title> </head> <body> User registration </h2> <hr> <FORM ACTION = "Manage/Useropt! Regist.ACTION" Method = "Post"> <TABLE> <TD> Username: </td> <input type = "text" name = "username"/> </td> </tr> <l> <TD> Password: </td> <td> <input type = "password" name = "password"/> </td> </tr> <tr> <td colorpan = "2"> <input type = "submit" value = "register"/> </td> </table> </form> </body> </html>
You can use Sturts now.
The above is all the contents of this article. I hope everyone can like it.