您的位置:首页 > 其它

Action获取表单提交数据

2017-03-14 14:45 204 查看
之前我们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!!!!!!!!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: