• 在Action中获取表单提交数据


    -----------------siwuxie095

       

       

       

       

       

       

       

       

    在 Action 中获取表单提交数据

       

       

    1、之前的 Web 阶段是提交表单到 Servlet,在其中使用 Request 对象

    的方法获取数据

       

       

       

    2、Struts2 是提交表单到 Action,但 Action 没有 Request 对象,不能

    直接使用 Request 对象获取数据

       

    「可以间接使用 Request 对象获取数据」

       

       

       

    3、Action 获取表单提交数据主要有三种方式:

       

    (1)使用 ActionContext

       

    (2)使用 ServletActionContext

       

    (3)使用 ServletRequestAware 接口

       

       

    「底层封装的依然是 Servlet 的 API」

       

       

       

       

       

       

       

    使用 ActionContext 类获取

       

       

    1、使用 getParameters() 方法获取表单提交数据,因为它不是静态

    方法,所以需要先创建 ActionContext 对象

       

    注意:

       

    ActionContext 对象不是 new 出来的,而是通过 ActionContext 类

    直接调用静态方法 getContext() 返回的

       

       

       

    2、具体过程

       

    (1)创建表单,提交表单到 Action

       

    (2)在 Action 中使用 ActionContext 类获取表单提交数据

       

       

       

    3、具体实现

       

    1)编写页面

       

    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>表单</title>

    </head>

    <body>

    <form action="${pageContext.request.contextPath }/form.action" method="post">

    username:<input type="text" name="username"/>

    <br/>

    password:<input type="password" name="password"/>

    <br/>

    address:<input type="text" name="address"/>

    <br/>

    <input type="submit" value="提交"/>

    </form>

    </body>

    </html>

       

       

       

    2)编写 Action

       

    Form1Action.java:

       

    package com.siwuxie095.action;

       

    import java.util.Arrays;

    import java.util.Map;

    import java.util.Set;

       

    import com.opensymphony.xwork2.ActionContext;

    import com.opensymphony.xwork2.ActionSupport;

       

       

    /**

    * 使用 ActionContext 类获取

    */

    public class Form1Action extends ActionSupport {

    @Override

    public String execute() throws Exception {

    ActionContext context=ActionContext.getContext();

    /*

    * 调用 getParameters() 方法,返回值是 Map 类型,创建以接收

    *

    * Key 表单中输入项的 name 属性值,Value 输入的值

    *

    */

    Map<String, Object> map=context.getParameters();

    // 得到所有的 Key

    Set<String> keys=map.keySet();

    //根据 Key 得到 Value

    for (String key : keys) {

    //数组形式:输入项可能有复选框,传过来多个值

    Object[] obj=(Object[]) map.get(key);

    System.out.println(Arrays.toString(obj));

    }

    return NONE;

    }

    }

       

       

       

    3)配置 Action

       

    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="demo" extends="struts-default" namespace="/">

    <action name="form" class="com.siwuxie095.action.Form1Action"></action>

    </package>

       

    </struts>

       

       

       

    4)访问路径

       

    1)http://localhost:8080/工程名/form.jsp

       

       

       

    注:

       

       

       

       

       

       

       

       

       

    使用 ServletActionContext 类获取

       

       

    1、直接调用 ServletActionContext类中的静态方法,

    获取 Request 对象,进而通过 getParameter() 方法

    获取表单提交数据

       

       

       

    2、具体实现

       

    1)编写页面(同上 form.jsp)

       

       

    2)编写 Action

       

    Form2Action.java:

       

    package com.siwuxie095.action;

       

    import javax.servlet.http.HttpServletRequest;

       

    import org.apache.struts2.ServletActionContext;

       

    import com.opensymphony.xwork2.ActionSupport;

       

       

    /**

    * 使用 ServletActionContext 类获取

    */

    public class Form2Action extends ActionSupport {

    @Override

    public String execute() throws Exception {

    // 先使用 ServletActionContext 类调用静态方法获取 Request 对象

    HttpServletRequest request=ServletActionContext.getRequest();

    // 再调用 Request 对象的方法获取表单提交数据

    String username=request.getParameter("username");

    String password=request.getParameter("password");

    String address=request.getParameter("address");

    System.out.println(username+"-"+password+"-"+address);

    return NONE;

    }

    }

       

       

       

    3)配置 Action

       

    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="demo" extends="struts-default" namespace="/">

    <action name="form" class="com.siwuxie095.action.Form2Action"></action>

    </package>

       

    </struts>

       

       

       

    4)访问路径(同上)

       

       

       

    注:

       

       

       

       

       

       

       

       

    使用 ServletRequestAware 接口获取

       

       

    1、让 Action 实现 ServletRequestAware接口,获取

    Request 对象,进而通过 getParameter() 方法获取表

    单提交数据

       

       

       

    2、具体实现

       

    1)编写页面(同上 form.jsp)

       

       

       

    2)编写 Action

       

    Form3Action.java:

       

    package com.siwuxie095.action;

       

    import javax.servlet.http.HttpServletRequest;

       

    import org.apache.struts2.interceptor.ServletRequestAware;

       

    import com.opensymphony.xwork2.ActionSupport;

       

       

    /**

    * 使用 ServletRequestAware 接口获取(实现该接口)

    */

    public class Form3Action extends ActionSupport implements ServletRequestAware {

       

    private HttpServletRequest request;

    //重写 ServletRequestAware 接口的 setServletRequest() 方法获取 Request 对象

    @Override

    public void setServletRequest(HttpServletRequest request) {

    //使用 ServletRequestAware 接口注入 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;

    }

       

    }

       

       

       

    3)配置 Action

       

    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="demo" extends="struts-default" namespace="/">

    <action name="form" class="com.siwuxie095.action.Form3Action"></action>

    </package>

       

    </struts>

       

       

    4)访问路径(同上)

       

       

       

    注:

       

       

       

       

       

       

       

       

    【made by siwuxie095】

  • 相关阅读:
    Python SyntaxError: Missing parentheses in call to 'print'
    Python SyntaxError: Missing parentheses in call to 'print'
    Ubuntu virtualenv 创建 python3 虚拟环境 激活 退出
    Ubuntu virtualenv 创建 python3 虚拟环境 激活 退出
    Python 类似 SyntaxError: Non-ASCII character 'xc3' in file
    Python 类似 SyntaxError: Non-ASCII character 'xc3' in file
    238 ES5新增方法:forEach()、map()、filter()、some()、every(),some、forEach、filter区别,from,of,find,findIndex
    237 借用原型对象,继承【父类】方法 【寄生组合继承】
    236 子构造函数继承父构造函数中的属性
    235 继承 之 call()
  • 原文地址:https://www.cnblogs.com/siwuxie095/p/7335778.html
Copyright © 2020-2023  润新知