-----------------siwuxie095
在 Action 中获取表单提交数据
1、之前的 Web 阶段是提交表单到Servlet,在其中使用Request 对象
的方法获取数据
2、Struts2 是提交表单到Action,但 Action 没有Request对象,不能
直接使用 Request 对象获取数据
「可以间接使用 Request 对象获取数据」
3、Action 获取表单提交数据主要有三种方式:
(1)使用ActionContext类
(2)使用ServletActionContext类
(3)使用ServletRequestAware接口
「底层封装的依然是 Servlet 的 API」
使用 ActionContext 类获取
1、使用getParameters()方法获取表单提交数据,因为它不是静态
方法,所以需要先创建ActionContext 对象
注意:
ActionContext 对象不是 new 出来的,而是通过 ActionContext 类
直接调用静态方法getContext()返回的
2、具体过程
(1)创建表单,提交表单到 Action
(2)在 Action 中使用 ActionContext 类获取表单提交数据
3、具体实现
(1)编写页面
form.jsp:
pageEncoding="UTF-8"%>
/p>
表单username:
password:
address:
(2)编写 Action
Form1Action.java:
packagecom.siwuxie095.action;
importjava.util.Arrays;
importjava.util.Map;
importjava.util.Set;
importcom.opensymphony.xwork2.ActionContext;
importcom.opensymphony.xwork2.ActionSupport;
/**
*使用ActionContext类获取
*/
public classForm1Action extendsActionSupport {
@Override
publicString execute() throwsException {
ActionContext context=ActionContext.getContext();
/*
*调用getParameters()方法,返回值是Map类型,创建以接收
*
* Key即表单中输入项的name属性值,Value即输入的值
*
*/
Map map=context.getParameters();
//得到所有的Key值
Set keys=map.keySet();
//根据Key得到Value
for(String key : keys) {
//数组形式:输入项可能有复选框,传过来多个值
Object[] obj=(Object[]) map.get(key);
System.out.println(Arrays.toString(obj));
}
returnNONE;
}
}
(3)配置 Action
struts.xml:
/p>
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
(4)访问路径
1)http://localhost:8080/工程名/form.jsp
注:
使用 ServletActionContext 类获取
1、直接调用ServletActionContext类中的静态方法,
获取Request 对象,进而通过getParameter()方法
获取表单提交数据
2、具体实现
(1)编写页面(同上 form.jsp)
(2)编写 Action
Form2Action.java:
packagecom.siwuxie095.action;
importjavax.servlet.http.HttpServletRequest;
importorg.apache.struts2.ServletActionContext;
importcom.opensymphony.xwork2.ActionSupport;
/**
*使用ServletActionContext类获取
*/
public classForm2Action extendsActionSupport {
@Override
publicString execute() throwsException {
//先使用ServletActionContext类调用静态方法获取Request对象
HttpServletRequest request=ServletActionContext.getRequest();
//再调用Request对象的方法获取表单提交数据
String username=request.getParameter("username");
String password=request.getParameter("password");
String address=request.getParameter("address");
System.out.println(username+"-"+password+"-"+address);
returnNONE;
}
}
(3)配置 Action
struts.xml:
/p>
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
(4)访问路径(同上)
注:
使用 ServletRequestAware 接口获取
1、让 Action 实现ServletRequestAware接口,获取
Request 对象,进而通过getParameter()方法获取表
单提交数据
2、具体实现
(1)编写页面(同上 form.jsp)
(2)编写 Action
Form3Action.java:
packagecom.siwuxie095.action;
importjavax.servlet.http.HttpServletRequest;
importorg.apache.struts2.interceptor.ServletRequestAware;
importcom.opensymphony.xwork2.ActionSupport;
/**
*使用ServletRequestAware接口获取(实现该接口)
*/
public classForm3Action extendsActionSupport implementsServletRequestAware {
privateHttpServletRequest request;
//重写ServletRequestAware接口的setServletRequest()方法获取Request对象
@Override
publicvoidsetServletRequest(HttpServletRequest request) {
//使用ServletRequestAware接口注入Request对象
this.request=request;
}
@Override
publicString execute() throwsException {
String username=request.getParameter("username");
String password=request.getParameter("password");
String address=request.getParameter("address");
System.out.println(username+"-"+password+"-"+address);
returnNONE;
}
}
(3)配置 Action
struts.xml:
/p>
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
(4)访问路径(同上)
注:
【made by siwuxie095】