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