ASF Bugzilla – Attachment 11777 Details for
Bug 29223
[PROPOSAL] Tag turns a collection into a javascript function to populate select.
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
Updated to use commons libraries.
JavaScriptCollection.java (text/plain), 3.48 KB, created by
Edgar Dollin
on 2004-06-07 12:25:31 UTC
(
hide
)
Description:
Updated to use commons libraries.
Filename:
MIME Type:
Creator:
Edgar Dollin
Created:
2004-06-07 12:25:31 UTC
Size:
3.48 KB
patch
obsolete
>/* > * Created on Apr 13, 2004 > */ >package net.bmllc.tags; > >import java.io.Serializable; >import java.util.Collection; >import java.util.Iterator; > >import javax.servlet.jsp.JspException; >import javax.servlet.jsp.JspWriter; >import javax.servlet.jsp.tagext.TagSupport; > >import org.apache.commons.beanutils.BeanUtils; >import org.apache.commons.lang.StringEscapeUtils; >import org.apache.commons.logging.Log; >import org.apache.commons.logging.LogFactory; > >/** > * @author edgar > * > */ >public class JavaScriptCollection extends TagSupport implements Serializable { > > private static Log log = LogFactory.getLog(JavaScriptCollection.class); > > /** > * output a javascript function to add elements of a collection > * to a select > */ > > String id; > public String getId() { > return id; > } > public void setId(String string) { > id = string; > } > > String function; > public String getFunction() { > return function; > } > public void setFunction(String string) { > function = string; > } > > String label = "label"; > public String getLabel() { > return label; > } > public void setLabel(String label) { > this.label = label; > } > > String value = "value"; > public String getValue() { > return value; > } > public void setValue(String value) { > this.value = value; > } > > public JavaScriptCollection() { > super(); > } > > /** > * output the javascript > */ > > public int doStartTag() throws JspException { > if (function == null) { > return super.doStartTag(); > } > if (id == null) { > return super.doStartTag(); > } > Collection c = (Collection) pageContext.findAttribute(id); > if (c == null) { > return super.doStartTag(); > } > > JspWriter out; > try { > out = pageContext.getOut(); > } catch (Exception e) { > log.fatal("doStartTag: no JspWriter object", e); > return super.doStartTag(); > } > > try { > out.println("<script type='text/javascript'>"); > out.println("function " + getFunction() + "(fieldName, value) {"); > out.println(" if (fieldName == null) {"); out.println(" alert('" + getFunction() + " has been invoked with a null fieldName');"); > out.println(" return;"); > out.println(" };"); > out.println(" var f = Struts_eval(fieldName, 0);"); > out.println(" if (f == null) {"); > out.println(" alert('field ' + fieldName + ' does not resolve');"); > out.println(" return;"); > out.println(" };"); > out.println(" if (f.options == null) {"); out.println(" alert('field ' + fieldName + ' is not a select field');"); > out.println(" return;"); > out.println(" };"); > for (Iterator i = c.iterator(); i.hasNext();) { > Object o = i.next(); > if (o == null) { > continue; > } > > out.print(" f.options[f.options.length] = new Option('"); > out.print(StringEscapeUtils.escapeJavaScript(BeanUtils.getProperty(o, getLabel()))); > out.print("','"); > out.print(StringEscapeUtils.escapeJavaScript(BeanUtils.getProperty(o, getValue()))); > out.println("');"); > } > > out.println(" if (value == null) return;"); > out.println(" for (i = 0; i < f.options.length; i++) {"); > out.println(" if (value == f.options[i].value) {"); > out.println(" f.options[i].selected=true"); > out.println(" break;"); > out.println(" }"); > out.println(" }"); > out.println("}"); > out.println("</script>"); > } > > catch (Exception e) { > log.fatal("doStartTag: io error writing javascript",e); > return super.doStartTag(); > } > > return super.doStartTag(); > } > >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 29223
:
11675
|
11676
| 11777