以前咱们web阶段中,提交表单到servlet里面,在servlet里面使用
request对象里面的方法获取,getParameter,getParameterMaphtml
提交表单到action,可是action没有request对象,不能直接使用
request对象java
此时咱们就要思考怎样才能获取到表单中的信息?Action的确给出了三种获取的方式web
<%@ 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>
这个ActionContext类对象不是new出来的 而是经过类ActionContext中的一个静态方法getContext来获取的ide
用getParameters返回一个Map<String,Parameter>
属性的表单post
例子ui
Form1DemoActionthis
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; } }
显然咱们能够经过获取HttpServletRequest来进行解决spa
Form2DemoActioncode
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; } }
Form3DemoActionorm
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; } }