struts2自学入门2-值栈与OGNL表达式

本文深入解析Struts2框架中的值栈(ValueStack)概念与操作方法,包括其与OGNL(Object-Graph Navigation Language)表达式的交互,探讨如何在ActionContext中管理数据,以及如何利用OGNL进行数据访问和表达式处理。同时,文章还介绍了注解方式的配置、自定义拦截器的实现,以及拦截器组的定义和使用。

struts2的值栈与OGNL

1、什么是值栈

struts2提供的一个接口:ValueStack,我们用的是它的实现类:OgnlValueStack (不是咱们创建,是struts2来创建)。 当浏览器访问action的时候,会创建值栈对象,还会创建ActionContext对象最后把整个值栈放在actionContext中,然后把整个actionContext与当前线程绑定

2、操作值栈的数据
  1. 在context区map结构操作数据
//第一种方式:根据key从ActionContext中获取应用域的map,往map中放数据
ActionContext context = ActionContext.getContext();
Map<String,Object> application = context.getApplication();
application.put("key","value");
//使用原始的ServletAPI对象ServletContext
ServletContext application = ServletActionContext.getServletContext();
application.setAttribute("key","value");


//前台获取数据
<s:properte value="#key">
  1. 在root区list结构操作数据

    action中的属性都存放在root区,通常用于存放数据到前台展示

//后台放入数据
public class ActionDemo extends ActionSupport{
    private String username;
    //必须书写get方法,才能在前台获取
    public String getUsername(){
        return username;
    }
    
    public String execute(){
        username = "张三";
        //后台获取值栈中root的数据
        ValueStack vs = ActionContext.getContext().getValueStack();
        vs.findValue("usename");
        return null;
    }
}
//前台显示数据
<s:property value="username"/>

前台获取root中的数据不需要在前面加#号,获取context区的数据需要加#号

3、OGNL表达式
  1. #的作用

    //每次循环遍历的时候需要加#号
    <!-- 手动构建的list集合 -->
    <s:iterator value="{'aa','bb','cc'}" var="ll">
    	<s:property value="#ll"/>
    </s:iterator>
    <!-- 手动构建的map集合 -->
    <s:iterator value="#{'aa':'111','bb':'222','cc':'333'}" var="mm">
    	<s:property value="#mm.key"/>----><s:property value="#mm.value"/><br/>
    </s:iterator>
    
  2. %的作用

    //强制将标签内容转换成ognl表达式
    <s:textfield value="%{#request.abcd}"></s:textfield>
    
  3. $的作用

    //可以在struts.xml配置文件中获取值栈的数据
    ${值栈中的数据}
    
4、注解方式

条件:

  1. 需要struts2-convention-plugin-2.3.24.jar包
  2. struts自己扫描注解,只会去包名以action、actions、struts、strus2结尾的包去扫描Action

@ParentPackage("struts-default")	//指定继承的父包
@Namespace("/")						//定义在action类上的命名空间
public class CustomerAction extends ActionSupport
{
	@Action(value="customer_findAll",results={
	@Result(name="success",location="/jsp/customer/list.jsp")})
	public String findAll()
	{
		.....
	}
}
5、拦截器Interceptor

自定义拦截器步骤

  1. 需要创建一个拦截器(javabean)-实现Interceptor-继承AbstractInterceptor(重点:以后继承MethodFilterInterceptor,这样就可以设置哪些方法可以不拦截)
  2. 为action声明这个创建好的拦截器
  3. 定义一个拦截器组,组里面可以放1或多个拦截器(注意:组和组可以互相调用,拦截器可以出现在多个组中)
  4. 指定执行组中拦截器
//创建拦截器
public class MyInterceptor extends AbstractInterceptor{

	@Override
	public String intercept(ActionInvocation arg0) throws Exception {
		System.out.println("MyInterceptor拦截器拦截了!");
		//返回值为action执行的返回值
		String name = arg0.invoke();
		System.out.println("action方法被执行,又回到拦截器了!");
		return name;
	}

}
<package name="p1" extends="struts-default" namespace="/">
		<!-- 配置拦截器 -->
		<interceptors>
			<!-- 声明自定义拦截器 -->
			<interceptor name="MyInterceptor1" class="com.edu.action.MyInterceptor1"></interceptor>
            <interceptor name="MyInterceptor2" class="com.edu.action.MyInterceptor2"></interceptor>
            <!-- 在声明自定义拦截器3的时候,设置执行findAll方法时,bulanjie -->
            <interceptor name="MyInterceptor2" class="com.edu.action.MyInterceptor2">
                <parm name="exincludeMethod">findAll</parm>
            </interceptor>
			<!-- 定义一个拦截器组 -->
            	<interceptor-stack name="MyInterceptor-stack">
				<!-- 放入需要执行的拦截器,需要把默认的拦截器栈写入 -->
                <interceptor-ref name="defaultStack"></interceptor-ref>
				<interceptor-ref name="MyInterceptor1"></interceptor-ref>
			</interceptor-stack>
		</interceptors>
		<!-- 指定执行那个拦截器组 -->
		<default-interceptor-ref name="MyInterceptor-stack"></default-interceptor-ref>
		<action name="demo01" class="">
            <!-- 可以创建局部的拦截器或者拦截器组 -->
            <interceptor-ref name="MyInterceptor2"></interceptor-ref>
    	</action>
	</package>
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值