扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
/* * J2J.java * * Created on 2006年10月2日, 下午7:16 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package net.vlinux.tag.j2j; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.lang.reflect.*; import java.util.*; /** * * @author vlinux */ public class NewObject extends TagSupport { private String dist; private String distName; private String scope; private String source; private List<Method> getGetMethods( Object aObject ) { Method[] array = aObject.getClass().getMethods(); List<Method> list = new ArrayList<Method>(); for( int i=0;i<array.length;i++ ){ String methodName = array[i].getName(); if( methodName.matches("get.*") ) list.add(array[i]); } return list; } private String getFieldName( Method aMethod){ String methodName = aMethod.getName(); String subName = methodName.substring(3,methodName.length()); return subName.toLowerCase(); } private Object getSourceObject(String scope, String source){ if( scope.equals("request") ){ return pageContext.getRequest().getAttribute(source); }else if( scope.equals("session") ){ return pageContext.getSession().getAttribute(source); }else if( scope.equals("page") ){ return pageContext.getAttribute(source); }else{ System.out.println("xxx"); return null; } } public int doStartTag(){ JspWriter out = pageContext.getOut(); Object sourceObject = getSourceObject(getScope(),getSource()); List list = getGetMethods( sourceObject ); try{ out.println( "<script>" ); out.println( "\tvar " + getDistName() + " = new " + getDist() + "();"); for( int i=0;i<list.size();i++ ){ try{ String fieldName = getFieldName((Method)list.get(i)); String value = ((Method)list.get(i)).invoke( getSourceObject(getScope(),getSource())).toString(); out.println( "\t"+getDistName() + "." + fieldName + " = \"" + value +"\""); }catch(Exception e){ // } } out.println( "</script>" ); }catch( java.io.IOException ioe){ // } return (EVAL_BODY_INCLUDE); } public int doEndTag(){ return (EVAL_PAGE); } public String getDist() { return dist; } public void setDist(String dist) { this.dist = dist; } public String getDistName() { return distName; } public void setDistName(String distName) { this.distName = distName; } public String getScope() { return scope; } public void setScope(String scope) { this.scope = scope; } public String getSource() { return source; } public void setSource(String source) { this.source = source; } } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者