Struts2:Action获取表单提交数据

Action获取表单提交数据

之前web阶段,提交表单到servlet里面,在servlet里面使用request对象里面的方法获取,getParameter,getParameterMap。
提交表单到action,但是action没有request对象,不能直接使用request对象。
action获取表单提交数据主要三种方式:

  • 使用ActionContext类
  • 使用ServletActionContext类
  • 使用接口注入方式

使用ActionContext类获取

  • Map<String,Object> getParameters():返回一个包含所有HttpServletRequest参数信息,因为方法不是静态的方法,需要创建ActionContext类的对象,这个ActionContext类对象不是new出来的。
  • static ActionContext getContext():获取当前线程的ActionContext对象。

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>Insert title here</title>
</head>
<body>
	<form action="${pageContext.request.contextPath }/form.action" method="get">
		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、提交表单到action里面:struts2.xml

<package name="demo" extends="struts-default" namespace="/">
	<action name="form" class="action.LoginAction"></action>
</package>

3、在action使用ActionContext获取数据:LoginAction.java

public class LoginAction extends ActionSupport {
	// 第一种方式 使用ActionContext类获取
	@Override
	public String execute() throws Exception {
		// 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) {
			// 根据key得到value
			// 数组形式:因为输入项里面可能有复选框情况
			Object[] obj = (Object[]) map.get(key);
			System.out.println(Arrays.toString(obj));
		}
		return NONE;
	}
}

使用ServletActionContext类获取

  • static HttpServletRequest getRequest():获取 Web 应用的HttpServletRequest 对象。
  • static HttpServletResponse getResponse():获取 Web 应用的 HttpServletResponse 对象。
  • static ServletContext getServletContext() :获取 Web 应用的 ServletContext 对象。
  • static PageContext getPageContext():获取 Web 应用的PageContext 对象。

调用类里面静态方法,得到request对象

public class LoginAction extends ActionSupport {
    // 第二种方式 使用ServletActionContext类获取
	@Override
	public String execute() throws Exception {
		// 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);
		
		//操作三个域对象
		//1 request域
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("req", "reqValue");
		//2 session域
		HttpSession session = request.getSession();
		session.setAttribute("sess", "sessValue");
		//3 ServletContext域
		ServletContext context = ServletActionContext.getServletContext();
		context.setAttribute("contextname", "contextValue");
		
		return NONE;
	}
}

使用接口注入(了解)

  • ServletRequestAware:实现该接口的Action可以直接访问Web应用的HttpServletRequest实例。
  • ServletResponseAware:实现该接口的Action可以直接访问Web应用的HttpServletResponse实例。
  • SessionAware:实现该接口的Action可以直接访问Web应用的HttpSession实例。
  • ServletContextAware:实现该接口的Action可以直接访问Web应用的ServletContext实例。

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

public class LoginAction extends ActionSupport implements ServletRequestAware {
	private HttpServletRequest request;

	@Override
	public void setServletRequest(HttpServletRequest 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);
		
	//操作三个域对象
	//1 request域
	HttpServletRequest request = ServletActionContext.getRequest();
	request.setAttribute("req", "reqValue");
	//2 session域
	HttpSession session = request.getSession();
	session.setAttribute("sess", "sessValue");
	//3 ServletContext域
	ServletContext context = ServletActionContext.getServletContext();
	context.setAttribute("contextname", "contextValue");
	
		return NONE;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值