Action 中获取表单数据的三种方式
(尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/53138905 冷血之心的博客)
Action 中获取表单提交数据的三种方式:
(1)使用ActionContext类来获取。
(2)使用ServletActionContext类获取。
(3)使用接口注入的方式获取。
先来说说获取表单数据的直接方法:
1、在Web开发阶段,我们提交表单到Servlet里边,在Servlet里面使用request对象的方法来获取提交数据,如getParameter,getParameterMap。
2、现在我们用Action代替了Servlet,所以提交表单到了Action中,但是Action中没有request对象,所以不能直接使用request对象。
下边分别对三种方式加以阐述:
(1)使用ActionContext类来获取。
- 创建表单,提交表单数据到action中
- 在action中使用ActionContext获取数据。
代码如下:
Form1DemoAction.java
package form; import java.util.Arrays;
import java.util.Map;
import java.util.Set; import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport; public class Form1DemoAction extends ActionSupport { @Override
public String execute() throws Exception{
//获取表单数据的第一种方法:ActionContext类获取
/**
* 1、获取ActionContext对象
* 2、调用方法得到表单数据
*/
ActionContext context = ActionContext.getContext();
//key是表单输入的name属性值,value是输入的值
Map<String, Object> map = context.getParameters(); Set<String> keys = map.keySet();
for(String key:keys){
Object[] obj = (Object[]) map.get(key);
System.out.println(Arrays.toString(obj));
} return NONE;
}
}
表单form1.jsp如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'form1.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body> <form action="${pageContext.request.contextPath}/form1.action" method="post">
username: <input type="text" name="username"/> <br>
password: <input type="text" name="password"/> <br>
address: <input type="text" name="address"/> <br>
<input type="submit" value="提交"> </form> </body>
</html>
配置文件struts.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 获取表单提交的数据 -->
<package name="form" extends="struts-default" namespace="/">
<action name="form1" class="form.Form1DemoAction"> </action> </package> </struts>
在web.xml设置拦截器:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Test_Struts2</display-name> <filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
分析:我们首先写了一个表单,提交数据指向了form1.action。在配置文件中,将form1.action指向了我们自定义的action类
Form1DemoAction。当我们访问form1.jsp并且提交了表单数据后,Form1DemoAction类中的execute()将会执行,然后就可以得
到表单提交的数据了。
(2)使用ServletActionContext类获取。
Form2DemoAction.java如下:
package form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class Form2DemoAction extends ActionSupport { @Override
public String execute() throws Exception{
//获取表单数据的第二种方法:ServletActionContext类获取 //1、使用ServletActionContext获取request对象。
HttpServletRequest request = ServletActionContext.getRequest(); //2、调用request里边的方法得到结果
String username = request.getParameter("username");
String password = request.getParameter("password");
String address = request.getParameter("address"); System.out.println(username+":"+password+":"+address);
return NONE;
}
}
其中,在struts.xml我们需要配置再一个<action>,如下:
<action name="form2" class="form.Form2DemoAction"> </action>
在表单中,我们使用如下语句指向了form2.action
action="${pageContext.request.contextPath}/form2.action"
(3)使用接口注入的方式获取。
- 让action实现接口,得到request对象
Form3DemoAction.java
package form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
public class Form3DemoAction extends ActionSupport implements ServletRequestAware {
private HttpServletRequest request;
@Override
public String execute() throws Exception{
//获取表单数据的第三种方法:使用接口注入方法来获取
//2、调用request里边的方法得到结果
String username = request.getParameter("username");
String password = request.getParameter("password");
String address = request.getParameter("address");
System.out.println(username+":"+password+":"+address);
return NONE;
}
@Override
public void setServletRequest(HttpServletRequest request) {
//1、得到request对象
this.request=request;
}
}
其中,在struts.xml我们需要配置再一个<action>,如下:
<action name="form3" class="form.Form3DemoAction"> </action>
在表单中,我们使用如下语句指向了form2.action
action="${pageContext.request.contextPath}/form3.action"
好了,以上就是Struts2中action获取表单数据的三种方式,其中,常用的是通过ActionContext和ServletActionContext来
获取数据。使用接口注入的方法不常用。
如果对你有帮助,记得点赞哈~欢迎大家关注我的博客,随时加群交流哦~
Action 中获取表单数据的三种方式的更多相关文章
- Action获取表单数据的三种方式
1.使用ActionContext类获取 示例 获取用户提交的用户名和密码 jsp页面 action中的java代码 2.使用ServletActionContext类获取 jsp页面 Java代码 ...
- Python Django 获取表单数据的三种方式
# In viewsdef zbsservice(request): #返回一个列表 v1 = models.Business.objects.all() # .value返回一个字典 v2 = mo ...
- Django - 获取表单数据的三种方式
1.query set 对象 2.字典 3.query set 元组 备注:对象通过 ”对象.列名"方式访问,元组通过“对象.索引”方式访问.
- Day20-单表中获取表单数据的3种方式
1. 搭建环境请参考:http://www.cnblogs.com/momo8238/p/7508677.html 2. 创建表结构 models.py from django.db import m ...
- strus2中获取表单数据 两种方式 属性驱动 和模型驱动
strus2中获取表单数据 两种方式 属性驱动 和模型驱动 属性驱动 /** * 当前请求的action在栈顶,ss是栈顶的元素,所以可以利用setValue方法赋值 * 如果一个属性在对象栈,在页面 ...
- 在Action中获取表单提交数据
-----------------siwuxie095 在 Action 中获取表单提交数据 1.之前的 Web 阶段是提交表单到 Servlet,在其中使用 Request 对象 的方法获取数据 2 ...
- Action中取得request,session的四种方式
Action中取得request,session的四种方式 在Struts2中,从Action中取得request,session的对象进行应用是开发中的必需步骤,那么如何从Action中取得这些对象 ...
- Struts2中获取HttpServletRequest,HttpSession等的几种方式
转自:http://www.kaifajie.cn/struts/8944.html package com.log; import java.io.IOException; import java. ...
- 转 Velocity中加载vm文件的三种方式
Velocity中加载vm文件的三种方式 velocitypropertiespath Velocity中加载vm文件的三种方式: 方式一:加载classpath目录下的vm文件 Prope ...
随机推荐
- 直接访问实例变量 VS 通过点语法访问实例变量
直接访问实例变量,不会经过 OC 的方法派发机制,速度比较块.会直接访问对象的实例变量对应的内存. 直接访问实例变量,不会调用"设置方法".绕过了相关属性对应的"内存管理 ...
- Python小白学习之路(二十)—【打开文件的模式二】【文件的其他操作】
打开文件的模式(二) 对于非文本文件,我们只能使用b模式,"b"表示以字节的方式操作(而所有文件也都是以字节的形式存储的,使用这种模式无需考虑文本文件的字符编码.图片文件的jgp格 ...
- 在VMware 14中安装Centos7
在VMware 14中安装Centos7 一.安装前准备 安装VMware14.1 Centos7 64位镜像下载 在VMware中安装Centos7的步骤为: 1.创建虚拟机 创建虚拟机有两种方式: ...
- Yarn 和 Npm 命令行切换 摘录
原文作者: @Gant Laborde原文地址: https://shift.infinite.red/np...中文翻译: @文蔺译文地址:http://www.wemlion.com/2016/n ...
- tensorflow进阶篇-4(损失函数1)
L2正则损失函数(即欧拉损失函数),L2正则损失函数是预测值与目标函数差值的平方和.L2正则损失函数是非常有用的损失函数,因为它在目标值附近有更好的曲度,并且离目标越近收敛越慢: # L = (pre ...
- spring自定义注解拦截器的配置
1.创建注解文件 (文件格式为注解) 这里面什么都不需要写 文件名就是注解名称 如下 是@anno package com.ABC123.anno; import java.lang.annotati ...
- Oracle 中分组排序取值的问题
整理一下排序: 建表语句:create table EXAM( name VARCHAR2(32), subject VARCHAR2(32), score INTEGER)数据:IN ...
- Eclipse删除switch workspace下多余的workspace
第一步:修改org.eclipse.ui.ide.prefs 文件 打开Eclipse目录的\configuration\.settings目录,找到org.eclipse.ui.ide.prefs ...
- javascript技巧总结
1.删除前后空格 String.prototype.trim = function () { return this.replace(/(^[ | ])|([ | ]$)/g, "" ...
- Pycharm 问题:Clear Read-Only Status
用的是ubuntu系统,一直在普通用户模式下打开Git下建的项目,今天运行神经网络程序时,由于有一个cudnn错误,必须要在sudo模式下才不会报错,所以用sudo试着打开了pycharm,发现是完全 ...