tag 탭 출력 대상,request,map 등의 값

3282 단어
java :
package com.util.tag.html;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

@SuppressWarnings("serial")
public class LabelTag extends BodyTagSupport {
private String property = null;
@SuppressWarnings("unchecked")
@Override
public int doStartTag() throws JspException {
HttpServletRequest request = (HttpServletRequest)this.pageContext.getRequest();
String[] str = property.split("/");
Object targetObject = request.getAttribute(str[0]);
Object targetValue = null ;

try {
if(targetObject instanceof String){
targetValue = targetObject;
}else if(targetObject instanceof Map){
targetValue = ((Map)targetObject).get(str[1]);
}else{
targetValue = labelForJavaBean(targetObject,str[1]);
}
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
try {
this.pageContext.getOut().write(targetValue.toString());
} catch (IOException e) {
e.printStackTrace();
}
return super.doStartTag();
}
/**
*  javabean 
* @author  yao
* @param targetObject
* @param propertyName
* @return 
* @throws ClassNotFoundException
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
@SuppressWarnings("unchecked")
public Object labelForJavaBean(Object targetObject,String propertyName) throws ClassNotFoundException,
NoSuchMethodException,InvocationTargetException,IllegalAccessException{
StringBuffer sb = new StringBuffer();
Class clazz = Class.forName(targetObject.getClass().getName());
sb.append("get").append(upperFirstWord(propertyName));
Method method = clazz.getDeclaredMethod(sb.toString());
return method.invoke(targetObject);
}
public String upperFirstWord(String str){
String firstWord = str.substring(0,1);
return str.replaceFirst(firstWord, firstWord.toUpperCase());
}
public String getProperty() {
return property;
}

public void setProperty(String property) {
this.property = property;
}
}
HtmlTag.tld :
<?xml version="1.0" encoding="UTF-8" ?>  
<taglib  
 xmlns="http://java.sun.com/xml/ns/j2ee"  
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee  
 web-jsptaglibrary_2_0.xsd" verson="2.0">   
<tlib-version>1.0</tlib-version>
<jspversion>1.1</jspversion>
<shortname>yao</shortname>
<uri>http://com.yao/tags/html</uri>
<tag>
<name>label</name>
<tagclass>com.util.tag.html.LabelTag</tagclass>
<bodycontent>empty</bodycontent>
<attribute>
<name>property</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue> 
</attribute>
</tag>
</taglib> 

맵/id, object/attributeName

좋은 웹페이지 즐겨찾기