Strust2 Action获取表单提交数据

  • 以前咱们web阶段中,提交表单到servlet里面,在servlet里面使用 
    request对象里面的方法获取,getParameter,getParameterMaphtml

  • 提交表单到action,可是action没有request对象,不能直接使用 
    request对象java

  • 此时咱们就要思考怎样才能获取到表单中的信息?Action的确给出了三种获取的方式web

action获取表单提交数据主要三种方式

 

<%@ 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来获取的ide

(2).经过getParameters()返回一个包含全部HttpServletrequest参数信息(Map)

用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;
    }
}

 

2.使用ServletActionContext类

  • static HttpServletRequest getRequest():获取Web应用的HttpServletRequest对象
  • static HttpServletResponse getResponse():获取Web应用的HttpServletResponse对象
  • static ServletContext getServletContext():获取Web应用的ServletContext对象
  • static PageContext getPageContext():获取Web应用的PageContext对象 

显然咱们能够经过获取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;
                }
}

 

3.使用接口注入方式

  • 让action实体接口ServletRequestAware 为了获得request对象

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;
    }

}
相关文章
相关标签/搜索