Several Methods for getting parameter values from request by struts2 action
Using ajax in the jquery framework, you can easily pass parameters to the background. taking $. post as an example, the parameters can be strings and key-value pairs in two ways:
$. Post (url, "name = aty & age = 25") and $. post (url, {"name": "aty", "age": 25 }). Obviously, json is more important than String concatenation.
. For background actions, these two methods are equivalent, and jquery also provides param, serialize, and serialize to implement json and url
Conversion of parameter strings. Let's take a look at how action receives the parameter values in the http request.
We use http: // 127.0.0.1: 8080/demo/index. jsp? Name = aty & age = 20 as an example. Obtain the request parameter value in struts2 action. In general
There are also two ways: the first is to define a variable with the same name in action and provide the get/set method.
public class DemoAction{private String name;private int age;public String getName(){return this.name;}public void setName(String name){this.name = name;}public int getAge(){return this.age;}public void setName(int age){this.age = age;}}
The second method is to manually obtain HttpServletRequest and then call request. getParameter ()
public class DemoAction{public String execute(){HttpServletRequest request = ServletActionContext.getRequest();String name = request.getParameter("name");String age = request.getParameter("age");}}
What are the differences between the two methods? Obviously, it is the difference between a member variable and a local variable. We know that an action can define multiple public methods to process different foreground requests. If the same request parameter is used by multiple methods, the first method is applicable. If a request parameter is used by only one method, the second method is suitable. The principle is to ensure that the same parameter name appears only once in the action Code (avoid repetition), and the variable scope should be as small as possible (Code cohesion ).
To encapsulate http Request Parameters to object classes, see the struts2 model driver http://blog.csdn.net/li_tengfei/article/details/6098145. The following describes how to encapsulate parameters in Map and List.
public class DemoAction{private Map
requestMap = new HashMap
();private List
requestList = new ArrayList
();}
Js encapsulates parameters into list
var params = {}; params["requestList[0].id"] = $("#person_id").attr("value"); params["requestList[0].username"] = "aty"; params["requestList[0].password"] = "123"; params["requestList[0].age"] = 25; params["requestList[0].address"] = ""; $.post(url,params);
Js encapsulates parameters into map
var params = {}; params["requestMap.id"] = $("#person_id").attr("value"); params["requestMap.username"] = "aty"; params["requestMap.password"] = "123"; params["requestMap.age"] = 25; params["requestMap.address"] = ""; $.post(url,params);
We can see that using Map to receive http request parameters is no different from using the entity class, and the methods in js and java are the same.