(1)使用ActionContext类
范例:form.jsp
<form action="${pageContext.request.contextPath }/form1.action" method="post">
usename:<input type="text" nam=“username"/>
password:<input type="text" name="password"/>
<input type="submit" value="提交"/>
</form>
配置struts.xml
<struts>
<package name="demo" namespace="/" extends="struts-default">
<action name="form1" class="包.类"></action>
</package>
</struts>
使用第一种方法:使用ActionConext来获取
public class FormAcrion extends ActionSupport{
ActionContext context = ActionContext.getContext();
Map<String,Object> map = context.getParamters();
Set<String> 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");
}
}