Struts2 is an excellent MVC-based framework. The use of Struts2 is very simple. Here is a simple procedure for using the Struts2 framework. 1. Download the Jar package
Download the Struts2 Jar package from the Apache website.
2. Deploy Struts. xml and Web. xml
Open the Blank instance in the compressed package, copy the Struts. xml file to the src directory of the project, and copy the filter part of Web. xml to the web. CRF file in the project.
3. Copy the Jar package
Copy all the Jar packages in the lib directory of the blank instance to the lib directory of the project.
4. Write Action
Generally, Action is the Controller in MVC. Action must inherit the ActionSupport class.
Write various processing methods in it.
5. Configure Struts. xml
/welcome.jsp
Namespace refers to the virtual path. If namespace = "student", the action path is localhost/student/addStudent.
Generally, the default value of extends is struts-default.
In the action tag, this is the address requested by the browser.
Class Attribute refers to the Action that processes the request. Package name + Action class name
The method attribute refers to the method name used to process the request in the Action.
Result is the page to be returned.
The name attribute is used to return the page in result if the string returned by the method matches.
6. wildcard characters
Use wildcards to simplify the action configuration
/student{1}.jsp
/welcome.jsp
Here, the value of the name attribute of the action can have a * sign as a matching sign. The {1} of the method attribute represents the * content.
For example, if the browser requests studentDelete, and the value of the method is Delete, it will be handed over to the Delete method in the Action.
VII. Action receiving Parameters
Parameters of the Action receiving page are very simple.
As long as the attribute declared in the Action and the set and get methods are written, Struts will automatically call the set Method to pass the value to the corresponding attribute because the foreground name is the same as the attribute name.
If the declared attribute in the Action is a class object, as long as the set and get methods are written, the foreground name is the object name. Object attribute. Object To obtain the attribute value.
package com.lubby.action;import java.util.Map;import com.lubby.bean.Student;import com.opensymphony.xwork2.Action;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class Action1 extends ActionSupport{private String name;private int age;private Student stu;public String add(){System.out.println(name());System.out.println(age());System.out.println(stu.getName());System.out.println(stu.getAge());return Action.SUCCESS;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Student getStu() {return stu;}public void setStu(Student stu) {this.stu = stu;}}
Front-end page
VIII. type of result
Result has a type tag. The two most commonly used are dispatcher and redirect.
If no type is specified, the default value is dispatcher.
Difference: dispatcher is a server jump. The address in the client's address bar does not change, and the request and session remain unchanged on the jump page. In fact, the entire ActionContext has not changed.
Redirect can be considered as a client jump, that is, the server tells the client to re-enter the new address. The address in the client's address bar has changed, and the request and session have changed.