您的位置:首页 > 编程语言 > Java开发

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

2014-01-14 13:41 525 查看
public class GetRequestParameterAction extends ActionSupport {

private String bookName;
private String bookPrice;

public String getBookName() {
return bookName;
}

public void setBookName(String bookName) {
this.bookName = bookName;
}

public String getBookPrice() {
return bookPrice;
}

public void setBookPrice(String bookPrice) {
this.bookPrice = bookPrice;
}

public String  execute() throws Exception{

//方式一: 将参数作为Action的类属性,让OGNL自动填充

System.out.println("方法一,把参数作为Action的类属性,让OGNL自动填充:");
System.out.println("bookName: "+this.bookName);
System.out.println("bookPrice: " +this.bookPrice);

//方法二:在Action中使用ActionContext得到parameterMap获取参数:
ActionContext context=ActionContext.getContext();
Map  parameterMap=context.getParameters();

String bookName2[]=(String[])parameterMap.get("bookName");
String bookPrice2[]=(String[])parameterMap.get("bookPrice");

System.out.println("方法二,在Action中使用ActionContext得到parameterMap获取参数:");
System.out.println("bookName: " +bookName2[0]);
System.out.println("bookPrice: " +bookPrice2[0]);

//方法三:在Action中取得HttpServletRequest对象,使用request.getParameter获取参数
HttpServletRequest request = (HttpServletRequest)context.get(ServletActionContext.HTTP_REQUEST);

String bookName=request.getParameter("bookName");
String bookPrice=request.getParameter("bookPrice");

System.out.println("方法三,在Action中取得HttpServletRequest对象,使用request.getParameter获取参数:");
System.out.println("bookName: " +bookName);
System.out.println("bookPrice: " +bookPrice);
return SUCCESS;

}

}
http://lkf520java.iteye.com/blog/565989
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: