struts2学习(二)—action获取表单提交数据的三种方式

本文内容主要来自创智博客学习视频的整理
http://v.itcast.cn/course/212.html

action获取表单提交数据的三种方式:

(1)使用actionContext类
(2)使用ServletActionContext类
(3)使用接口注入的方式

1.使用actionContext类获取表单数据

在浏览器中输入http://127.0.0.1:8080/struts2All/form1.jsp
,可得网页,在username,password,address中输入相应内容,点“提交”,就可获取到网页上输入的值。

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

public class Form1DemoAction extends ActionSupport {

    @Override
    public String execute() throws Exception {

        // 第一种方式:使用ActionContext类获取表单数据
        // 1.获取ActionContext对象
        ActionContext context = ActionContext.getContext();
        // 2,获取表单数据,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;
    }

}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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>

2.使用ServletActionContext类

在浏览器中输入http://127.0.0.1:8080/struts2All/form1.jsp
,可得网页,在username,password,address中输入相应内容,点“提交”,就可获取到网页上输入的值。

package form;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class Form2DemoAction extends ActionSupport {

    public String execute() throws Exception{
        //1.使用ServletActionContext类获取请求
        HttpServletRequest request = ServletActionContext.getRequest(); 
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String address = request.getParameter("address");
        System.out.println("username:"+username+" password:"+password+" address:"+address);

        return NONE;
    }
}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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.使用接口注入的方式

继承ServletRequestAware,request可直接注入进来,然后从中获取数据。

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;

    public String execute() throws Exception{
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String address = request.getParameter("address");
        System.out.println("username:"+username+" password:"+password+" address:"+address);
        return NONE;
    }


    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }
}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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="submit"/>
    </form>
</body>
</html>

4.其他代码部分

4.1 代码架构

这里写图片描述

4.2 struts文件

<?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="demo2" extends="struts-default" namespace="/">
        <action name="form1" class="form.Form1DemoAction">
        </action>
        <action name="form2" class="form.Form2DemoAction">
        </action>
        <action name="form3" class="form.Form3DemoAction">
        </action>
    </package>
</struts>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值