下面的代码可以用来实现,jsp为什么就是一个servlet的特点。<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@page import="org.skycn.tianzhw.entity.*"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@taglib uri="http://www.skycn.com" prefix="tianzhw"%> <% Contact contact = new Contact(); Address address = new Address(); Teacher teacher = new Teacher(); User user = new User(); contact.setMobileTelephone("1234560956870-983498572345"); contact.setEMail("tianzhw@vip.qq.com"); user.setName("zhongguo"); user.setAge("12"); user.setAddress(address); user.setTeacher(teacher); teacher.setName("zxx"); teacher.setAge("20"); teacher.setAddress(address); address.setArea("beijing"); address.setCity("beijing"); address.setContact(contact); address.setProvince("hebei"); request.setAttribute("user",user); session.setAttribute("teacher",teacher); application.setAttribute("name","nihao shijie"); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css"> --> </head> <body>" ${user.address.contact.mobileTelephone} <br> <%=org.skycn.tianzhw.AnalyseBean.analyse("${user.teacher.name}",pageContext) %> <br> ${tianzhw:test("成功") } </body> </html> 上面的页面,是一个数据准备阶段。 package org.skycn.tianzhw; import java.lang.reflect.InvocationTargetException; import javax.servlet.ServletContext; import javax.servlet.ServletRequest; import javax.servlet.http.HttpSession; import javax.servlet.jsp.PageContext; import com.sun.org.apache.commons.beanutils.BeanUtils; import com.sun.org.apache.commons.beanutils.MethodUtils; import com.sun.org.apache.commons.beanutils.PropertyUtils; public class AnalyseBean { private String src; private PageContext context; public AnalyseBean(String src, PageContext context) { this.src = src; this.context = context; } public static Object analyse(String src, PageContext context) { //截取字符串 int _tempOne = src.indexOf("{"); int _tempTwo = src.indexOf("}"); src = src.substring(_tempOne + 1, _tempTwo); //以"."为分隔符,截取字符串 String[] strs = src.split("//."); int size = strs.length; //获得全部作用域对象 ServletRequest request = context.getRequest(); HttpSession session = context.getSession(); ServletContext servlet = context.getServletContext(); //无论字符串的长度是多长,这里面不考虑字符为null,或者格式不正确的错误。 Object res = null; Object obj = context.getAttribute(strs[0]); if (null == obj) { obj = request.getAttribute(strs[0]); if (null == obj) { obj = session.getAttribute(strs[0]); if (null == obj) { obj = servlet.getAttribute(strs[0]); if (null == obj) { throw new RuntimeException( "net exist Object in any airspace"); } } } } //思路: 如果String数组的长度是2,那么就一定是一个对象的一个属性值,长度是3,就是说一个对象中包涵一个自定义对象的属性值。。。。。。那么 //那么如果有N个长度,就是有N-1个对象的属性值。 try { //长度小于2的时候,直接就是在全部作用域中查找,返回这个属性 if( 2 > size ) { return obj; } if (2 == size) { res = BeanUtils.getProperty(obj, strs[1]); } //思路:定义一个控制变量i,初始化值为1,当size大于2的时候, 数组strs[size - 1]一定是最后的一个对象。 if (2 < size) { int i = 1; Object obj2 = null ; while (!((size - i)==1)){ obj2 = PropertyUtils.getProperty(obj, strs[i]); obj = obj2 ; i++; } res = BeanUtils.getProperty(obj2, strs[size - 1]); } } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } return res; } } 上面的代码,就是如何实现的类。有更加方便的实现方法,我的这个类里面实现了,使用beanUtils支持。