Parameter passing means transferring parameters to the program background. The background may do some processing, and then save the content into the database.
There are many methods for passing parameters, as explained one by one.
1. Direct parameter method in Action
There is the following index.jsp file
The code copy is as follows:<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<base href="<%=basePath %>"/>
<title>Insert title here</title>
</head>
<body>
Use the action attribute to receive the parameter <a href="user/user!add?name=a&age=8">Add user</a>
</body>
</html>
For <a></a>, pass two parameters to the program, one is name and the other is age. The configuration in struts.xml is as follows:
Copy the code code as follows:<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="user" extends="struts-default" namespace="/user">
<action name="user">
<result>/user_add_success.jsp</result>
</action>
</package>
</struts>
How should we write our UserAction at this time? Examples are as follows:
The code copy is as follows: package com.bjsxt.struts2.user.action;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
private String name;
private int age;
public String add() {
System.out.println("name=" + name);
System.out.println("age=" + age);
return 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;
}
}
It is very simple, it defines two properties. Note: the set and get methods of these two properties must be written, and the rapid generation method of eclipse is used, which is very simple. In this way, the above program will print the desired result when it is run
name=a and age=8.
The explanation is as follows: First, struts2 will automatically pass parameters, and we do not need to participate in this process; Second, struts pass parameters to set and get methods, not name and age attributes. That is to say, if we modify the name in it to other names, such as username, but the methods are still setName and getName, there is no difference between the implementation of the entire function, it is just a bit awkward; Third, and the most important point is that if there are many attributes, we need a lot of set and get methods, which is very inconvenient, so the following method is extended.
2. Action Add class object method
At this time, the properties in our 1 are all classified into a class, such as User
The code copy is as follows: package com.bjsxt.struts2.user.model;
public class User {
private String name;
private int age;
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;
}
}
This makes writing in Action class much easier
The code copy is as follows: package com.bjsxt.struts2.user.action;
import com.bjsxt.struts2.user.model.User;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
private User user;
public String add() {
System.out.println("name=" + user.getName());
System.out.println("age=" + user.getAge());
return SUCCESS;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
Note: At this time, we do not need to manually generate a User object ourselves. This process is automatically completed by Struts2.
And the url needs to be modified at this time, that is, the <a></a> tag in the index is modified:
The code copy is as follows:<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<base href="<%=basePath %>"/>
<title>Insert title here</title>
</head>
<body>
Use Domain Model to receive parameters <a href="user/user!add?user.name=a&user.age=8">Add user</a>
</body>
</html>
Modify into the above 18-line section.
The above describes two methods of Struts2 parameter passing, the second method is called: DomainModel, domain model. That is, create a new class to store attributes.
Another method is described below, called ModelDriven, model-driven.
It is very similar to the second method, and the others are the same, except that there is a difference between Action and Access. Its Action is as follows:
The code copy is as follows: package com.bjsxt.struts2.user.action;
import com.bjsxt.struts2.user.model.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class UserAction extends ActionSupport implements ModelDriven<User>{
private User user = new User();
public String add() {
System.out.println("name=" + user.getName());
System.out.println("age=" + user.getAge());
return SUCCESS;
}
@Override
public User getModel() {
return user;
}
}
We can see from this that it implements the ModelDriven interface and adopts generic technology. In this way, Struts2 will not automatically instantiate an object, so we can only generate it manually. It overrides the getModel() method of the ModelDriven interface, and its function is to return a class object.
Its access is different from the second (as in the first method):
Copy the code code as follows: Use ModelDriven to receive the parameters <a href="user/user!add?name=a&age=8">Add user</a>
It does not use the user.name method, which is why it is necessary to new an object.
The basic ideological process of this method is: first, the Action parses the url, obtains the parameters, and then enters the Action. It is found that this Action implements a ModelDriven interface. At this time, the getModel method of the ModelDriven interface is called to obtain the class object, and then the set and get methods of this class are called to pass the parameters in.
This method reflects the MVC idea of Struts2, M---Model, V---View, C---Controller, but this method is rarely used, and the second method we use the most is the above.
The above is the entire content of the parameter delivery method in Struts2. I hope it can give you a reference and I hope you can support Wulin.com more.