struts2OGNL表达式(三)

OGNL表达式

  OGNL对象试图导航语言。${user.addr.name}这种写法就叫对象试图导航。Struts框架使用OGNL作为默认的表达式语言

  OGNL不仅仅可以试图导航,支持比EL表达式更加丰富的功能。

  OGNL的jar包,在导入Struts包的同时,就已经包含OGNL的jar包了,直接使用即可。

  ognl从OGNLContext中取数据,OGNLContext中有两部分组成 root 和 Context

  root 放置任何对象作为root都可以,Context 必须使用Map 键值对。 

      OGNL基本语法

  取值

        User rootUser = new User("tom",18);
        Map<String,User> context = new HashMap<String,User>();
        context.put("user1",new User("jack",18));
        context.put("user2",new User("rose",22));

        OgnlContext oc = new OgnlContext();
        oc.setRoot(rootUser);
        oc.setValues(context);

        String name = (String) 
        Ognl.getValue("name",oc,oc.getRoot());
        System.out.println(name);

        String name1 = 
        (String)Ognl.getValue("#user1.name",oc,oc.getRoot());
        System.out.println(name1);

  赋值(输出laoshi)

        String name = (String) Ognl.getValue("name='laoshi'",oc,oc.getRoot());

  调用方法

String name = (String) Ognl.getValue("getName()",oc,oc.getRoot());

  串行执行表达式

String name = (String)Ognl.getValue("setName('luck'),getName()",oc,oc.getRoot());

  调用静态方法

Double r = (Double)Ognl.getValue("@java.lang.Math@random()",oc,oc.getRoot());

  创建对象-list

Integer size = (Integer) Ognl.getValue("{'tom','jerry','jack','rose'}.size()",oc,oc.getRoot());

  map

Integer size = (Integer) Ognl.getValue("#{'name':'tom','age':18}.size()",oc,oc.getRoot());

 

OGNL与 Struts2的结合

  在struts2中 valueStack值栈 就是OGNLContext 包含root(放置的是一个栈(先进后出)) 和Context(放入的是ActionContext数据中心)

  root:默认情况下栈中存放的是当前访问的Action对象,Context中就是ActionContext数据中心 那些 request、session。。。

  参数接收原理->表单页name=tom->params拦截器=>交给ognl处理=>从root中拿到name属性,并赋值为tom->然后到达Action完成参数赋值。

  

  在配置文件中使用OGNL-重定向带动态参数

    <package name="hello" namespace="/demo" extends="struts-default">
        <action name="demo1" class="com.struts2.web.HelloAction" method="hello">
            <result name="success" type="redirectAction">
                <param name="actionName">demo2</param>
                <param name="namespace">/demo</param>
                <!--参数-->
                <param name="name">${name}</param>
            </result>
        </action>

        <action name="demo2" class="com.struts2.web.Hello1Action" method="hello1">
            <result name="success">/demo1.jsp</result>
        </action>
    </package>

helloAction 和hello1Action

public class HelloAction extends ActionSupport {
    private String name;
    public String hello(){
        System.out.println(name);
        return "success";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
public class Hello1Action {
    private String name;
    public String hello1(){
        System.out.println(name);
        return "success";
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName(){
        return this.name;
    }
}

提交页

  <form action="${pageContext.request.contextPath}/demo/demo1" method="post">
    <input name="name" type="text">
    <button type="submit">提交</button>
  </form>

重定向后页

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!--引入标签 -->
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
  <head>
  </head>
  <body>
  姓名:<s:property value="name" />
  </body>
</html>

 

posted @ 2018-05-22 11:44  海盗船长  阅读(201)  评论(0编辑  收藏  举报