本文内容主要来自创智博客学习视频的整理
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>