`
Riddick
  • 浏览: 633589 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

Struts2请求参数的接收

 
阅读更多

1)--采用基本类型接收请求参数(get/post)


在Action类中定义与请求参数同名的属性,struts2便能自动接收请求参数并赋予给同名属性。

请求路径: http://localhost:8080/test/view.action?id=78

public class ProductAction {
      private Integer id;
      public void setId(Integer id) {//struts2通过反射技术调用与请求参数同名的属性的setter方法来获取请求参数值
             this.id = id;
      }
      public Integer getId() {return id;}
}

 
2)--采用复合类型接收请求参数


请求路径: http://localhost:8080/test/view.action?product.id=78

 

public class ProductAction {
    private Product product;
    public void setProduct(Product product) { this.product = product; }
    public Product getProduct() {return product;}
}

 

Struts2首先通过反射技术调用Product的默认构造器创建product对象,然后再通过反射技术调用product中与请求参数同名的属性的setter方法来获取请求参数值。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics