Use two small instances to deepen understanding of the Struts2 framework, so as to better grasp its process
User Registration under the Struts tag
Step 1: create a dynamic project and add related class packages
Step 2: register the StrutsPrepareAndFilter filter provided by Struts2 in the web. xml file
<Web-app xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id = "WebApp_ID" version = "2.5"> <welcome-file-list> <welcome-file> index.html </welcome-file> <welcome-file> index.htm </welcome-file> <Welcome-file> index. jsp </welcome-file> <welcome-file> default.html </welcome-file> <welcome-file> default.htm </welcome-file> <welcome-file> default. jsp </welcome-file> </welcome-file-list> <! -- Struts2 filter --> <filter> <! -- Filter name --> <filter-name> struts2 </filter-name> <! -- Filter class --> <! -- Register StrutsPrepareAndExecuteFilter provided by Struts2 --> <filter-class> org. apache. struts2.dispatcher. ng. filter. StrutsPrepareAndExecuteFilter </filter-class> </filter> <! -- Struts2 filter ing --> <filter-mapping> <! -- Filter name --> <filter-name> struts2 </filter-name> <! -- Filter ing --> <url-pattern>/* </url-pattern> </filter-mapping> </web-app>
Step 3: Create the index. jsp homepage to write a user registry ticket using the form tag provided by struts2)
<H2> User Registration
Step 4: Create the success. jsp page after registration
<Div> <ul> <li> User name: <s: property value = "name"/> </li> <li> password: <s: property value = "password"/> </li> <li> Gender: <s: if test = "sex = 0"> female </s: if> <s: else> male </s: else> </li> <li> region: <s: property value = "area"/> </li> <li> Hobbies: <s: property value = "holobby"/> </li> <li> Description: <s: property value = "description"/> </li> </ul> </div>
Step 5: Create a UserAction class that inherits from the ActionSupport class to process user registration requests and user information editing requests.
Public class UserAction extends ActionSupport {private static final long serialVersionUID = 1L; // username private String name; // password private String password; // description private String description; // gender private int sex = 0; // province private String area; // interests private String [] Hober; // user registration public String execute () throws Exception {return SUCCESS;} public String getName () {return name;} public void setName (String name) {this. name = name;} public String getPassword () {return password;} public void setPassword (String password) {this. password = password;} public String getDescription () {return description;} public void setDescription (String description) {this. description = description;} public int getSex () {return sex;} public void setSex (int sex) {this. sex = sex;} public String getArea () {return area;} public void setArea (String area) {this. area = area;} public String [] getholobby () {return holobby;} public void setholobby (String [] holobby) {this. holobby = holobby ;}}
Step 6: Create the configuration file Struts. xml. In this file, configure the UserAction object to be placed under the src folder)
<! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.1 // EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <! -- Declare the variable development mode) --> <constant name = "struts. devMode" value = "true"/> <! -- Declare package --> <package name = "myPackge" extends = "struts-default"> <! -- Create a TagAction ing --> <action name = "userAction" class = "com. lxy. UserAction"> <! -- Return page for successful registration --> <result> success. jsp </result> </action> </package> </struts>
The running effect is as follows:
Enter Registration Information
650) this. length = 650; "src =" http://www.bkjia.com/uploads/allimg/140207/2232332040-0.jpg "title =" 1.jpg" width = "500" height = "309" border = "0" hspace = "0" vspace = "0" style = "width: 500px; height: 309px; "alt =" wKioL1LodEvwd7QKAAK9QoEqsGE880.jpg "/>
Registered successfully
650) this. length = 650; "src =" http://www.bkjia.com/uploads/allimg/140207/22323352b-1.jpg "title =" 2 (22.16.jpg "width =" 500 "height =" 211 "border =" 0 "hspace =" 0 "vspace =" 0 "style =" width: 500px; height: 211px; "alt =" wkiom1lodj6zt_fvaag3ucdz4248926.jpg "/>
Use the verification framework to verify data under the Struts2 tag
Step 1: create a dynamic project and add related class packages
Step 2: register the StrutsPrepareAndFilter filter provided by Struts2 in the web. xml file
<Web-app xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id = "WebApp_ID" version = "2.5"> <welcome-file-list> <welcome-file> index.html </welcome-file> <welcome-file> index.htm </welcome-file> <Welcome-file> index. jsp </welcome-file> <welcome-file> default.html </welcome-file> <welcome-file> default.htm </welcome-file> <welcome-file> default. jsp </welcome-file> </welcome-file-list> <! -- Struts2 filter --> <filter> <! -- Filter name --> <filter-name> struts2 </filter-name> <! -- Filter class --> <! -- Register StrutsPrepareAndExecuteFilter provided by Struts2 --> <filter-class> org. apache. struts2.dispatcher. ng. filter. StrutsPrepareAndExecuteFilter </filter-class> </filter> <! -- Struts2 filter ing --> <filter-mapping> <! -- Filter name --> <filter-name> struts2 </filter-name> <! -- Filter ing --> <url-pattern>/* </url-pattern> </filter-mapping> </web-app>
Step 3: Create the user login Action object UserAction and configure it in Struts. xml
Public class UserAction extends ActionSupport {private static final long serialVersionUID = 1L; private String username; // user name private String password; // password // User Logon @ Override public String execute () throws Exception {return SUCCESS;} 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 ;}}
Struts. xml
<Struts> <! -- Declare package --> <package name = "myPackge" extends = "struts-default"> <! -- Configure UserAction --> <action name = "userAction" class = "com. lxy. UserAction"> <! -- User Logon page --> <result name = "input">/login. jsp </result> <! -- Registration successful page --> <result>/success. jsp </result> </action> </package> </struts>
Step 4: Create login. jsp on the user logon page and use the Struts form tag to create login. jsp)
<S: form action = "userAction" method = "post"> <s: textfield name = "username" label = "username" required = "true" requiredposition = "left"> </s: textfield> <s: password name = "password" label = "password" required = "true" requiredposition = "left"> </s: password> <s: submit key = "submit" value = "login"> </s: submit> </s: form>
Step 5: Create success. jsp
<H2> <s: property value = "username"/>. logon successful
Step 6: Compile the user authentication file UserAction-validation.xml (must be placed in the package where UserAction is located)
<Validators> <! -- Verify the user name --> <field name = "username"> <field-validator type = "requiredstring"> <message> enter the user name </message> </field-validator> </field> <! -- Verify the password --> <field name = "password"> <field-validator type = "requiredstring"> <message> enter the password </message> </field-validator> </field> </validators>
The running effect is as follows:
Login
650) this. length = 650; "src =" http://s3.51cto.com/wyfs02/M02/11/F5/wKiom1LoeHuzO_USAAGWy7IqZxc293.jpg "title =" 3.jpg" width = "500" height = "135" border = "0" hspace = "0" vspace = "0" style = "width: 500px; height: 135px; "alt =" wKiom1LoeHuzO_USAAGWy7IqZxc293.jpg "/>
Logon successful
650) this. length = 650; "src =" http://s3.51cto.com/wyfs02/M00/11/F5/wKiom1LoeLCTDWgeAAF0sAyyspY565.jpg "title =" 4.jpg" width = "500" height = "133" border = "0" hspace = "0" vspace = "0" style = "width: 500px; height: 133px; "alt =" wKiom1LoeLCTDWgeAAF0sAyyspY565.jpg "/>
This article from the "good memory, not as bad pen" blog, please be sure to keep this source http://lixiyu.blog.51cto.com/4136883/1355303