struts2 - 5 struts2 通过ACtionContext 与 servletACtionContext 使用servlet的API

124 篇文章 1 订阅
96 篇文章 0 订阅

代码演示在action类中使用servlet的API

 

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

		<!-- 全局result -->
        <global-results>
            <result name="success">/main.html</result>
        </global-results>
		<!-- 不写method直接执行action类中的execute方法 -->
		<action name="demoAction" class="web.action.ActionDemo1"/>
		
		<action name="userAction_*" class="web.action.UserAction" method="{1}">
			<result name="msg">/retMsg.jsp</result>
		</action>

    </package>



</struts>

 

action类

public class UserAction extends ActionSupport {
	
	
	/**
	 *
	 * 1.通过ActionContext类使用servlet的API(完全解耦合方式)
	 * 2.通过servletACtionContext类使用原生servlet的API
	 * @return
	 */
	public String doReg() {

		/*
		 * 1.通过ActionContext类使用servlet的API(完全解耦合方式)
		 */
		//获得action上下文
		ActionContext actionContext = ActionContext.getContext();
		
		//获得页面参数
		Map<String, Object> map = actionContext.getParameters();
		Set<String> keyset = map.keySet();
		for (String key : keyset) {
			//这里返回的应该是一个字符串数组,需要强转一下
			 String[] vals = (String[]) map.get(key);
			System.out.println(key + ":" + vals[0]);
		}
		
		//获得session域的map集合
		Map<String, Object> session = actionContext.getSession();
		session.put("sessionText", "sessionText");
		//获得application域的map集合
		Map<String, Object> application = actionContext.getApplication();
		application.put("applicationText", "applicationText");
		//写入request域
		actionContext.put("requestText", "requestText");
		
		
		/*
		 * 2.通过servletACtionContext类使用原生servlet的API,不再演示。。
		 * 
		 */
		/*
			PageContext pageContext = ServletActionContext.getPageContext();
			
			HttpServletRequest request = ServletActionContext.getRequest();
			
			HttpServletResponse response = ServletActionContext.getResponse();
			
			ServletContext servletContext = ServletActionContext.getServletContext();
			
			//获得域对象后就可以使用servlet的API了。。。
		*/
		
		
		
		return "msg";
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值