歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Struts2 action中獲取jsp頁面的參數的方法

Struts2 action中獲取jsp頁面的參數的方法

日期:2017/3/1 10:28:55   编辑:Linux編程

實例:現在jsp頁面傳遞一個名為username的參數到action中

url: http://localhost:8080/StudentSystem/role_list.action?username=1321312

一、通過get set方法獲取

在對應的action類中定義同名變量,並生成set get方法,那麼參數將會自動獲取值

String username;

public String getUsername()

{

return username;

}

public void setUsername(String username)

{

this.username = username;

}

System.out.println(username);//結果為1321312

二、通過ServletActionContext獲取//導入import org.apache.struts2.ServletActionContext;

HttpServletRequest reqeust= ServletActionContext.getRequest();

String username=reqeust.getParameter("username");//字符串

//url: http://localhost:8080/StudentSystem/role_list.action?username=1321312&username=34343

String[] username=reqeust.getParameterValues("username");//字符串數組

System.out.println(username);//結果為1321312

System.out.println(username[0]);//結果為1321312

三、通過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]);//結果為1321312

Copyright © Linux教程網 All Rights Reserved