Action获取表单提交数据的三种方式

(1)使用ActionContext类

范例:form.jsp

usename:

password:

配置struts.xml

使用第一种方法:使用ActionConext来获取

public class FormAcrion extends ActionSupport{

ActionContext context = ActionContext.getContext();

Map map = context.getParamters();

Set keys = map.keySet();

for(String key:keys){

Object[] obj = (Object[])map.get(key);

System.out.println(Arrays.toString(obj));

}//valuesh数组形式,因为输入项里面的选项可能是复选框情况

使用第二种方法获取表单数据

使用ServletActionContext

`static HttpServletRequest getReques();//获取web应用的HttpServletRequest对象

`static HttpServletResponse getResponse();//获取web应用的HttpServletResponse对象

`static ServletContext getServletContext();//获取web'应用的ServletContext对象

`static PageContext getPageContext();//获取web应用的pageContext对象

(1)调用类里的静态方法,得到request对象

HttpServletRequest request = ServletActionContext().getRequest();

String username= request.getParameter("username");

String password = request.getParameter("password");


使用第三种方式(接口注入)了解

public class demo extends ActionSuppoert implements ServletRequestAware{

private HttpServletRequest request;

@Override

public void setServletRequest(HttpServletRequest request){

this.request = request;

}

public String execute() throws Exception{

request.getParameter("username");

}

}

你可能感兴趣的:(Action获取表单提交数据的三种方式)