Struts2 ServletAPI的使用

在Struts2应用中,我们经常需要用到Servlet的API来帮助我们完成功能需求,有三种使用方式

1 方式一:ServletActionContext

在index.jsp添加表单,用于提交参数到Action:

<h3>使用ServletActionContext类</h3>
<form action="demo1.action" method="post">
  	姓名:<input type="text" name="name"/><br/>
  	性别:<input type="text" name="gender"/><br/>
  	年龄:<input type="text" name="age"/><br/>
  	<input type="submit" value="提交"/>
</form>	

Action类,使用ServletActionContext获取HttpServletRequest,获取参数

package com.yiidian.action;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 使用ServletActionContext类
 * @author 一点教程(yiidian.com)
 *
 */
public class Demo1Action extends ActionSupport{

	@Override
	public String execute() throws Exception {
		//1.接收参数
		HttpServletRequest request = ServletActionContext.getRequest();
		String name = request.getParameter("name");
		String gender = request.getParameter("gender");
		String age = request.getParameter("age");
		
		System.out.println(name);
		System.out.println(gender);
		System.out.println(age);
		
		//2.共享数据到页面
		//2.1 request域
		request.setAttribute("request_data", "小苍");
		//2.2 session域
		request.getSession().setAttribute("session_data", "小风");
		//2.3 application域(ServletContext)
		ServletActionContext.getServletContext().setAttribute("application_data", "小泽");
		
		return SUCCESS;
	}
	
}

struts.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
	"http://struts.apache.org/dtds/struts-2.5.dtd">
	
<struts>

	<package name="base" extends="struts-default" namespace="/">
		<action name="demo1" class="com.yiidian.action.Demo1Action">
			<result>/succ.jsp</result>
		</action>
	</package>
	
</struts>

最后,在succ.jsp获取数据

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>一点教程-成功页面</title>
  </head>
  <body>
  ${requestScope.request_data }<br/>
  ${sessionScope.session_data }<br/>
  ${applicationScope.application_data }<br/>
  </body>
</html>

表单输入参数,提交表单,效果如下:

控制台打印:

3 方式二:ActionContext

在index.jsp添加表单,用于提交参数到Action:

<h3>使用ActionContext类</h3>
<form action="demo2.action" method="post">
  	姓名:<input type="text" name="name"/><br/>
  	性别:<input type="text" name="gender"/><br/>
  	年龄:<input type="text" name="age"/><br/>
  	<input type="submit" value="提交"/>
</form>

Action类,使用ActionContext接收请求参数

package com.yiidian.action;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.StrutsStatics;
import org.apache.struts2.dispatcher.HttpParameters;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 使用ActionContext类
 * @author 一点教程(yiidian.com)
 *
 */
public class Demo2Action extends ActionSupport{

	@Override
	public String execute() throws Exception {
		//1.接收参数
		ActionContext context = ActionContext.getContext();
		
		HttpParameters parameters = context.getParameters();
		
		String name = parameters.get("name").getValue();
		String gender = parameters.get("gender").getValue();
		String age = parameters.get("age").getValue();
		
		System.out.println(name);
		System.out.println(gender);
		System.out.println(age);
		
		//2.共享数据到页面
		//2.1 request域
		HttpServletRequest request = (HttpServletRequest)context.get(StrutsStatics.HTTP_REQUEST);
		request.setAttribute("request_data", "小苍");
		//2.2 session域
		context.getSession().put("session_data", "小风");
		//2.3 application域
		context.getApplication().put("application_data", "小泽");
		
		return SUCCESS;
	}
}

struts.xml配置

<action name="demo2" class="com.yiidian.action.Demo2Action">
	<result>/succ.jsp</result>
</action>

表单输入参数,提交表单,效果如下:

控制台打印

3 方式三:接口注入方式

在index.jsp添加表单,用于提交参数到Action:

<h3>使用接口注入方式</h3>
  	<form action="demo3.action" method="post">
  		姓名:<input type="text" name="name"/><br/>
  		性别:<input type="text" name="gender"/><br/>
  		年龄:<input type="text" name="age"/><br/>
  	<input type="submit" value="提交"/>
</form>

Action类通过实现ServletRequestAware,ServletContextAware接口来注入HttpServletRequest

package com.yiidian.action;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.util.ServletContextAware;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 使用接口注入方式
 * @author 一点教程(yiidian.com)
 *
 */
public class Demo3Action extends ActionSupport implements ServletRequestAware,ServletContextAware{
	private HttpServletRequest request;
	private ServletContext application;
	@Override
	public String execute() throws Exception {
		//1.接收参数
		String name = request.getParameter("name");
		String gender = request.getParameter("gender");
		String age = request.getParameter("age");
	
		System.out.println(name);
		System.out.println(gender);
		System.out.println(age);
		
		//2.共享数据到页面
		request.setAttribute("request_data", "小苍");
		request.getSession().setAttribute("session_data", "小风");
		application.setAttribute("application_data", "小泽");
		
		return SUCCESS;
	}
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
	@Override
	public void setServletContext(ServletContext context) {
		this.application = context;
	}
}

struts.xml配置

<action name="demo3" class="com.yiidian.action.Demo3Action">
	<result>/succ.jsp</result>
</action>

表单输入参数,提交表单,效果如下:

控制台打印结果

 

源码下载:https://pan.baidu.com/s/1tZ-Evih4zfZCbgohizXj-w

热门文章

优秀文章