Action获取表单提交数据
2017-03-14 14:45
204 查看
之前我们web阶段中,提交表单到servlet里面,在servlet里面使用
request对象里面的方法获取,getParameter,getParameterMap
提交表单到action,但是action没有request对象,不能直接使用
request对象
此时我们就要思考怎样才能获取到表单中的信息?Action的确给出了三种获取的方式
例子
Form1DemoAction
struts.xml
控制台也打印出相应的情况
static HttpServletResponse getResponse():获取Web应用的HttpServletResponse对象
static ServletContext getServletContext():获取Web应用的ServletContext对象
static PageContext getPageContext():获取Web应用的PageContext对象
显然我们可以通过获取HttpServletRequest来进行解决
Form2DemoAction
struts.xml
form1.jsp
Form3DemoAction
struts.xml
form1.jsp
使用ServletActionContext类进行操作
END!!!!!!!!!!
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!!!!!!!!!!
相关文章推荐
- ASP.NET MVC中在Action获取提交的表单数据方法总结
- 在Action中获取表单提交数据
- struts2学习(二)—action获取表单提交数据的三种方式
- Action获取表单提交数据+向jsp传递数据【重要】
- ASP.NET MVC中在Action获取提交的表单数据方法总结
- ASP.NET MVC中在Action获取提交的表单数据方法总结 .
- 表单中多个请求参数名字相同,服务器到底获取的是哪个请求参数的值,表单的action和get和post提交方式的关系以及提交数据的不同点
- Strust2 Action获取表单提交数据
- ASP.NET MVC中在Action获取提交的表单数据方法
- 在Action中获取表单提交数据
- ASP.NET MVC中在Action获取提交的表单数据方法总结
- 客户端表单提交数据方式与服务器获取数据
- structs +hibernate 通过Action 将前台数据存入数据库 , 表单提交
- struts2 Action获取表单数据
- javaweb之request通过各种表单提交项收集用户数据和用request获取表单提交数据
- servlet获取表单提交的数据、请求转发、请求包含、请求重定向
- struts2 Action获取表单数据
- servlet获取表单提交的所有数据名称和值
- struts2 Action获取表单数据
- JS自动获取Form表单数据并转换成键值对 ajax提交Form