在Struts2的Action中取得请求参数值的几种方法

实例:现在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

 

 

 

 

 

把参数放到作用域里面去.
如:action 中: request.setAttribute("参数名","值");
  页面上:  request.getAttribute("参数名");

 

request.getParameter()方法完全可以获得URL参数,检查下是否参数名写错

 

你使用的是不是BaseAction.如果就是server.getRequest().getParameter()

看看你的控件是不是 name 属性!如果id的话就获得不到

方法一:action 定义相同名字的属性,添加这个属性的get和set 方法,然后直接在action 里面拿这个参数的值。
方法二:String param = ServletActionContext.getRequest().getParameter("参数名");

 

先看Action类代码

 

[java] view plain copy
  1. public class TestAction extends ActionSupport {     
  2.     
  3.     private String bookName;     
  4.     private String bookPrice;     
  5.          
  6.     public String getBookName() {     
  7.         return bookName;     
  8.     }     
  9.     
  10.     public void setBookName(String bookName) {     
  11.         this.bookName = bookName;     
  12.     }     
  13.     
  14.     public String getBookPrice() {     
  15.         return bookPrice;     
  16.     }     
  17.     
  18.     public void setBookPrice(String bookPrice) {     
  19.         this.bookPrice = bookPrice;     
  20.     }     
  21.          
  22.          
  23.     public String  execute() throws Exception{     
  24.              
  25.              
  26.         //方式一: 将参数作为Action的类属性,让OGNL自动填充     
  27.               
  28.         System.out.println("方法一,把参数作为Action的类属性,让OGNL自动填充:");     
  29.         System.out.println("bookName: "+this.bookName);     
  30.         System.out.println("bookPrice: " +this.bookPrice);     
  31.              
  32.              
  33.         //方法二:在Action中使用ActionContext得到parameterMap获取参数:     
  34.         ActionContext context=ActionContext.getContext();     
  35.         Map  parameterMap=context.getParameters();     
  36.              
  37.         String bookName2[]=(String[])parameterMap.get("bookName");     
  38.         String bookPrice2[]=(String[])parameterMap.get("bookPrice");     
  39.              
  40.         System.out.println("方法二,在Action中使用ActionContext得到parameterMap获取参数:");     
  41.         System.out.println("bookName: " +bookName2[0]);     
  42.         System.out.println("bookPrice: " +bookPrice2[0]);     
  43.              
  44.              
  45.         //方法三:在Action中取得HttpServletRequest对象,使用request.getParameter获取参数     
  46.         HttpServletRequest request = (HttpServletRequest)context.get(ServletActionContext.HTTP_REQUEST);      
  47.               
  48.         String bookName=request.getParameter("bookName");     
  49.         String bookPrice=request.getParameter("bookPrice");     
  50.              
  51.         System.out.println("方法三,在Action中取得HttpServletRequest对象,使用request.getParameter获取参数:");     
  52.         System.out.println("bookName: " +bookName);     
  53.         System.out.println("bookPrice: " +bookPrice);     
  54.         return SUCCESS;     
  55.              
  56.     }     
  57.     
  58. }    

 

 

总结: 

 

  • 方法一:当把参数作为Action的类属性,且提供属性的getter/setter方法时,xwork的OGNL会自动把request参数的值设置到类属性中,此时访问请求参数只需要访问类属性即可。
  • 方法二:可以通过ActionContext对象Map  parameterMap=context.getParameters();方法,得到请求参数Map,然后通过parameterMap来获取请求参 数。需要注意的是:当通过parameterMap的键取得参数值时,取得是一个数组对象,即同名参数的值的集合。
  • 方法三:通过ActionContext取得HttpServletRequest对象,然后使用request.getParameter("参数名")得到参数值。 

 

Struts2中Action接收参数的方法主要有以下三种:
1.使用Action的属性接收参数:
    a.定义:在Action类中定义属性,创建get和set方法;
    b.接收:通过属性接收参数,如:userName;
    c.发送:使用属性名传递参数,如:user1!add?userName=Magci;
2.使用DomainModel接收参数:
    a.定义:定义Model类,在Action中定义Model类的对象(不需要new),创建该对象的get和set方法;
    b.接收:通过对象的属性接收参数,如:user.getUserName();
    c.发送:使用对象的属性传递参数,如:user2!add?user.userName=MGC;
3.使用ModelDriven接收参数:
    a.定义:Action实现ModelDriven泛型接口,定义Model类的对象(必须new),通过getModel方法返回该对象;
    b.接收:通过对象的属性接收参数,如:user.getUserName();
    c.发送:直接使用属性名传递参数,如:user2!add?userName=MGC;

转载于:https://www.cnblogs.com/plzdaye/p/3961092.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值