深入分析JavaWeb Item48 -- Struts2中OGNL表达式与ValueStack_struts 2 过滤ongl表达式(1)

contextMap中

k这里写图片描述

5、利用OGNL获取ValueStack中:根栈和contextMap中的数据

原则:OGNL表达式如果以#开头,访问的contextMap中的数据
如果不以#开头,是访问的根栈中的对象的属性

1、在动作类中Demo1Action中重写execute方法

package com.itheima.actions;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;

public class Demo1Action extends ActionSupport{
    private String username = "刘小晨";
// private String p="actionP";

    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }

// public String getP() {
// return p;
// }
//
// public void setP(String p) {
// this.p = p;
// }

    public String execute() throws Exception {
// ServletActionContext.getRequest().setAttribute("p", "rp");//ServletRequest中的request
        ServletActionContext.getRequest().getSession().setAttribute("p", "sp");//HttpSession中的session
        ServletActionContext.getServletContext().setAttribute("p", "ap");//ServletContext中的application

        ValueStack vs = ActionContext.getContext().getValueStack();
        System.out.println(vs);
        return super.execute();
    }

}

配置struts.xml

<struts>
    <constant name="struts.devMode" value="true" />
    <package name="p1" extends="struts-default">
        <action name="showContextMap" class="com.itheima.actions.Demo1Action">
            <result>/contextMap.jsp</result>
        </action>
    </package>
</struts>

编写contextMap.jsp页面

<%@ page language="java" import="java.util.\*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>OGNL取contextMap中的数据</title>
  </head>

  <body>
    <br/>取contextMap中的根中(List类型的,实际类型CompoundRoot)的数据.<br/>
    <s:property value="locale"/>
    <s:property value="p" /><br/><!--从栈顶开始搜索Map的key或者是对象的属性-->
    <s:property value="[0]" /><br/><!--不是栈顶对象,是从索引为0处砍出来的一个新集合-->
    <s:property value="[0].top" /><br/><!--栈顶对象-->
    <!-- value="username"的涵义:从根中的栈顶开始,对每个对象搜索他的getUsername()方法,找到为止 -->
    <br/>取contextMap中的其他的数据(非根中的).<br/>

    <s:property value="#request"/><br/>
    动作中的:<s:property value="p"/><br/>
    请求范围:<s:property value="#request.p"/><br/>
    会话范围:<s:property value="#session.p"/><br/>
    应用范围:<s:property value="#application.p"/><br/>
    <!-- 依次从动作\页面\请求\会话范围\应用范围查找名称为p的对象 -->
    attr:<s:property value="#attr.p"/><br/>


    <!-- 显示出来的东西并不是contextMap中所有的东西,只是大部分 -->
    <s:debug></s:debug>
  </body>
</html>

数据内存结构

这里写图片描述

6、ValueStack的常用方法
  • void set(String key,Object value):先获取根栈栈顶的Map,如果不存在,压入一个新的Map

这里写图片描述

  • void setValue(String ,Object):String是一个OGNL表达式。如果表达式以#开头,操作contextMap。如果不是,设置根栈中对象的某个属性,从顶到尾依次搜寻。

这里写图片描述

  • Object findValue(String expr):参数是一个OGNL表达式。如果以#开头,从contextMap中找key值所对应的对象。如果不是以#开头,搜索根栈中对象的属性(getter方法)
    特别注意:如果编写的表达式不是以#开头,先搜索根栈对象的所有属性,如果没有找到,会把它当做key值到contextMap中找。

这里写图片描述

这里写图片描述

  • String findString(String expr):把OGNL表达式获取的对象转换成String
    这里写图片描述
7、contextMap中放的常用数据

request:请求范围的数据。即ServletRequest中的那个Map
parameters:请求参数的数据。即request.getParameterMap得到
application:应用范围的数据。即ServletContext中的那个Map
session:会话范围的数据。即HttpSession中的那个Map
attr:也是一个Map。会从以下Map中依次搜索:request、session、application

这里写图片描述

五、OGNL的一些其他操作

1、投影

1、 集合的投影(只输出部分属性)

 <s:iterator value="allList.{name}" var="person">
           <s:property/>  <br>
 </s:iterator> 

2、 过滤条件:this 表示集合中的元素;

a.“?#”:过滤所有符合条件的集合,如:users.{?#this.age > 19};
b.“^#”:过滤第一个符合条件的元素,如:users.{^#this.age > 19};
c.“$#”:过滤最后一个符合条件的元素,如:users.{$#this.age > 19} 。

<s:iterator value="allList.{?#this.age>25}" var="person">
        <s:property value="name"/>  xxxxxx  <s:property value="age"/> <br>
 </s:iterator> 

3、 集合的投影和过滤
投影(过滤)操作返回的是一个集合,可以使用索引取得集合中指定的
元素,如:users.{?#this.age > 19}[0]

<s:iterator value="allList.{?#this.age>25}.{name}" var="person">
        <s:property/><br>
    </s:iterator> 

 <s:iterator value="allList.{?#this.age>25}[0]" var="person">
        <s:property/><br>
  </s:iterator> 

编写TagDemo1Action

package com.itheima.actions;

import java.util.ArrayList;
import java.util.List;

import com.itheima.domain.Person;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class TagDemo1Action extends ActionSupport {
    private String username="刘小晨";
    private List<Person> ps = new ArrayList<Person>();
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public List<Person> getPs() {
        return ps;
    }

    public void setPs(List<Person> ps) {
        this.ps = ps;
    }

    public String execute() throws Exception {
        //初始化一些人
        ps.add(new Person("安康", "女性", 7000));
        ps.add(new Person("唐诗诗", "男性", 10000));
        ps.add(new Person("王卫星", "有待鉴定", 10000));

        ActionContext.getContext().getValueStack().setValue("#gender", "美女");
        return SUCCESS;
    }

}

编写jsp页面

<%@ page language="java" import="java.util.\*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>OGNL投影等操作</title>
  </head>

  <body>
    <br/>--------OGNL的投影查询----------<br/>
    <table border="1">
        <tr>
            <th>姓名</th>
        </tr>
        <!-- ps.{nickname} ---List<String> var=“p”引用的是一个String类型的,就代表着匿名 -->
         <s:iterator value="ps.{nickname}" var="p">
            <tr>
                <td><s:property value="#p"/></td>
            </tr>
         </s:iterator>
     </table>

     <hr/>
     <br/>--------OGNL的过滤----------<br/>
     <table border="1">
        <tr>
            <th>姓名</th>
            <th>性别</th>
            <th>薪水</th>
        </tr>
         <s:iterator value="ps.{?#this.salary>8000}" var="p">
            <tr>
                <td>${p.nickname}</td>
                <td><s:property value="#p.gender"/></td>
                <td>${p.salary}</td>
            </tr>
         </s:iterator>
     </table>
     <table border="1">
        <tr>
            <th>姓名</th>
            <th>性别</th>
            <th>薪水</th>
        </tr>
         <s:iterator value="ps.{^#this.salary>8000}" var="p">
            <tr>
                <td>${p.nickname}</td>
                <td><s:property value="#p.gender"/></td>
                <td>${p.salary}</td>
            </tr>
         </s:iterator>
     </table>
     <table border="1">
        <tr>
            <th>姓名</th>
            <th>性别</th>
            <th>薪水</th>
        </tr>
         <s:iterator value="ps.{$#this.salary>8000}" var="p">
            <tr>
                <td>${p.nickname}</td>
                <td><s:property value="#p.gender"/></td>
                <td>${p.salary}</td>
            </tr>
         </s:iterator>
     </table>
    <s:debug></s:debug>
  </body>
</html>

2、创建集合对象

构造Map,如#{‘foo1’:‘bar1’, ‘foo2’:‘bar2’}。这种方式常用在给radio或select、checkbox等标签赋值上

jsp页面:

 <s:radio list=“#{‘male’:‘男’,‘female’:‘女’}” name=“sex” label=“性别” /> 

运行结果是

<input type="radio" name="sex" id="sexmale" value="male"/>男
<input type="radio" name="sex" id="sexfemale" value="female"/>女

Action中的代码:

Map map=new HashMap();
map.put("male", "男");
map.put("female", "女");
ServletActionContext.getRequest().setAttribute("map", map);

jsp页面:

  <s:property value="#request.map.male"/><br>
  <s:property value="#request.map['female']"/><br>

运行结果是
男 女

 <%@ page language="java" import="java.util.\*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>利用OGNL创建List和Map(很重要)</title>
    <s:head/>
  </head>

  <body>
        <!-- 
 规律:
 通用标签:
 value属性:大部分都是OGNL表达式(90%)
 UI标签:表单有关
 value属性:大部分都不是OGNL表达式(90%)

 如果要当做OGNL表达式对待:使用%{}
 如果要把OGNL当做字符串对待:使用''
 -->
        <br/>-----创建List-------<br/>
        <s:iterator value="{'a','b','c'}" var="s">
            <s:property/><br/>
        </s:iterator>
        <br/>-----创建Map----有点像Json---<br/>
        <s:iterator value="#{'a':'aaa','b':'bbb','c':'ccc'}" var="me">


### React

*   介绍一下react

*   React单项数据流

*   react生命周期函数和react组件的生命周期

*   react和Vue的原理,区别,亮点,作用

*   reactJs的组件交流

*   有了解过react的虚拟DOM吗,虚拟DOM是怎么对比的呢

*   项目里用到了react,为什么要选择react,react有哪些好处

*   怎么获取真正的dom

*   选择react的原因

*   react的生命周期函数

*   setState之后的流程

*   react高阶组件知道吗?

*   React的jsx,函数式编程

*   react的组件是通过什么去判断是否刷新的

*   如何配置React-Router

*   路由的动态加载模块

*   Redux中间件是什么东西,接受几个参数

*   redux请求中间件如何处理并发

**[开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】](https://bbs.csdn.net/topics/618166371)**

![](https://img-blog.csdnimg.cn/img_convert/9749ea39072fc4b7b27af6f3a4db5ab1.png)
  • 25
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值