您的位置:首页 > 其它

action里获取表单数据的三种方式

2017-11-20 22:29 363 查看
 

(尊重劳动成果,转载请注明出处: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

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如下:

[html] view
plain copy

<%@ 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如下:

[java] view
plain copy

<?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设置拦截器:

[html] view
plain copy

<?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如下:

[java] view
plain copy

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>,如下:

[html] view
plain copy

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

          

</action>  

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

[html] view
plain copy

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

 

 (3)使用接口注入的方式获取。
 让action实现接口,得到request对象

 

Form3DemoAction.java

[java] view
plain copy

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>,如下:

[html] view
plain copy

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

          

</action>  

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

[html] view
plain copy

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

 

 

 

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

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

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: