Action擷取表單提交資料

來源:互聯網
上載者:User

之前我們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。。。。。。。。。。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.