Struts2 example, struts2 instance
(1) Use MyEclipse to create a Web Project named myStruts2. The structure is as follows:
(2) Import struts2 requires the package, I Will unzip the struts-2.3.16.3 \ apps \ struts2-blank \ WEB-INF \ lib \ all the packages below are imported;
(3) modify web. xml and set filter and filter-mapping:
<?xml version="1.0" encoding="GB2312"?><web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
(4) create three new jsp files under WebRoot \ jsp:
Input. jsp, used to input information
<% @ Page language = "java" pageEncoding = "gb2312" %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> <! -- Struts2 tag Library call declaration --> <% @ taglib prefix = "s" uri = "/struts-tags" %>
List. jsp, used to list input information
<% @ Page language = "java" contentType = "text/html; charset = GB2312" %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
Error. jsp, used for error prompt:
<% @ Page language = "java" contentType = "text/html; charset = GB2312" %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
(5) create a struts. xml file and set the package and Action:
<? Xml version = "1.0" encoding = "gb2312"?> <! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.0 // EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <! -- Action package definition --> <package name = "myStruts2" extends = "struts-default"> <! -- Global navigation page definition --> <global-results> <result name = "global">/jsp/error. jsp </result> </global-results> <! -- Action name, class, and navigation page definition --> <! -- Use the Action class to navigate the Action definition --> <action name = "INPUT" class = "myStruts2.InputAction"> <result name = "input">/jsp/input. jsp </result> <result name = "list">/jsp/list. jsp </result> <result name = "error">/jsp/error. jsp </result> </action> <! -- Define the Action in Direct Navigation --> <action name = "index"> <result>/jsp/input. jsp </result> </action> </package> </struts>
(6) Create an InputAction class:
Package myStruts2; import com. opensymphony. xwork2.ActionContext; import com. opensymphony. xwork2.ActionSupport; public class InputAction extends ActionSupport {// Action class public private variable, used for page navigation flag private static String FORWARD = null; // name, phone number, mailbox private String name; private String phone; private String mail; public String getName () {return name;} public void setName (String x) {this. name = x;} public String GetPhone () {return phone;} public void setPhone (String x) {this. phone = x;} public String getMail () {return mail;} public void setMail (String x) {this. mail = x;} public String execute () throws Exception {// the property value is the name = getName (); phone = getPhone () entered on the JSP page (); mail = getMail (); try {// judge whether the input value is an empty object or if (name! = Null &&! Name. equals ("") & phone! = Null &&! Phone. equals ("") & mail! = Null &&! Mail. equals ("") {ActionContext. getContext (). getSession (). put ("name", getName (); ActionContext. getContext (). getSession (). put ("phone", getPhone (); ActionContext. getContext (). getSession (). put ("mail", getMail (); // navigate to the operation success Page Based on the marked content. FORWARD = "list ";} else {// navigate to the Operation Failure Page Based on the flag content. FORWARD = "error" ;}} catch (Exception ex) {ex. printStackTrace ();} return FORWARD ;}}
(7) Run Tomcat after deployment. The result is as follows:
A small struts2 project instance is only used for learning and exercises.
Struts2 verification framework verifies User Registration
The core code is as follows:
1. register. jsp
Java code
1. <form action = "register. action" method = "post">
2. <I> <font face = "_ GB2312" color = "# FF0000"> <s: fielderror/>
3. </font>
4. </I>
5. <! -- Read the display prompt -->
6. <table>
7. <tr>
8. <td>
9. User Name:
10. </td>
11. <td>
12. <input type = "text" name = "user. userName">
13. </td>
14. </tr>
15. <tr>
16. <td>
17. Password:
18. </td>
19. <td>
20. <input type = "password" name = "user. password">
21. </td>
22. </tr>
23. <tr>
24. <td>
25. Confirm the password:
26. </td>
27. <td>
28. <input type = "password" name = "user. rePassword">
29. </td>
30. </tr>
31.
32. <tr>
33. <td>
34. Age:
35. </td>
36. <td>
37. <input type = "text" name = "user. age">
38. </td>
39. </tr>
40. <tr>
41. <td>
42. Birthday:
43. </td>
44... the remaining full text>
Struts2 multi-instance Problems
Struts [Action is a multi-instance, not a Singleton, that is, an Action object is generated for each request. The reason is: struts 2 Action contains data. For example, the data you enter on the page will be contained in the member variable of the Action 9517. If the Action is a single instance, these data will affect each other in a multi-threaded environment. For example, you can see the data filled in by others, and the Action of struts 1 is single-instance, because its data is stored in the Form class, in the multi-threaded environment, Action is only responsible for processing some logic, and there is no data, that is to say, we use it as a tool. The servlet is also the xbf of a single instance.