執行個體:現在jsp頁面傳遞一個名為username的參數到action中
url: http://localhost:8080/StudentSystem/role_list.action?username=qk12333.com
一、通過get set方法擷取
在對應的action類中定義同名變數,並產生set get方法,那麼參數將會自動擷取值
String username;
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
System.out.println(username);//結果為qk12333.com
二、通過ServletActionContext擷取//匯入import org.apache.struts2.ServletActionContext;
HttpServletRequest reqeust= ServletActionContext.getRequest();
String username=reqeust.getParameter("username");//字串
//url: http://localhost:8080/StudentSystem/role_list.action?username=qk12333.com&username=qk12333.com
String[] username=reqeust.getParameterValues("username");//字串數組
System.out.println(username);//結果為qk12333.com System.out.println(username[0]);//結果為 qk12333.com
三、通過ActionContext擷取//匯入import com.opensymphony.xwork2.ActionContext;
ActionContext context = ActionContext.getContext();
Map params = context.getParameters();
String[] username=(String[])params.get("username");
//ActionContext擷取到一個對象如object或String[] System.out.println(username[0]);//結果為 qk12333.com