码迷,mamicode.com
首页 > 其他好文 > 详细

Struts2框架使用(六)之ognl表达式获取值

时间:2017-12-19 19:43:00      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:bean   数据   nal   blog   getname   ica   lan   context   actions   

OGNL 是对象图导航语言 Object-Graph Navigation Language 的缩写,它是一种功能强大的表达式语言。

我们可以使用ognl获取很多值。

例如

我们先编写一个Action,存入需要读取的数据。

package com.mrlv.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mrlv.pojo.Student;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;

public class IndexAction extends ActionSupport{

    private String name;
    private int age;
    private Student student;
    private List<Student> students;
    private Map<String, Student> studentMap;
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Override
    public String execute() throws Exception {
        System.out.println("执行IndexAction");
        ActionContext context = ActionContext.getContext();
        // 获取狭义上的值栈
        ValueStack valueStack = context.getValueStack();
        valueStack.set("name", "张三(valueStack)");
        valueStack.set("age", 11);
        
        //获取二次封装的session
        Map<String, Object> session = context.getSession();
        session.put("name", "王五(session)");
        session.put("age", 13);
        
        //获取application
        Map<String, Object> application = context.getApplication();
        application.put("name", "赵六(application)");
        application.put("age", 14);
        
        student = new Student("小七", 12);
        
        students = new ArrayList<Student>();
        students.add(new Student("老八",13));
        students.add(new Student("老九",14));
        
        studentMap = new HashMap<String,Student>();
        studentMap.put("goodStudent", new Student("学霸",20));
        studentMap.put("badStudent", new Student("学渣",19));
        
        return SUCCESS;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }

    public Map<String, Student> getStudentMap() {
        return studentMap;
    }

    public void setStudentMap(Map<String, Student> studentMap) {
        this.studentMap = studentMap;
    }
}

如果想要调用OGNL表达式访问数据,需要在jsp头部添加struts的标签。

<%@taglib prefix="s" uri="/struts-tags" %>

OGNL 访问 ValueStack(值栈) 数据,这里获取的是

张三(valueStack)11
<s:property value="name"/>
<s:property value="age"/><br>

OGNL 访问 ActionContext 数据,访问某个范围下的数据要用#,如下

#parameters 请求参数 request.getParameter(...),这里当输入的url中携带?name=AAA&age=12,则会输出

AAA12
<s:property value="#parameters.name"/>
<s:property value="#parameters.age"/><br>

#request 请求作用域中的数据 request.getAttribute(...),输出结果

李四(request)12
  <%
     request.setAttribute("name", "李四(request)");
     request.setAttribute("age", "12");
  %>

 

<s:property value="#request.name" />
<s:property value="#request.age"/><br>

 

#session 会话作用域中的数据 session.getAttribute(...),输出结果

王五(session)13
<s:property value="#session.name" />
<s:property value="#session.age"/><br>

 

#application 应用程序作用域中的数据 application.getAttribute(...),输出结果

赵六(application)13
<s:property value="#application.name" />
<s:property value="#application.age"/><br>

 

#attr 按照 page request session application 顺序查找值,输出结果

李四(request)12
<s:property value="#attr.name"/>
<s:property value="#attr.age"/><br>

 OGNL获取集合类的时候方法如下:

  ognl访问javaBean对象:<s:property value="student.name"/><s:property value="student.age"/><br>
  ognl访问List集合:<s:property value="students[0].name"/>
  <s:property value="students[0].age"/><br>
  <s:property value="students[1].name"/>
  <s:property value="students[1].age"/><br>
  ognl访问Map:<s:property value="studentMap[‘goodStudent‘].name"/>
  <s:property value="studentMap[‘goodStudent‘].age"/><br>
  <s:property value="studentMap[‘badStudent‘].name"/>
  <s:property value="studentMap[‘badStudent‘].age"/><br>

OGNL也可以通过表达式获取静态变量,或访问静态方法。

如果想要访问静态方法的话,需要在struts.xml上添加一条配置语句,访问静态变量则不用。

<!-- 使用访问静态方法的话,需要在struts.xml配置开启 -->
  <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant> 

 

第一个@后面携带的是类的路径名,第二个@携带的是静态变量或方法。

访问静态属性: <s:property value="@com.mrlv.common.MyStatic@str"/><br/>
  <!-- 使用访问静态方法的话,需要在struts.xml配置开启 -->
  访问静态方法:<s:property value="@com.mrlv.common.MyStatic@printUrl()"/>

 

Struts2框架使用(六)之ognl表达式获取值

标签:bean   数据   nal   blog   getname   ica   lan   context   actions   

原文地址:http://www.cnblogs.com/lvshiyu/p/8066032.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!