當前位置:編程學習大全網 - 網站源碼 - struts2 jsp頁面怎麽獲取值

struts2 jsp頁面怎麽獲取值

現在jsp頁面傳遞壹個名為username的參數到action中

壹、通過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:

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

  • 上一篇:濟源市交通航運發展有限公司怎麽樣?
  • 下一篇:微信支付在Android和IOS上表現不壹致 怎麽破
  • copyright 2024編程學習大全網