Struts2接收前端参数的三种方法,struts2前端
分享于 点击 45461 次 点评:60
Struts2接收前端参数的三种方法,struts2前端
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("bookName: "+this.bookName);
System.out.println("bookPrice: " +this.bookprice);
//方法二:在Action中使用ActionContext得到parameterMap获取参数:
ActionContext context=ActionContext.getContext();
Map<K, V> parameterMap=context.getParameters();
String bookName2[]=parameterMap.get("bookName");
String bookPrice2[]=(String[])parameterMap.get("bookprice");
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("bookName: " +bookName);
System.out.println("bookPrice: " +bookPrice);
return SUCCESS;
}
}
相关文章
- 暂无相关文章
用户点评