java action提交表单数据_在Action中获取表单提交数据

-----------------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

注:

152c396c875c550ae5d852575538ec99.png

37051dd454b3dce6c081137163001a91.png

使用 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)访问路径(同上)

注:

9eaa99fa9e51cc231410e14207cf81b3.png

使用 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)访问路径(同上)

注:

5d66092417be7875bdbe1a02744346bb.png

【made by siwuxie095】

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值