OGNL表达式

OGNL表达式

OGNL的作用

  

OGNL的操作对象

 

      ValueStack:ValueStack中默认就是Action的对象,以及Action对象中的属性。

     

      ActionCotnext上下文。

         A:Request作用域

         B:Session作用域

         C:Application作用域

         D:Parameter,Request的请求参数     

        

      Ognl表达式访问或者定义不同对象时,需要用到不同的符号。

     

      无符号

    

      #     访问ActionContext对象时,需要用到的符号。  

      {}    OGNL中用于定义集合时

      #{}    OGNL中用于定义Map

      %{}   OGNL中,将字符串转义成OGNL表达式,并输出转化后的结果

      ${}    struts.xml中,获取ValueStack里成员变量的值。

1:OGNL表达式访问成员变量与成员方法

   public int add(int x, int y) {

      System.out.println("调用到add这个成员方法");

      return x + y;

   }

 

   public String accessMember() throwsException {

      this.userBean = new UserBean();

      this.userBean.setUserid("1");

      this.userBean.setUsername("admin");

      return "succ";

   }

省略UserBean

Jsp:

      userBean = <s:property value="userBean"/>   

  

         userid = <s:property value="userBean.userid"/>

        

         username = <s:property value="userBean['username']"/>

        

         username = <s:property value="userBean.getUsername()"/>

        

         <s:property value="userBean.setUserid(222)"/>

        

         userid = <s:property value="userBean.userid"/>

        

   add = <s:property value="add(10,20)"/>

2:输出静态变量与静态方法

public static final double PI = 3.1415;

 

   public static String getSystemTime() {

      Date date = new Date();

      DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

      return dateFormat.format(date);

   }

 

   public String accessStatic() throws Exception {

 

      return "succ";

   }

 

 

调用静态方法前提:struts.xml中开启常量 = struts.ognl.allowStaticMethodAcces


 Jsp:


   PI = <s:property value="@com.action.OgnlAction@PI"/>

  

   系统时间 = <s:property value="@com.action.OgnlAction@getSystemTime()"/>

3:输出集合

private Map<String, Object> map;

   private List<UserBean> userList;

   private Set<String> strSet;

//省略set,get

public String accessCollection() throws Exception {

      /**

       * 集合初始化

       */

      this.strSet = new LinkedHashSet<String>();

      this.strSet.add("aa");

      this.strSet.add("bb");

      this.strSet.add("cc");

      this.strSet.add("dd");

 

      this.userList = new ArrayList<UserBean>(6);

      for (int i = 1; i <= 5; i++) {

         UserBean userBean = new UserBean();

         userBean.setUserid(String.valueOf(i));

         userBean.setUsername("用户名_" + i);

 

         this.userList.add(userBean);

      }

 

      this.map = new LinkedHashMap<String,Object>();

      this.map.put("key_1", "value_1");

      this.map.put("key_2", OgnlAction.getSystemTime());

      this.map.put("key_3", this.strSet);

      this.map.put("key_4", this.userList);

      return "succ";

   }


 Jsp:       

 

strSet = <s:property value="strSet"/>

  

   大小Size = <s:property value="strSet.size()"/>

  

   删除元素= <s:property value="strSet.remove('bb')"/>

  

   strSet = <s:property value="strSet"/>

  

  

   userList = <s:property value="userList"/>

  

   大小 = <s:property value="userList.size()"/>

  

   第2个用户 = <s:property value="userList.get(1)"/>   username = <s:property value="userList.get(1).username"/>

  

   删除元素 = <s:property value="userList.remove(2)"/>

  

   大小 = <s:property value="userList.size()"/>

  

   map = <s:property value="map"/>

  

   size = <s:property value="map.size()"/>

  

   key_1 = <s:property value="map.key_1"/>

  

   key_2 = <s:property value="map['key_2']"/>

  

   key_3 = <s:property value="map.get('key_3')"/>

  

   key_4 = <s:property value="map.key_4.get(1).username"/>

4:作用域与请求参数

public String accessScope() throws Exception {

      this.userList = new ArrayList<UserBean>(6);

      for (int i = 1; i <= 5; i++) {

         UserBean userBean = new UserBean();

         userBean.setUserid(String.valueOf(i));

         userBean.setUsername("用户名_" + i);

 

         this.userList.add(userBean);

      }

 

      this.request.setAttribute("req_key", "Request作用域");

      this.session.setAttribute("session_key", this.userList);

      this.servletContext.setAttribute("app_key", OgnlAction.getSystemTime());

 

      // 1:第一种:后台转换的解决。

      // 页面中使用param对象或者EL表达式,取出来仍然是乱码。

      // String param_b =request.getParameter("param_b");

      // param_b = newString(param_b.getBytes("ISO-8859-1"), "UTF-8");

 

      // this.request.setAttribute("param_b_xx",param_b);

 

      // 2:第二种,改TomcatGETURL编码

      // server.xml中配置。ConnectorconnectionTimeout="20000" port="8080"

      // protocol="HTTP/1.1"redirectPort="8443" URIEncoding="UTF-8"

 

      // 3:写过滤器

 

      return "succ";

   }

 

Jsp:

 

req_key= <s:property value="#request.req_key"/>         ${requestScope.req_key}

  

   session_key = <s:property value="#session.session_key"/>

  

   app_key = <s:property value="#application.app_key"/>     ${applicationScope.app_key}

  

   请求参数 =

  

   param_a = <s:property value="#parameters.param_a"/>         ${param.param_a}

  

   param_b = <s:property value="#parameters.param_b"/>         ${param.param_b}

  

   param_b = <s:property value="#request.param_b_xx"/>

 

5:ValueStack与ActionContext 

publicString accessValueStack() throwsException {

      // ServletActionContext servletActionContext

 

      ActionContext context = ActionContext.getContext();

      context.put("context_key", "这是放到ActionContext中的值");

 

      /**

       * 操作ValueStack

       */

      ValueStack valueStack =context.getValueStack();

      /**

       *setValue只能设置Action中存在的属性的值。

       */

      valueStack.setValue("xx", OgnlAction.getSystemTime());

     

      /**

       *set可以设置与增加Action中不存在的属性的值。

       */

      valueStack.set("key_1", "value_1");

      valueStack.set("key_2", "value_2");

      valueStack.set("xx", "这是通过Set增加的");

  

     

     

      //valueStack.push(arg0);

 

      return "succ";

   }

 

 

context_key= <s:property value="#context_key"/>

  

   key_1 = <s:property value="key_1"/>

  

   key_2 = <s:property value="key_2"/>

  

   栈顶中的xx = <s:property value="xx"/>

  

   Action中属性的XX = <s:property value="[1].xx"/> 通过下标的方式获取栈中的元素。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值