Update of /cvsroot/displaytag/search-mod/src/org/displaytag/search
In directory sc8-pr-cvs1:/tmp/cvs-serv23021
Added Files:
SearchTag.java
Log Message:
Put in a shell of the tag that already works with the exapmle-search jsp.
--- NEW FILE: SearchTag.java ---
package org.displaytag.search;
import org.apache.commons.beanutils.BeanUtils;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyTagSupport;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
/**
* Created by IntelliJ IDEA.
* User: <a href="mailto:[EMAIL PROTECTED]">Benjamin Simpson</a>
* Date: Jul 18, 2003
* Time: 11:07:45 AM
*
* @jsp:tag
* name="search"
* description="plug-in tag to modify table display through searches"
*/
public class SearchTag extends BodyTagSupport {
private String name;
/**
* @jsp:attribute
* required="true"
* rtexprvalue="false"
* type="String"
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int doStartTag() throws JspException {
try {
JspWriter writer = pageContext.getOut();
Collection coll = (Collection) pageContext.getAttribute(name,
PageContext.PAGE_SCOPE);
Object obj = coll.isEmpty() ? new Object() : coll.toArray()[0];
Field[] fieldArry = obj.getClass().getFields();
Method[] methArry = obj.getClass().getMethods();
Set fieldSet = new HashSet();
outer: for (int i = 0; i < methArry.length; i++) {
Method method = methArry[i];
String attrName = method.getName().toLowerCase();
if (method.getName().toLowerCase().startsWith("get") == false)
continue outer;
attrName = attrName.substring(3).toLowerCase();
inner: for (int j = 0; j < fieldArry.length; j++) {
Field field = fieldArry[j];
if (field.getName().toLowerCase().equals(attrName) == false)
continue inner;
fieldSet.add(field.getName());
continue outer;
}
}
writer.println("Search Table has " + coll.size() + " items in it.<br>");
writer.println("<table><tr><td><form action=\"\">Search<input
type=\"text\" name=\"search\">");
writer.println("<input type=\"submit\" name=\"GO!\"
value=\"cmd_displaytag_search\">");
writer.println("<select name=\"field\">");
writer.println("<option value=\"\">All Fields</option>");
Object[] fields = fieldSet.toArray();
for (int i = 0, len = fields.length; i < len; i++) {
Object field = fields[i];
writer.println("<option value=\"" + field + "\">" + field +
"</option>");
}
writer.println("</select>");
writer.println("</form></td></tr></table><br>");
writer.flush();
//@todo make this happen only once!
//@todo find out what the fields to be displayed are?
//@todo use * characters (how many??)
//filter the object and put back!
String parm = pageContext.getRequest().getParameter("search");
String field = pageContext.getRequest().getParameter("field");
if (parm == null) return BodyTagSupport.SKIP_BODY;
if (field == null || field.trim().length() == 0) return
BodyTagSupport.SKIP_BODY;
Object[] rows = coll.toArray();
LinkedList list = new LinkedList(coll);
for (int i = 0; i < rows.length; i++) {
String value = BeanUtils.getProperty(rows[i], field);
if (value.indexOf(parm) > -1) continue;
list.remove(rows[i]);
}
System.out.println("*** setting back obj of size " + list.size());
pageContext.setAttribute(name, list, PageContext.PAGE_SCOPE);
} catch (Exception e) {
e.printStackTrace();
}
return BodyTagSupport.SKIP_BODY;
}
}
-------------------------------------------------------
This SF.net email is sponsored by: VM Ware
With VMware you can run multiple operating systems on a single machine.
WITHOUT REBOOTING! Mix Linux / Windows / Novell virtual machines at the
same time. Free trial click here: http://www.vmware.com/wl/offer/345/0
_______________________________________________
displaytag-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/displaytag-devel