1. 程式人生 > >Action 中獲取表單資料的三種方式

Action 中獲取表單資料的三種方式

Action 中獲取表單提交資料的三種方式:

(1)使用ActionContext類來獲取。

(2)使用ServletActionContext類獲取。

(3)使用介面注入的方式獲取。

先來說說獲取表單資料的直接方法:

1、在Web開發階段,我們提交表單到Servlet裡邊,在Servlet裡面使用request物件的方法來獲取提交資料,如getParameter,getParameterMap。

2、現在我們用Action代替了Servlet,所以提交表單到了Action中,但是Action中沒有request物件,所以不能直接使用request物件。

下邊分別對三種方式加以闡述:

(1)使用ActionContext類來獲取。

  • 建立表單,提交表單資料到action中
  • 在action中使用ActionContext獲取資料。

程式碼如下:

Form1DemoAction.java

package form;

import java.util.Arrays;
import java.util.Map;
import java.util.Set;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class Form1DemoAction extends ActionSupport {

	@Override
	public String execute() throws Exception{
		//獲取表單資料的第一種方法:ActionContext類獲取
		/**
		 * 1、獲取ActionContext物件
		 * 2、呼叫方法得到表單資料
		 */
		ActionContext context = ActionContext.getContext();
		//key是表單輸入的name屬性值,value是輸入的值
		Map<String, Object> map = context.getParameters();
		
		Set<String> keys = map.keySet();
		for(String key:keys){
			Object[] obj = (Object[]) map.get(key);
			System.out.println(Arrays.toString(obj));
		}
		
		
		
		return NONE;
	}
}


表單form1.jsp如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'form1.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </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>


配置檔案struts.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
	
<struts>

    <!-- 獲取表單提交的資料 -->
	<package name="form" extends="struts-default" namespace="/">
		<action name="form1" class="form.Form1DemoAction">
		
		</action>
				
	</package>

     
          
     
</struts>

在web.xml設定攔截器:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Test_Struts2</display-name>
  
  <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>


        分析:我們首先寫了一個表單,提交資料指向了form1.action。在配置檔案中,將form1.action指向了我們自定義的action類

Form1DemoAction。當我們訪問form1.jsp並且提交了表單資料後,Form1DemoAction類中的execute()將會執行,然後就可以得

到表單提交的資料了。

 (2)使用ServletActionContext類獲取。

Form2DemoAction.java如下:

package form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class Form2DemoAction extends ActionSupport {

	@Override
	public String execute() throws Exception{
		//獲取表單資料的第二種方法:ServletActionContext類獲取
		
		//1、使用ServletActionContext獲取request物件。
		HttpServletRequest request = ServletActionContext.getRequest();
		
		//2、呼叫request裡邊的方法得到結果
		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>,如下:

<action name="form2" class="form.Form2DemoAction">
		
</action>


在表單中,我們使用如下語句指向了form2.action

action="${pageContext.request.contextPath}/form2.action"

 (3)使用介面注入的方式獲取。

  •  讓action實現介面,得到request物件

Form3DemoAction.java

package form;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionSupport;

public class Form3DemoAction extends ActionSupport implements ServletRequestAware {

	private HttpServletRequest request;
	@Override
	public String execute() throws Exception{
		//獲取表單資料的第三種方法:使用介面注入方法來獲取
		
		//2、呼叫request裡邊的方法得到結果
				String username = request.getParameter("username");
				String password = request.getParameter("password");
				String address = request.getParameter("address");
				
				System.out.println(username+":"+password+":"+address);
		
		return NONE;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		
		//1、得到request物件
	  this.request=request;
		
	}

	
}

其中,在struts.xml我們需要配置再一個<action>,如下:

<action name="form3" class="form.Form3DemoAction">
		
</action>


在表單中,我們使用如下語句指向了form2.action

action="${pageContext.request.contextPath}/form3.action"

       好了,以上就是Struts2中action獲取表單資料的三種方式,其中,常用的是通過ActionContext和ServletActionContext來

獲取資料。使用介面注入的方法不常用。

 如果對你有幫助,記得點贊哈~歡迎大家關注我的部落格,隨時加群交流哦~