| Method from org.apache.struts2.components.UIBean Detail: |
public void addFormParameter(String key,
Object value) {
Form form = (Form) findAncestor(Form.class);
if (form != null) {
form.addParameter(key, value);
}
}
|
protected Template buildTemplateName(String myTemplate,
String myDefaultTemplate) {
String template = myDefaultTemplate;
if (myTemplate != null) {
template = findString(myTemplate);
}
String templateDir = getTemplateDir();
String theme = getTheme();
return new Template(templateDir, theme, template);
}
|
protected void enableAncestorFormCustomOnsubmit() {
Form form = (Form) findAncestor(Form.class);
if (form != null) {
form.addParameter("customOnsubmitEnabled", Boolean.TRUE);
} else {
LOG.warn("Cannot find an Ancestor form, custom onsubmit is NOT enabled");
}
}
|
public boolean end(Writer writer,
String body) {
evaluateParams();
try {
super.end(writer, body, false);
mergeTemplate(writer, buildTemplateName(template, getDefaultTemplate()));
} catch (Exception e) {
LOG.error("error when rendering", e);
}
finally {
popComponentStack();
}
return false;
}
|
protected String ensureAttributeSafelyNotEscaped(String val) {
if (val != null) {
return val.replaceAll("\"", """);
} else {
return "";
}
}
Ensures an unescaped attribute value cannot be vulnerable to XSS attacks |
protected String escape(String name) {
// escape any possible values that can make the ID painful to work with in JavaScript
if (name != null) {
return name.replaceAll("[\\.\\[\\]]", "_");
} else {
return "";
}
}
|
protected void evaluateExtraParams() {
}
|
protected boolean evaluateNameValue() {
return true;
}
|
public void evaluateParams() {
addParameter("templateDir", getTemplateDir());
addParameter("theme", getTheme());
String name = null;
if (this.key != null) {
if(this.name == null) {
this.name = key;
}
if(this.label == null) {
this.label = "%{getText('"+ key +"')}";
}
}
if (this.name != null) {
name = findString(this.name);
addParameter("name", name);
}
if (label != null) {
addParameter("label", findString(label));
}
if (labelPosition != null) {
addParameter("labelposition", findString(labelPosition));
}
if (requiredposition != null) {
addParameter("requiredposition", findString(requiredposition));
}
if (required != null) {
addParameter("required", findValue(required, Boolean.class));
}
if (disabled != null) {
addParameter("disabled", findValue(disabled, Boolean.class));
}
if (tabindex != null) {
addParameter("tabindex", findString(tabindex));
}
if (onclick != null) {
addParameter("onclick", findString(onclick));
}
if (ondblclick != null) {
addParameter("ondblclick", findString(ondblclick));
}
if (onmousedown != null) {
addParameter("onmousedown", findString(onmousedown));
}
if (onmouseup != null) {
addParameter("onmouseup", findString(onmouseup));
}
if (onmouseover != null) {
addParameter("onmouseover", findString(onmouseover));
}
if (onmousemove != null) {
addParameter("onmousemove", findString(onmousemove));
}
if (onmouseout != null) {
addParameter("onmouseout", findString(onmouseout));
}
if (onfocus != null) {
addParameter("onfocus", findString(onfocus));
}
if (onblur != null) {
addParameter("onblur", findString(onblur));
}
if (onkeypress != null) {
addParameter("onkeypress", findString(onkeypress));
}
if (onkeydown != null) {
addParameter("onkeydown", findString(onkeydown));
}
if (onkeyup != null) {
addParameter("onkeyup", findString(onkeyup));
}
if (onselect != null) {
addParameter("onselect", findString(onselect));
}
if (onchange != null) {
addParameter("onchange", findString(onchange));
}
if (accesskey != null) {
addParameter("accesskey", findString(accesskey));
}
if (cssClass != null) {
addParameter("cssClass", findString(cssClass));
}
if (cssStyle != null) {
addParameter("cssStyle", findString(cssStyle));
}
if (title != null) {
addParameter("title", findString(title));
}
// see if the value was specified as a parameter already
if (parameters.containsKey("value")) {
parameters.put("nameValue", parameters.get("value"));
} else {
if (evaluateNameValue()) {
final Class valueClazz = getValueClassType();
if (valueClazz != null) {
if (value != null) {
addParameter("nameValue", findValue(value, valueClazz));
} else if (name != null) {
String expr = name;
if (altSyntax()) {
expr = "%{" + expr + "}";
}
addParameter("nameValue", findValue(expr, valueClazz));
}
} else {
if (value != null) {
addParameter("nameValue", findValue(value));
} else if (name != null) {
addParameter("nameValue", findValue(name));
}
}
}
}
final Form form = (Form) findAncestor(Form.class);
// create HTML id element
populateComponentHtmlId(form);
if (form != null ) {
addParameter("form", form.getParameters());
if ( name != null ) {
// list should have been created by the form component
List tags = (List) form.getParameters().get("tagNames");
tags.add(name);
}
}
// tooltip & tooltipConfig
if (tooltipConfig != null) {
addParameter("tooltipConfig", findValue(tooltipConfig));
}
if (tooltip != null) {
addParameter("tooltip", findString(tooltip));
Map tooltipConfigMap = getTooltipConfig(this);
if (form != null) { // inform the containing form that we need tooltip javascript included
form.addParameter("hasTooltip", Boolean.TRUE);
// tooltipConfig defined in component itseilf will take precedence
// over those defined in the containing form
Map overallTooltipConfigMap = getTooltipConfig(form);
overallTooltipConfigMap.putAll(tooltipConfigMap); // override parent form's tooltip config
for (Iterator i = overallTooltipConfigMap.entrySet().iterator(); i.hasNext(); ) {
Map.Entry entry = (Map.Entry) i.next();
addParameter((String) entry.getKey(), entry.getValue());
}
}
else {
LOG.warn("No ancestor Form found, javascript based tooltip will not work, however standard HTML tooltip using alt and title attribute will still work ");
}
}
evaluateExtraParams();
}
|
abstract protected String getDefaultTemplate()
A contract that requires each concrete UI Tag to specify which template should be used as a default. For
example, the CheckboxTab might return "checkbox.vm" while the RadioTag might return "radio.vm". This value
not begin with a '/' unless you intend to make the path absolute rather than relative to the
current theme. |
public String getTemplate() {
return template;
}
|
public String getTemplateDir() {
String templateDir = null;
if (this.templateDir != null) {
templateDir = findString(this.templateDir);
}
// If templateDir is not explicitly given,
// try to find attribute which states the dir set to use
if ((templateDir == null) || (templateDir.equals(""))) {
templateDir = (String) stack.findValue("#attr.templateDir");
}
// Default template set
if ((templateDir == null) || (templateDir.equals(""))) {
templateDir = defaultTemplateDir;
}
// Defaults to 'template'
if ((templateDir == null) || (templateDir.equals(""))) {
templateDir = "template";
}
return templateDir;
}
|
public String getTheme() {
String theme = null;
if (this.theme != null) {
theme = findString(this.theme);
}
if ( theme == null || theme.equals("") ) {
Form form = (Form) findAncestor(Form.class);
if (form != null) {
theme = form.getTheme();
}
}
// If theme set is not explicitly given,
// try to find attribute which states the theme set to use
if ((theme == null) || (theme.equals(""))) {
theme = (String) stack.findValue("#attr.theme");
}
// Default theme set
if ((theme == null) || (theme.equals(""))) {
theme = defaultUITheme;
}
return theme;
}
|
protected Map getTooltipConfig(UIBean component) {
Object tooltipConfigObj = component.getParameters().get("tooltipConfig");
Map tooltipConfig = new LinkedHashMap();
if (tooltipConfigObj instanceof Map) {
// we get this if its configured using
// 1] UI component's tooltipConfig attribute OR
// 2] < param name="tooltip" value="" / > param tag value attribute
tooltipConfig = new LinkedHashMap((Map)tooltipConfigObj);
} else if (tooltipConfigObj instanceof String) {
// we get this if its configured using
// < param name="tooltipConfig" > ... < /param > tag's body
String tooltipConfigStr = (String) tooltipConfigObj;
String[] tooltipConfigArray = tooltipConfigStr.split("\\|");
for (int a=0; a< tooltipConfigArray.length; a++) {
String[] configEntry = ((String)tooltipConfigArray[a].trim()).split("=");
String key = configEntry[0].trim();
String value = null;
if (configEntry.length > 1) {
value = configEntry[1].trim();
tooltipConfig.put(key, value.toString());
}
else {
LOG.warn("component "+component+" tooltip config param "+key+" has no value defined, skipped");
}
}
}
return tooltipConfig;
}
|
protected Class getValueClassType() {
return String.class;
}
|
protected void mergeTemplate(Writer writer,
Template template) throws Exception {
final TemplateEngine engine = templateEngineManager.getTemplateEngine(template, templateSuffix);
if (engine == null) {
throw new ConfigurationException("Unable to find a TemplateEngine for template " + template);
}
if (LOG.isDebugEnabled()) {
LOG.debug("Rendering template " + template);
}
final TemplateRenderingContext context = new TemplateRenderingContext(template, writer, getStack(), getParameters(), this);
engine.renderTemplate(context);
}
|
protected void populateComponentHtmlId(Form form) {
String tryId;
if (id != null) {
// this check is needed for backwards compatibility with 2.1.x
if (altSyntax()) {
tryId = findString(id);
} else {
tryId = id;
}
} else if (form != null) {
tryId = form.getParameters().get("id") + "_"
+ escape(name != null ? findString(name) : null);
} else {
tryId = escape(name != null ? findString(name) : null);
}
addParameter("id", tryId);
addParameter("escapedId", escape(tryId));
}
Create HTML id element for the component and populate this component parmaeter
map. Additionally, a parameter named escapedId is populated which contains the found id value filtered by
#escape(String) , needed eg. for naming Javascript identifiers based on the id value.
The order is as follows :-
- This component id attribute
- [containing_form_id]_[this_component_name]
- [this_component_name]
|
public void setAccesskey(String accesskey) {
this.accesskey = accesskey;
}
|
public void setCssClass(String cssClass) {
this.cssClass = cssClass;
}
|
public void setCssStyle(String cssStyle) {
this.cssStyle = cssStyle;
}
|
public void setDefaultTemplateDir(String dir) {
this.defaultTemplateDir = dir;
}
|
public void setDefaultUITheme(String theme) {
this.defaultUITheme = theme;
}
|
public void setDisabled(String disabled) {
this.disabled = disabled;
}
|
public void setKey(String key) {
this.key = key;
}
|
public void setLabel(String label) {
this.label = label;
}
|
public void setLabelposition(String labelPosition) {
this.labelPosition = labelPosition;
}
|
public void setName(String name) {
this.name = name;
}
|
public void setOnblur(String onblur) {
this.onblur = onblur;
}
|
public void setOnchange(String onchange) {
this.onchange = onchange;
}
|
public void setOnclick(String onclick) {
this.onclick = onclick;
}
|
public void setOndblclick(String ondblclick) {
this.ondblclick = ondblclick;
}
|
public void setOnfocus(String onfocus) {
this.onfocus = onfocus;
}
|
public void setOnkeydown(String onkeydown) {
this.onkeydown = onkeydown;
}
|
public void setOnkeypress(String onkeypress) {
this.onkeypress = onkeypress;
}
|
public void setOnkeyup(String onkeyup) {
this.onkeyup = onkeyup;
}
|
public void setOnmousedown(String onmousedown) {
this.onmousedown = onmousedown;
}
|
public void setOnmousemove(String onmousemove) {
this.onmousemove = onmousemove;
}
|
public void setOnmouseout(String onmouseout) {
this.onmouseout = onmouseout;
}
|
public void setOnmouseover(String onmouseover) {
this.onmouseover = onmouseover;
}
|
public void setOnmouseup(String onmouseup) {
this.onmouseup = onmouseup;
}
|
public void setOnselect(String onselect) {
this.onselect = onselect;
}
|
public void setRequired(String required) {
this.required = required;
}
|
public void setRequiredposition(String requiredposition) {
this.requiredposition = requiredposition;
}
|
public void setTabindex(String tabindex) {
this.tabindex = tabindex;
}
|
public void setTemplate(String template) {
this.template = template;
}
|
public void setTemplateDir(String templateDir) {
this.templateDir = templateDir;
}
|
public void setTemplateEngineManager(TemplateEngineManager mgr) {
this.templateEngineManager = mgr;
}
|
public void setTheme(String theme) {
this.theme = theme;
}
|
public void setTitle(String title) {
this.title = title;
}
|
public void setTooltip(String tooltip) {
this.tooltip = tooltip;
}
|
public void setTooltipConfig(String tooltipConfig) {
this.tooltipConfig = tooltipConfig;
}
|
public void setValue(String value) {
this.value = value;
}
|