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