Sturuts configuration, struts2 Configuration
1. Understand the core components of the strutsStruts2 framework, such as Action and interceptor. The Struts2 framework uses packages to manage actions and interceptors. Each package is a collection of multiple actions, multiple interceptors, and multiple interceptor references. In the struts. xml file, the package element is used to define the package configuration. Each package element defines a package configuration. Its common attributes include: l name: mandatory, which is used to specify the package name. L extends: an optional attribute that specifies that the package inherits from other packages. Inherit other packages and inherit the Action and interceptor definitions in other packages. L namespace: an optional attribute used to specify the namespace of the package. 2. Configure struts to create a new web project, right-click a project, select add struts under myeclipse, select struts2.1, click Next, select the desired package, and save it. 3.Modify the user logon verification example and add an additional user registration function. 1. Modify the Action class:
| PackageOrg. qiujy. web. struts2.action;ImportCom. opensymphony. xwork2.ActionContext;ImportCom. opensymphony. xwork2.ActionSupport ;/***@ AuthorQiujy *@ Version1.0 */PublicClassLoginActionExtendsActionSupport {PrivateString userName;PrivateString password;PrivateString msg; // result attributes /***@ ReturnThemsg */PublicString getMsg (){ReturnMsg ;}/***@ ParamMsgthemsgtoset */PublicVoidSetMsg (String msg ){This. Msg = msg ;}/***@ ReturnTheuserName */PublicString getUserName (){ReturnUserName ;}/***@ ParamUserNametheuserNametoset */PublicVoidSetUserName (String userName ){This. UserName = userName ;}/***@ ReturnThepassword */PublicString getPassword (){ReturnPassword ;}/***@ ParamPasswordthepasswordtoset */PublicVoidSetPassword (String password ){This. Password = password;}/*** login () method for processing user requests *@ ReturnResult navigation string *@ ThrowsException */PublicString login ()ThrowsException {If("Test". equals (123) & "Test". equals (123) {Msg = "Logon successful, welcome" +123; // Obtain the ActionContext instance and use it to access the Servlet API ActionContext context = ActionContext.GetContext(); // Check whether the user name has been stored in the session. If the user name is saved, the user has logged on. // otherwise, the user name is successfully logged on for the first time.If(Null! = Context. getSession (). get ("uName") {msg =This. UserName + ": You have logged on !!! ";}Else{Context. getSession (). put ("uName ",This. UserName );}ReturnThis.SUCCESS;}Else{Msg = "Logon Failed, incorrect user name or password ";ReturnThis.ERROR;}}PublicString regist ()ThrowsException {// Add the user name and password to the database //... msg = "registered successfully. ";ReturnThis.SUCCESS;}} |
2. struts. xml file: the configuration is the same as before.
| <! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.0 // EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name = "my" extends = "struts-default "namespace ="/manage "> <! -- Define the action for processing the request URL as login. Action --> <action name = "userOpt" class = "org. qiujy. web. struts2.action. LoginAction"> <! -- Define the ing between processing result strings and Resources --> <result name = "success">/success. jsp </result> <result name = "error">/error. jsp </result> </action> </package> </struts> |
3. Page: index. jsp
| <% @ Page language = "java" pageEncoding = "UTF-8" %> <FormAction="Manage/userOpt! Login. action"Method="Post"><Table border = "1"> <tr> <td> User Name: </td> <input type = "text" name = "userName"/> </td> </tr> <td> password: </td> <input type = "password" name = "password"/> </td> </tr> <td colspan = "2"> <input type = "submit" value = "OK"/> </td> </tr> </table> </form> </body> |
Regist. jsp
| <% @ Page language = "java" pageEncoding = "UTF-8" %> |
Now you can use sturts.