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

}
相關文章
相關標籤/搜索