Parameter passing will be transferred to the program background, the background may do some processing, and then the content into the database click!
There are many ways to pass parameters, as described below.
1. Direct parameter method in action
Like the index.jsp file.
Copy Code code 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 ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb18030 "/>
<base href= "<%=basepath%>"/>
<title>insert title here</title>
<body>
Receive parameters using the Action property <a href= "User/user!add?name=a&age=8" > Add user </a>
</body>
For <a></a>, pass two parameters to the program, one is name, one is age, the configuration in Struts.xml is as follows:
Copy 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" class= "com.bjsxt.struts2.user.action.UserAction" >
<result>/user_add_success.jsp</result>
</action>
</package>
</struts>
How should our useraction be written? Examples are as follows:
Copy Code code 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;
}
}
Quite simply, there are two attributes defined, note: Both the set and get methods of these properties must be written, and can be easily generated using Eclipse's quick-build approach. So the above program will print out the desired result at run time
Name=a and Age=8.
It is described as follows: First, the STRUTS2 will automatically pass the parameter, this process does not need our participation; second, struts passes the arguments against the set and get methods, not the name and age attributes, that is, if we modify the name to be other names, such as username, but the method is still setname and getname, for the implementation of the entire function there is no difference, just a bit awkward; third, and most importantly, if there are many attributes, then we need a lot of set and get methods, It was very inconvenient, so the lure stretched out the following way.
2, Action Add Class object method
This time, the attributes in our 1 are attributed to a class, such as user
Copy Code code 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;
}
}
So it's a lot easier to type in the action class.
Copy Code code 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, this process is automatically completed by STRUTS2.
And the URL at this time also need to make a change, that is, the <a></a> tags in index to make the following changes:
Copy Code code 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 ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb18030 "/>
<base href= "<%=basepath%>"/>
<title>insert title here</title>
<body>
Use domain model to receive parameters <a href= "User/user!add?user.name=a&user.age=8" > Add Users </a>
</body>
Modify to the above 18 lines section.
The above illustrates two methods of Struts2 parameter passing, in which the second method is called: Domainmodel, domain model. is to create a new class that holds the property.
The other method, described below, is called: Modeldriven, model-driven.
It's very similar to the second method, and the other is the same, just the action and access difference, and its action is as follows:
Copy Code code 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 it that it implements the Modeldriven interface and uses the generic technology. In this way Struts2 does not automatically instantiate an object, so only we can build it manually. It has overridden 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 Code code as follows:
Use Modeldriven to receive parameters <a href= "User/user!add?name=a&age=8" > Add Users </a>
It doesn't take the user.name approach, which is why you have to have a new object.
The basic idea of this approach is: first action to parse the URL, get the parameters, and then enter the action, found that this action implements a Modeldriven interface, this time call the Modeldriven interface Getmodel method, Gets the object of the class, and then calls the set and get methods of the class, passing the arguments in.
This way embodies the Struts2 MVC idea, M----model,v----view,c----Controller, but this way is rarely used, we use the most or the second way above.
The above is the parameters of the Struts2 in all the contents of the method, I hope to give you a reference, but also hope that we support the cloud habitat community.