Action 中获取表单数据的三种方式

来源:互联网 发布:p2p下载软件推荐 编辑:程序博客网 时间:2024/05/01 11:51

(尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/53138905  冷血之心的博客)

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

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package form;  
  2.   
  3. import java.util.Arrays;  
  4. import java.util.Map;  
  5. import java.util.Set;  
  6.   
  7. import com.opensymphony.xwork2.ActionContext;  
  8. import com.opensymphony.xwork2.ActionSupport;  
  9.   
  10. public class Form1DemoAction extends ActionSupport {  
  11.   
  12.     @Override  
  13.     public String execute() throws Exception{  
  14.         //获取表单数据的第一种方法:ActionContext类获取  
  15.         /** 
  16.          * 1、获取ActionContext对象 
  17.          * 2、调用方法得到表单数据 
  18.          */  
  19.         ActionContext context = ActionContext.getContext();  
  20.         //key是表单输入的name属性值,value是输入的值  
  21.         Map<String, Object> map = context.getParameters();  
  22.           
  23.         Set<String> keys = map.keySet();  
  24.         for(String key:keys){  
  25.             Object[] obj = (Object[]) map.get(key);  
  26.             System.out.println(Arrays.toString(obj));  
  27.         }  
  28.           
  29.           
  30.           
  31.         return NONE;  
  32.     }  
  33. }  


表单form1.jsp如下:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'form1.jsp' starting page</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!-- 
  20.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  21.     -->  
  22.   
  23.   </head>  
  24.     
  25.   <body>  
  26.      
  27.     <form action="${pageContext.request.contextPath}/form1.action" method="post">  
  28.         username: <input type="text" name="username"/> <br>  
  29.         password: <input type="text" name="password"/> <br>  
  30.         address:  <input type="text" name="address"/>  <br>  
  31.         <input type="submit" value="提交">  
  32.       
  33.     </form>  
  34.      
  35.   </body>  
  36. </html>  


配置文件struts.xml如下:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.3.dtd">  
  5.       
  6. <struts>  
  7.   
  8.     <!-- 获取表单提交的数据 -->  
  9.     <package name="form" extends="struts-default" namespace="/">  
  10.         <action name="form1" class="form.Form1DemoAction">  
  11.           
  12.         </action>  
  13.                   
  14.     </package>  
  15.   
  16.        
  17.             
  18.        
  19. </struts>  

 

在web.xml设置拦截器:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <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">  
  3.   <display-name>Test_Struts2</display-name>  
  4.     
  5.   <filter>  
  6.         <filter-name>struts2</filter-name>  
  7.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  8.     </filter>  
  9.   
  10.     <filter-mapping>  
  11.         <filter-name>struts2</filter-name>  
  12.         <url-pattern>/*</url-pattern>  
  13.     </filter-mapping>  
  14.       
  15.   <welcome-file-list>  
  16.     <welcome-file>index.html</welcome-file>  
  17.     <welcome-file>index.htm</welcome-file>  
  18.     <welcome-file>index.jsp</welcome-file>  
  19.     <welcome-file>default.html</welcome-file>  
  20.     <welcome-file>default.htm</welcome-file>  
  21.     <welcome-file>default.jsp</welcome-file>  
  22.   </welcome-file-list>  
  23. </web-app>  


        分析:我们首先写了一个表单,提交数据指向了form1.action。在配置文件中,将form1.action指向了我们自定义的action类

Form1DemoAction。当我们访问form1.jsp并且提交了表单数据后,Form1DemoAction类中的execute()将会执行,然后就可以得

到表单提交的数据了。

 

 (2)使用ServletActionContext类获取。

 

Form2DemoAction.java如下:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package form;  
  2. import javax.servlet.http.HttpServletRequest;  
  3. import org.apache.struts2.ServletActionContext;  
  4. import com.opensymphony.xwork2.ActionSupport;  
  5. public class Form2DemoAction extends ActionSupport {  
  6.   
  7.     @Override  
  8.     public String execute() throws Exception{  
  9.         //获取表单数据的第二种方法:ServletActionContext类获取  
  10.           
  11.         //1、使用ServletActionContext获取request对象。  
  12.         HttpServletRequest request = ServletActionContext.getRequest();  
  13.           
  14.         //2、调用request里边的方法得到结果  
  15.         String username = request.getParameter("username");  
  16.         String password = request.getParameter("password");  
  17.         String address = request.getParameter("address");  
  18.           
  19.         System.out.println(username+":"+password+":"+address);  
  20.         return NONE;  
  21.     }  
  22. }  

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

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <action name="form2" class="form.Form2DemoAction">  
  2.           
  3. </action>  


在表单中,我们使用如下语句指向了form2.action

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. action="${pageContext.request.contextPath}/form2.action"  



 

 (3)使用接口注入的方式获取。

  •  让action实现接口,得到request对象

 

Form3DemoAction.java

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package form;  
  2.   
  3. import javax.servlet.http.HttpServletRequest;  
  4.   
  5. import org.apache.struts2.interceptor.ServletRequestAware;  
  6.   
  7. import com.opensymphony.xwork2.ActionSupport;  
  8.   
  9. public class Form3DemoAction extends ActionSupport implements ServletRequestAware {  
  10.   
  11.     private HttpServletRequest request;  
  12.     @Override  
  13.     public String execute() throws Exception{  
  14.         //获取表单数据的第三种方法:使用接口注入方法来获取  
  15.           
  16.         //2、调用request里边的方法得到结果  
  17.                 String username = request.getParameter("username");  
  18.                 String password = request.getParameter("password");  
  19.                 String address = request.getParameter("address");  
  20.                   
  21.                 System.out.println(username+":"+password+":"+address);  
  22.           
  23.         return NONE;  
  24.     }  
  25.   
  26.     @Override  
  27.     public void setServletRequest(HttpServletRequest request) {  
  28.           
  29.         //1、得到request对象  
  30.       this.request=request;  
  31.           
  32.     }  
  33.   
  34.       
  35. }  


 

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

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <action name="form3" class="form.Form3DemoAction">  
  2.           
  3. </action>  


在表单中,我们使用如下语句指向了form2.action

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. action="${pageContext.request.contextPath}/form3.action"  

 

 

 

       好了,以上就是Struts2中action获取表单数据的三种方式,其中,常用的是通过ActionContext和ServletActionContext来

获取数据。使用接口注入的方法不常用。

0 0
原创粉丝点击