用户登录
用户注册

分享至

struts2 action中获取jsp页面的参数的方法

  • 作者: 亿万精zi
  • 来源: 51数据库
  • 2021-06-26

 

实例:现在页面传递一个名为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

 

摘自:黑鹰的专栏

软件
前端设计
程序设计
Java相关