Write in front: this article with the following two articles to read, help to understand.
- Restore Oracle(1): Connection to Oracle
- Struts2 Learning Path (2):Struts2 three value transmission modes
The body of the
Database tables:
Step 1: Write the entity class
Entity class: Defines the data required by the Student class and sets set&get methods, constructors, overrides toString methods, etcCopy the code
Step 2: Write the interface class and factory class
The interface includes adding, deleting, and checking the student table. The factory class is used to get concrete action implementation classes.Copy the code
Step 3: Implement the interface (implementation class), design the test class
The implementation class is the implementation of the interface in step 2, writes the method body, and tests the written methodCopy the code
Step 4: Configure login. XML to associate JSP pages with background actions
Write the following code snippet in the struts. XML file to complete the login. XML configuration
<include file="login.xml"/>
Login.xml is used to process user actions and is distributed to actions for processing and resulting responses. This is the core part of Struts2. The user’s request is sent through the core controller to the Action mapping class. The Action mapping class decides whether to call an Action class and then returns it to the core controller. It then returns a confirmation of execution, executes an Action class through a layer of interceptors, returns a specific view based on the result, and wraps the response back to the client through a layer of interceptors
Here’s the code for login.xml
<package name="default" namespace="/" extends="struts-default">
<action name ="loginAction" class="com.huaruan.action.LoginAction">
<result name="success">Main.jsp</result>
<result name="input" type="redirect">Login.jsp</result>
</action>
<action name ="DbAction" class="com.huaruan.action.DbControlAction">
<result name="QuerryAll">QuerryAll.jsp</result>
<result name="QuerryByName">QuerryAll.jsp</result>
<result name="Delete">QuerryAll.jsp</result>
<result name="Update">ExecuteUpdate.jsp</result>
<result name="Insert">ExecuteUpdate.jsp</result>
</action>
</package>
Copy the code
Two actions, loginAction and DbAction, complete control over login and database operations, respectively
The result tag completes the different responses to the client based on the returned string.
Step 5: Design Login
Interface is shown in figure
The page contains a form, which maps to the Login method of the loginAction action, and the submit method is POST. The form contains a text box, a password box, a submit button, and a reset button, designed using the Strus2 label (s), respectively.
The Action class is designed in a model-driven (model three) manner
Defines the String Login() extends ActionSupport implements ModalDriven{} method that measures user input and returns a different String based on the result for login.xml to respond to. The core code is as follows:
public String Login() throws Exception {
String str = null;
String name= stu.getStuName();
String pwd= stu.getStuPwd();
StudentInter student= Factory.getStudent();
Student s = student.SelectByname(name);
System.out.println(s);
if(s.getStuName()! =null) {if(s.getStuName().equals(name)&&s.getStuPwd().equals(pwd)) {
str = SUCCESS;
}else{ str = INPUT; }}else {
str = INPUT;
}
return str;
}
Copy the code
Step 6: Design the database interaction
The interface is shown as follows:
I won’t go into too much detail here, but the basic principle is the same as the Login design.
Query all modules in detail:
The interface is shown as follows:
<s:iterator value=”list” status=”st” var=”student”>
The iterator tag is used to iterate over elements of a collection, such as a list, set, and map array. There are three attributes worth paying attention to when using the tag
-
- Value attribute: Optional attribute. The value attribute refers to an iterated collection, specified by ogNL expression. If empty, it defaults to the collection at the top of the ValueStack stack.
- 2. Id attribute: The optional attribute refers to the ID of the collection element
- 3. You can customize variables (var XX)
- 4. Status property: An optional property that, when iterated, produces an IteratorStatus object that determines the position of the current element and contains the following property methods:
- int getCount(); Number of iterated elements
- int getIndex(); Iterate over the current index of the element
- boolean getFirst(); Is it the first one
- boolean getEven(); Whether it is accidentally
- boolean getLast(); Is the last one
- bolean getOdd(); Whether or not a surprise that
Then take a look at our code, for example:
<table border="1"Number > < tr > < td > < / td > < td > student id < / td > < td > name < / td > < td > password < / td > < td > gender < / td > < td > age < / td > < td > action < / td > < / tr > < s: iterator value ="list" status="st" var="student">
<tr>
<td><s:property value="#st.getCount()"/></td>
<td><s:property value="#student.stuId"/></td>
<td><s:property value="#student.stuName"/></td>
<td><s:property value="#student.stuPwd"/></td>
<td><s:property value="#student.stuSex"/></td>
<td><s:property value="#student.stuAge"/></td>
<td><a href="DbAction! Delete? stuId=# stuId"/> "> Delete
</tr>
</s:iterator>
</table>
Copy the code
Here, the list is the encapsulated data (of type Student) returned by the Action to the foreground, var is the variable Student defined to fetch the object property value in the list, and status is used to determine the element position. The last column, which I use to delete this column, is defined as a label, and the address goes to the corresponding Action class, along with the stuId parameter that marks the deletion of this column.
Details in the Action class: Use a combination of pattern three and pattern one for value passing. As shown in figure
- Delete(): Used to complete the deletion operation
- Insert() : Used to perform the Insert(new) operation
- SelectAll(): used to complete all query operations
- SelectByName (): Used to perform queries by name
- Update(): Used to complete the Update operation (change the password)
Finally release the project and test run.
If there are clerical errors in the article, please leave a message to inform, will be corrected in time. thank you —Chuxus