stay struts2 The main ways to receive parameters in the framework are attribute driven parameter acquisition 、 Object driven getting parameters 、 There are four ways to get parameters: model driven and collection type , In this chapter, we learn how to get parameters by model driven method ~

Struts2 Model driven approach to get parameters

1、Struts2 Model driven approach to get parameters

You can see from the name that , This approach requires an object model , Use the object model to encapsulate the passed parameters , Basically, this method still uses attribute driven method to obtain parameters , The code steps are as follows :

First step : Create objects

Create a User Class object , The code is as follows :


import java.util.Date;

public class User {
	private String name;
	private Integer age;
	private Date birthday;
	public String getName() {
		return name;
	public void setName(String name) { = name;
	public Integer getAge() {
		return age;
	public void setAge(Integer age) {
		this.age = age;
	public Date getBirthday() {
		return birthday;
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	public String toString() {
		return "User [name=" + name + ", age=" + age + ", birthday=" + birthday + "]";

The second step : establish jsp page

Its front-end code does not need to be in front of the key name of each input box as the object driver gets parameters (ingput Inside the label name Property value ) Add the class name , The code is shown below :

<form action="${pageContext.request.contextPath}/api/DemoApi6" method="post">
<label> full name :<input type="text" name="username" ></label><br/>
<label> Age :<input type="number"  min="18" max="90" name="age" ></label><br/>
<label> Birthday :<input type="date"  name="birthday" ></label><br/>
<input type="submit" value="OK">

The third step : establish Action Method

The code is as follows :

// struts2 Use model driven to get parameters 
public class DemoApi6 extends ActionSupport implements ModelDriven<User>{
	//  Prepare one user object ;
	private User user = new User();

	public String action_name() throws Exception {
		System.out.println(" name :" + user.getName() + "; Age " + user.getAge() + "; Birthday :" + user.getBirthday());
		return SUCCESS;

	public User getModel() {
		return user;

Step four : The configuration file

<!--  Model driven getting parameters  -->
   <action name="DemoApi6" class="" method="action_name">
      <result name="success" type="dispatcher">/form3.jsp</result>

Step five :Demo test

Input parameters :
 Input parameter picture description
Show results :
 Show results
success ~

summary :

(1) In the use of User Class object must pay attention to the property name and jsp The key name of the input box on the page (name value ) Correspond well ;
(2) Model driven parameters are obtained in struts2 Framework projects are more commonly used , It should be more convenient ~

struts2 Several ways to get parameters are as follows :
Attribute driven method to get parameters :

Object driven method to get parameters :

Model driven approach to get parameters :

List Collection encapsulation passing get parameters :

Map Collection encapsulation passing get parameters :