5.struts2中Action类中获取ServletAPI的三种方式

**Servlet的API的访问(开发中偶尔会使用到)**
	
	1.在Action类中也可以获取到Servlet一些常用的API,有如下三种方式获取
		* 完全解耦合的方式
		* 使用接口注入的方式
		* 使用ServletActionContext中静态方法直接访问Servlet的API
		
		* 需求:提供JSP的表单页面的数据,在Action中使用Servlet的API接收到,然后保存到三个域对象中,最后再显示到JSP的页面上。
			* 提供JSP注册的页面,演示下面这三种方式:
				<h3>注册页面</h3>
				<form action="${ pageContext.request.contextPath }/xxx.action" method="post">
					姓名:<input type="text" name="username" /><br/>
					密码:<input type="password" name="password" /><br/>
					<input type="submit" value="注册" />
				</form>
	
	2.完全解耦合的方式
		* 如果使用该种方式,Struts2框架中提供了一个类,ActionContext类,该类中提供一些方法,通过方法获取Servlet的API
		* 一些常用的方法如下:
			* static ActionContext getContext()  										-- 获取ActionContext对象实例
			* java.util.Map<java.lang.String,java.lang.Object> getParameters()  		-- 获取请求参数,相当于request.getParameterMap();
			* java.util.Map<java.lang.String,java.lang.Object> getSession()  			-- 获取的代表session域的Map集合,就相当于操作session域。
			* java.util.Map<java.lang.String,java.lang.Object> getApplication() 		-- 获取代表application域的Map集合
			* void put(java.lang.String key, java.lang.Object value)  					-- 注意:向request域中存入值。
		
		* 完成代码的测试
	
    3.使用接口注入的方式
		* Struts2框架中提供了一些接口,编写的Action类可以是去实现这些接口,然后实现这些接口中的方法,这些方法都是把一些Servlet的常用对象通过参数的方式传递进来。
		* 常用的接口如下:
			* ServletRequestAware		-- 注入request
			* ServletContextAware		-- 注入ServletContext
			* ServletResponseAware		-- 注入response.
    
	4.使用ServletActionContext中静态方法直接访问Servlet的API
		* Struts2框架提供了一个类,ServletActionContext,该类中提供了一些静态的方法
		* 具体的方法如下
			* getPageContext();
			* getRequest()
			* getResponse();
			* getServletContext();

demo4.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>

<h3>注册页面(完全解耦合的方式)</h3>
<form action="${ pageContext.request.contextPath }/regist.action" method="post">
	姓名:<input type="text" name="username" /><br/>
	密码:<input type="password" name="password" /><br/>
	<input type="submit" value="注册" />
</form>

<h3>注册页面(接口注入的方式)</h3>
<form action="${ pageContext.request.contextPath }/regist2.action" method="post">
	姓名:<input type="text" name="username" /><br/>
	密码:<input type="password" name="password" /><br/>
	<input type="submit" value="注册" />
</form>

<h3>通过ServletActionContext对象的方式</h3>
<form action="${ pageContext.request.contextPath }/regist3.action" method="post">
	姓名:<input type="text" name="username" /><br/>
	密码:<input type="password" name="password" /><br/>
	<input type="submit" value="注册" />
</form>

</body>
</html>

struts_demo4.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="demo4" extends="struts-default" namespace="/">
		<!-- 完全解耦合的方式 -->
		<action name="regist" class="demo4.UserAction" method="regist">
			<result>/demo4/success.jsp</result>
		</action>
		
		<!-- 接口注入的方式 -->
		<action name="regist2" class="demo4.UserAction2" method="regist">
			<result>/demo4/success.jsp</result>
		</action>
		 
		<!-- 使用ServletActionContext对象的方式 -->
		<action name="regist3" class="demo4.UserAction3" method="regist">
			<result>/demo4/success.jsp</result>
		</action>
	</package>
</struts>

Action:

package demo4;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 完全解耦合的方式
 * @author mjl
 *
 */
public class UserAction extends ActionSupport{

	public String regist(){
		
		//接收表单的数据
		//先获取到ActionContext对象
		ActionContext context=ActionContext.getContext();
		//获取请求的参数
		Map<String, Object> map = context.getParameters();
		//通过Key获取值
		String[] username=(String[]) map.get("username");
		String[] password=(String[]) map.get("password");
		System.out.println("用户名:"+username[0]+",密码:"+password[0]);
		
		//获取session
		Map<String, Object> sessionMap = context.getSession();
		//像该map存入具体的值
		sessionMap.put("sessName", "美美");
		
		return SUCCESS;
	}
}


package demo4;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction2 extends ActionSupport implements ServletRequestAware{

	private HttpServletRequest request;

	public void setServletRequest(HttpServletRequest request) {
		this.request=request;
	}
	
	public String regist(){
		String username=request.getParameter("username");
		request.getSession().setAttribute("sessName", username);
		return SUCCESS;
	}
}


package demo4;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 采用servletActionContext对象的方法
 * @author mjl
 *
 */
public class UserAction3 extends ActionSupport{
	
	public String regist(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String username=request.getParameter("username");
		request.getSession().setAttribute("sessName", username);
		return SUCCESS;
	}
}

  

  

转载于:https://www.cnblogs.com/syj1993/p/8509368.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值