html中的selectBox也是令我头疼的东西,因为它总和数据库关联,但我们在jsp中又不能直接调用dao,
就算直接调用了dao,jsp中的代码也是一团乱。所以我专门写了这个类来解决这个问题。
我们只要专一个list给它,并告诉哪个方法可以得到key,哪个方法可以得到value,
就能生成一个selectBox。
package afu.framework.util;

import java.util.*;
import java.lang.reflect.*;

public class HtmlUtil


{
private List list;
private String getKeyMethodName;
private String getValueMethodName;
private Method getKeyMethod;
private Method getValueMethod;
public HtmlUtil(final List list,final String getKeyMethodName,final String getValueMethodName)

{
this.list = list;
this.getKeyMethodName = getKeyMethodName;
this.getValueMethodName = getValueMethodName;
}

/** *//**
* build a select box,the value type is int
* so the [selectedIndex] is a int
*/
public String buildSelectBox(final String name,final int width,final int selectedIndex)
throws BuildHtmlComponentException,InvocationTargetException,IllegalAccessException

{
checkElement("int");
StringBuffer box = new StringBuffer(200);
box.append("<select size=1 name='").append(name).append("'");
box.append(" style='width:").append(width).append("px;'>");
for(int i=0;i<list.size();i++)

{
Object obj = list.get(i);
int key = (Integer)getKeyMethod.invoke(obj);
if(key==selectedIndex)
box.append("<option value='" + key + "' selected>");
else
box.append("<option value='" + key + "'>");
box.append(getValueMethod.invoke(obj));
box.append("</option>");
}
box.append("</select>");
return box.toString();
}

/** *//**
* build a select box,the value type is String
* so the [selectedIndex] is a String
*/
public String buildSelectBox(final String name,final int width,final String selectedIndex)
throws BuildHtmlComponentException,InvocationTargetException,IllegalAccessException

{
checkElement("String");
StringBuffer box = new StringBuffer(200);
box.append("<select size=1 name='").append(name).append("'");
box.append(" style='width:").append(width).append("px;'>");
for(int i=0;i<list.size();i++)

{
Object obj = list.get(i);
String key = (String)getKeyMethod.invoke(obj);
if(key.equals(selectedIndex))
box.append("<option value='" + key + "' selected>");
else
box.append("<option value='" + key + "'>");
box.append(getValueMethod.invoke(obj));
box.append("</option>");
}
box.append("</select>");
return box.toString();
}
public String getValue(int index)

{
String result = null;
try

{
checkElement("int");
for(int i=0;i<list.size();i++)

{
Object obj = list.get(i);
int key = (Integer)getKeyMethod.invoke(obj);
if(key==index)

{
result = (String)getValueMethod.invoke(obj);
break;
}
}
}
catch(Exception e)

{
//e.printStackTrace();
}
return result;
}
public String getValue(String index)

{
String result = null;
try

{
checkElement("String");
for(int i=0;i<list.size();i++)

{
Object obj = list.get(i);
String key = (String)getKeyMethod.invoke(obj);
if(key.equals(index))

{
result = (String)getValueMethod.invoke(obj);
break;
}
}
}
catch(Exception e)

{
e.printStackTrace();
}
return result;
}
private void checkElement(String type) throws BuildHtmlComponentException

{
if(list==null || list.size()==0)
throw new BuildHtmlComponentException("element set is empty.");
Object element = list.get(0);
getKeyMethod = SysUtil.lookupMethod(element.getClass().getMethods(), getKeyMethodName);
if(getKeyMethod==null)
throw new BuildHtmlComponentException("method " + getKeyMethodName + " does not exist.");
if(!getKeyMethod.getReturnType().toString().equals(type))
throw new BuildHtmlComponentException("method " + getKeyMethodName + " return type is not " + type);
getValueMethod = SysUtil.lookupMethod(element.getClass().getMethods(), getValueMethodName);
if(getValueMethod==null)
throw new BuildHtmlComponentException("method " + getValueMethodName + " does not exist.");
}
}
调用时,这样写HtmlUtil
html = new HtmlUtil(roles,"getId","getRole");