Action获取表单提交数据
来源:互联网 发布:安卓远程录音源码 编辑:程序博客网 时间:2024/05/18 01:31
之前我们web阶段中,提交表单到servlet里面,在servlet里面使用
request对象里面的方法获取,getParameter,getParameterMap提交表单到action,但是action没有request对象,不能直接使用
request对象此时我们就要思考怎样才能获取到表单中的信息?Action的确给出了三种获取的方式
action获取表单提交数据主要三种方式
form.jsp类的代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></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>
1.使用ActionContext类
(1).获取ActionContext对象
这个ActionContext类对象不是new出来的 而是通过类ActionContext中的一个静态方法getContext来获取的
(2).通过getParameters()返回一个包含所有HttpServletrequest参数信息(Map)
用getParameters返回一个Map<String,Parameter>
属性的表单
例子
Form1DemoAction
public class Form1DemoAction extends ActionSupport{ @Override public String execute() throws Exception { //1 获取ActionContext对象 ActionContext context=ActionContext.getContext(); context.getParameters(); //获取Map属性表单 Map<String, Parameter> map=context.getParameters(); //遍历Map Set<String> keys=map.keySet(); for(String s:keys) { System.out.println(map.get(s)); } return NONE; }}
struts.xml
<action name="form1" class="cn.itcast.form.Form1DemoAction"></action>
控制台也打印出相应的情况
2.使用ServletActionContext类
- static HttpServletRequest getRequest():获取Web应用的HttpServletRequest对象
- static HttpServletResponse getResponse():获取Web应用的HttpServletResponse对象
- static ServletContext getServletContext():获取Web应用的ServletContext对象
- static PageContext getPageContext():获取Web应用的PageContext对象
显然我们可以通过获取HttpServletRequest来进行解决
Form2DemoAction
public class Form2DemoAction extends ActionSupport { @Override public String execute() throws Exception { HttpServletRequest request=ServletActionContext.getRequest(); 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 name="form2" class="cn.itcast.form.Form2DemoAction"></action>
form1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body> <form action="${pageContext.request.contextPath}/form2.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>
3.使用接口注入方式
- 让action实体接口ServletRequestAware 为了得到request对象
Form3DemoAction
public class Form3DemoAction extends ActionSupport implements ServletRequestAware{ private HttpServletRequest request; @Override public void setServletRequest(HttpServletRequest request) { this.request=request; } @Override public String execute() throws Exception { 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 name="form3" class="cn.itcast.form.Form3DemoAction"></action>
form1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body> <form action="${pageContext.request.contextPath}/form3.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>
在action操作域对象
- request,session,servletContext域对象
- 使用ServletActionContext类进行操作
END!!!!!!!!!!
1 0
- Action获取表单提交数据
- 在Action中获取表单提交数据
- Action获取表单提交数据+向jsp传递数据【重要】
- 获取提交表单的action 提交Url
- struts2 Action获取表单数据
- struts2 Action获取表单数据
- struts2 Action获取表单数据
- struts2 Action获取表单数据
- struts2 Action获取表单数据
- 《纪录遗忘》form表单action提交数据
- ASP.NET MVC中在Action获取提交的表单数据方法总结
- ASP.NET MVC中在Action获取提交的表单数据方法总结 .
- ASP.NET MVC中在Action获取提交的表单数据方法总结
- ASP.NET MVC中在Action获取提交的表单数据方法
- struts2学习(二)—action获取表单提交数据的三种方式
- 提交表单--获取表单中的数据值
- (转)struts2 Action获取表单数据
- 在Action中获取表单数据
- JavaScript:Json数据处理
- 排序算法(二)选择类排序:简单选择排序,堆排序,锦标赛排序
- ScrollView嵌套ListView的bug问题解决
- MySQL自定义函数用法详解-复合结构自定义变量/流程控制
- 分糖果
- Action获取表单提交数据
- 数组面向过程与面向对象的基本操作
- 标签个数与长度的验证
- Hadoop入门介绍
- SVN文件误删,再检出报红色X号
- Java 集合框架
- React 避免重渲染
- trait与policy模板技术
- Linux中服务器软件为什么需要编译安装