struts2学习之OGNL访问复杂对象值(八)

OGNL表达式
ognl访问复杂对象
1. 访问javaBean对象
2. 访问集合对象
3. 访问Map对象
javabean类

package com.newbeedaly.model;

public class Student {

    private String name;
    private int age;



    public Student() {
        super();
        // TODO Auto-generated constructor stub
    }


    public Student(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }


    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;
    }


}

action请求方法

package com.newbeedaly.action;

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

import com.newbeedaly.model.Student;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;

public class HelloAction extends ActionSupport{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Student student;
    private List<Student> students;
    private Map<String,Student> studentMap;

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

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



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

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



    public Student getStudent() {
        return student;
    }

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

    @Override
    public String execute() throws Exception {
        ActionContext actionContext=ActionContext.getContext();


        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;
    }




}

jsp页面

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/>
ongl访问静态方法和属性,启动tomcat,访问jsp页面
        1. 访问静态属性
        2. 访问静态方法,默认无法访问,在struts2.xml中struts标签中添加
        <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant> 
        jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
访问静态属性: <s:property value="@com.newbeedaly.common.MyStatic@str"/><br/>
访问静态方法:<s:property value="@com.newbeedaly.common.MyStatic@printUrl()"/>
</body>
</html>

java类

package com.newbeedaly.common;

public class MyStatic {

    public static final String str="newbeedaly.cn";

    public static String printUrl(){
        System.out.println("http://www.newbeedaly.cn");
        return "http://www.newbeedaly.cn";
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值