J2EE系列之Struts2学习笔记(十五)--OGNL访问复杂对象、静态属性和静态方法

来源:互联网 发布:怎么在手机上装修淘宝 编辑:程序博客网 时间:2024/06/05 21:56

这里记录一下OGNL如何获取javabean对象、集合对象、静态属性以及静态方法。在上一篇博客的工程基础上作如下的修改。

1.新建一个类Student:

package com.test.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;}}


2.修改HelloAction为:

package com.test.action;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import com.opensymphony.xwork2.ActionSupport;import com.test.model.Student;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;}@Overridepublic String execute() throws Exception {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("学霸",15));studentMap.put("badStudent", new Student("学渣",18));return SUCCESS;}}

我们在这里分别定义了javabean对象student,List集合对象和Map集合对象。接下来在jsp页面中获取这些对象的值。


3.success.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><%request.setAttribute("name", "李四(request)");request.setAttribute("age", 12);%></head><body>ognl访问javabean对象:<s:property value="student.name"/><s:property value="student.age"/><br/>EL表达式:${student.name } ${student.age }<br/>ognl访问List集合:<s:property value="students[0].name"/><s:property value="students[0].age"/><br/>EL表达式:${students[0].name } ${students[0].age }<br/>ognl访问Map集合:<s:property value="studentMap['goodStudent'].name"/><s:property value="studentMap['goodStudent'].age"/><br/>ognl访问Map集合:<s:property value="studentMap['badStudent'].name"/><s:property value="studentMap['badStudent'].age"/><br/>EL表达式:${studentMap['goodStudent'].name } ${studentMap['goodStudent'].age }</body></html>

这里分别以OGNL和EL表达式两种方式给出了访问这些对象值的方法。

4.程序运行结果:



OGNL不仅能访问对象,还能访问静态属性和静态方法。看如下示例:

1.新建一个MyStatic类:

package com.test.common;public class MyStatic {public static final String str="Struts2 练习";public static String printUrl(){System.out.println("struts2 练习");return "struts2 练习";}}

这个类中定义了一个静态属性str 和一个静态方法printUrl()。接下来在jsp页面中访问这两个元素。

2.新建一个ognl_static.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.test.common.MyStatic@str"/><br/>访问静态方法:<s:property value="@com.test.common.MyStatic@printUrl()"/></body></html>

这里给出了OGNL访问静态属性以及静态方法的固定格式。


3.Struts2默认是不能访问静态方法的,需要修改一下配置文件:

<struts>    <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>    <package name="manage" namespace="/" extends="struts-default">    <action name="hello" class="com.test.action.HelloAction">  <result name="success" type="dispatcher">success.jsp</result>  </action>    </package></struts>

4.程序运行结果为:


这里访问到了静态属性的值,并调用了静态方法。

0 0
原创粉丝点击