struts2学习(二)—action获取表单提交数据的三种方式
2017-03-02 19:52
811 查看
本文内容主要来自创智博客学习视频的整理
http://v.itcast.cn/course/212.html
action获取表单提交数据的三种方式:
(1)使用actionContext类
(2)使用ServletActionContext类
(3)使用接口注入的方式
,可得网页,在username,password,address中输入相应内容,点“提交”,就可获取到网页上输入的值。
,可得网页,在username,password,address中输入相应内容,点“提交”,就可获取到网页上输入的值。
![](http://img.blog.csdn.net/20170302195151277?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbHZ5dWFuMzAyNzY=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
使用actionContext类获取表单数据
使用ServletActionContext类
使用接口注入的方式
其他代码部分
1 代码架构
2 struts文件
http://v.itcast.cn/course/212.html
action获取表单提交数据的三种方式:
(1)使用actionContext类
(2)使用ServletActionContext类
(3)使用接口注入的方式
1.使用actionContext类获取表单数据
在浏览器中输入http://127.0.0.1:8080/struts2All/form1.jsp,可得网页,在username,password,address中输入相应内容,点“提交”,就可获取到网页上输入的值。
import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class Form1DemoAction extends ActionSupport { @Override public String execute() throws Exception { // 第一种方式:使用ActionContext类获取表单数据 // 1.获取ActionContext对象 ActionContext context = ActionContext.getContext(); // 2,获取表单数据,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; } }
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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>
2.使用ServletActionContext类
在浏览器中输入http://127.0.0.1:8080/struts2All/form1.jsp,可得网页,在username,password,address中输入相应内容,点“提交”,就可获取到网页上输入的值。
package form; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class Form2DemoAction extends ActionSupport { public String execute() throws Exception{ //1.使用ServletActionContext类获取请求 HttpServletRequest request = ServletActionContext.getRequest(); String username = request.getParameter("username"); String password = request.getParameter("password"); String address = request.getParameter("address"); System.out.println("username:"+username+" password:"+password+" address:"+address); return NONE; } }
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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.使用接口注入的方式
继承ServletRequestAware,request可直接注入进来,然后从中获取数据。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; public String execute() throws Exception{ String username = request.getParameter("username"); String password = request.getParameter("password"); String address = request.getParameter("address"); System.out.println("username:"+username+" password:"+password+" address:"+address); return NONE; } @Override public void setServletRequest(HttpServletRequest request) { this.request = request; } }
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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="submit"/> </form> </body> </html>
4.其他代码部分
4.1 代码架构
4.2 struts文件
<?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="demo2" extends="struts-default" namespace="/"> <action name="form1" class="form.Form1DemoAction"> </action> <action name="form2" class="form.Form2DemoAction"> </action> <action name="form3" class="form.Form3DemoAction"> </action> </package> </struts>
使用actionContext类获取表单数据
使用ServletActionContext类
使用接口注入的方式
其他代码部分
1 代码架构
2 struts文件
相关文章推荐
- action里获取表单数据的三种方式
- struts2获取表单数据的三种方式
- 表单中多个请求参数名字相同,服务器到底获取的是哪个请求参数的值,表单的action和get和post提交方式的关系以及提交数据的不同点
- 18.01.11,web学习第四十天,还有一年,努力吧青年 struts2第二天 action创建+result处理+获取数据方式
- Django框架学习笔记(13.获取单表单数据的三种方式)
- Struts2中表单与Action传递数据三种方式
- Struts2中表单与Action传递数据三种方式
- Action 中获取表单数据的三种方式
- struts2 lesson one: 的action特征,三种创建方法,action接收表单数据的三种访问,读取方式
- Action获取表单数据的三种方式
- Struts2 action获取数据的三种方式
- struts2——学习笔记1——struts2封装表单数据方式(三种)
- Struts2学习之三种获得表单数据方式
- Struts2中表单与Action传递数据三种方式
- 【JavaEE笔记】Struts2 (二) 结果页面配置 、获取表单提交数据 、提供获取表单数据方式 、获取数据封装到集合中
- Struts2中表单与Action传递数据三种方式
- struts2获取表单提交数据的三种方法
- Struts2中表单与Action传递数据三种方式
- Action 中获取表单数据的三种方式
- Struts2中表单与Action传递数据三种方式