FileDocCategorySizeDatePackage
UIBean.javaAPI DocExample34225Mon Jul 23 13:26:36 BST 2007org.apache.struts2.components

UIBean

public abstract class UIBean extends Component
UIBean is the standard superclass of all Struts UI componentns. It defines common Struts and html properties all UI components should present for usage.
Attribute Theme Data Types Description
templateDir n/a String define the template directory
theme n/a String define the theme name
template n/a String define the template name

Attribute Theme Data Types Description
cssClass simple String define html class attribute
cssStyle simple String define html style attribute
title simple String define html title attribute
disabled simple String define html disabled attribute
label xhtml String define label of form element
labelPosition xhtml String define label position of form element (top/left), default to left
requiredposition xhtml String define required label position of form element (left/right), default to right
name simple String Form Element's field name mapping
required xhtml Boolean add * to label (true to add false otherwise)
tabIndex simple String define html tabindex attribute
value simple Object define value of form element

Attribute Theme Data Types Description
onclick simple String html javascript onclick attribute
ondbclick simple String html javascript ondbclick attribute
onmousedown simple String html javascript onmousedown attribute
onmouseup simple String html javascript onmouseup attribute
onmouseover simple String html javascript onmouseover attribute
onmouseout simple String html javascript onmouseout attribute
onfocus simple String html javascript onfocus attribute
onblur simple String html javascript onblur attribute
onkeypress simple String html javascript onkeypress attribute
onkeyup simple String html javascript onkeyup attribute
onkeydown simple String html javascript onkeydown attribute
onselect simple String html javascript onselect attribute
onchange simple String html javascript onchange attribute

Attribute Data Type Default Description
tooltip String none Set the tooltip of this particular component
jsTooltipEnabled String false Enable js tooltip rendering
tooltipIcon String /struts/static/tooltip/tooltip.gif The url to the tooltip icon
tooltipDelay String 500 Tooltip shows up after the specified timeout (miliseconds). A behavior similar to that of OS based tooltips.
key simple String The name of the property this input field represents. This will auto populate the name, label, and value
Every Form UI component (in xhtml / css_xhtml or any others that extends of them) could have tooltip assigned to a them. The Form component's tooltip related attribute once defined will be applicable to all form UI component that is created under it unless explicitly overriden by having the Form UI component itself defined that tooltip attribute.

In Example 1, the textfield will inherit the tooltipDelay adn tooltipIcon attribte from its containing form. In other words, although it doesn't defined a tooltipAboveMousePointer attribute, it will have that attributes inherited from its containing form.

In Example 2, the the textfield will inherite both the tooltipDelay and tooltipIcon attribute from its containing form but tooltipDelay attribute is overriden at the textfield itself. Hence, the textfield actually will have tooltipIcon defined as /myImages/myIcon.gif, inherited from its containing form and tooltipDelay defined as 5000, due to overriden at the textfield itself.

Example 3, 4 and 5 shows different way of setting the tooltipConfig attribute.
Example 3:Set tooltip config through body of param tag
Example 4:Set tooltip config through value attribute of param tag
Example 5:Set tooltip config through tooltipConfig attribute of component tag



<!-- Example 1: -->
<s:form
tooltipConfig="#{'tooltipDelay':'500',
'tooltipIcon='/myImages/myIcon.gif'}" .... >
....
<s:textfield label="Customer Name" tooltip="Enter the customer name" .... />
....
</s:form>

<!-- Example 2: -->
<s:form
tooltipConfig="#{'tooltipDelay':'500',
'tooltipIcon':'/myImages/myIcon.gif'}" ... >
....
<s:textfield label="Address"
tooltip="Enter your address"
tooltipConfig="#{'tooltipDelay':'5000'}" />
....
</s:form>


<-- Example 3: -->
<s:textfield
label="Customer Name"
tooltip="One of our customer Details'">
<s:param name="tooltipConfig">
tooltipDelay = 500 |
tooltipIcon = /myImages/myIcon.gif
</s:param>
</s:textfield>


<-- Example 4: -->
<s:textfield
label="Customer Address"
tooltip="Enter The Customer Address" >
<s:param
name="tooltipConfig"
value="#{'tooltipDelay':'500',
'tooltipIcon':'/myImages/myIcon.gif'}" />
</s:textfield>


<-- Example 5: -->
<s:textfield
label="Customer Telephone Number"
tooltip="Enter customer Telephone Number"
tooltipConfig="#{'tooltipDelay':'500',
'tooltipIcon':'/myImages/myIcon.gif'}" />


Fields Summary
private static final Log
LOG
protected HttpServletRequest
request
protected HttpServletResponse
response
protected String
templateSuffix
protected String
template
protected String
templateDir
protected String
theme
protected String
key
protected String
cssClass
protected String
cssStyle
protected String
disabled
protected String
label
protected String
labelPosition
protected String
requiredposition
protected String
name
protected String
required
protected String
tabindex
protected String
value
protected String
title
protected String
onclick
protected String
ondblclick
protected String
onmousedown
protected String
onmouseup
protected String
onmouseover
protected String
onmousemove
protected String
onmouseout
protected String
onfocus
protected String
onblur
protected String
onkeypress
protected String
onkeydown
protected String
onkeyup
protected String
onselect
protected String
onchange
protected String
accesskey
protected String
tooltip
protected String
tooltipConfig
protected String
defaultTemplateDir
protected String
defaultUITheme
protected org.apache.struts2.components.template.TemplateEngineManager
templateEngineManager
Constructors Summary
public UIBean(com.opensymphony.xwork2.util.ValueStack stack, HttpServletRequest request, HttpServletResponse response)


           
        super(stack);
        this.request = request;
        this.response = response;
        this.templateSuffix = ContextUtil.getTemplateSuffix(stack.getContext());
    
Methods Summary
public voidaddFormParameter(java.lang.String key, java.lang.Object value)

        Form form = (Form) findAncestor(Form.class);
        if (form != null) {
            form.addParameter(key, value);
        }
    
protected org.apache.struts2.components.template.TemplatebuildTemplateName(java.lang.String myTemplate, java.lang.String myDefaultTemplate)

        String template = myDefaultTemplate;

        if (myTemplate != null) {
            template = findString(myTemplate);
        }

        String templateDir = getTemplateDir();
        String theme = getTheme();

        return new Template(templateDir, theme, template);

    
protected voidenableAncestorFormCustomOnsubmit()

        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 booleanend(java.io.Writer writer, java.lang.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 java.lang.Stringescape(java.lang.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 voidevaluateExtraParams()

    
protected booleanevaluateNameValue()

        return true;
    
public voidevaluateParams()

        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();

    
protected abstract java.lang.StringgetDefaultTemplate()
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.

return
The name of the template to be used as the default.

public java.lang.StringgetTemplate()

        return template;
    
public java.lang.StringgetTemplateDir()

        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 java.lang.StringgetTheme()

        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 java.util.MapgetTooltipConfig(org.apache.struts2.components.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 java.lang.ClassgetValueClassType()

        return String.class;
    
protected voidmergeTemplate(java.io.Writer writer, org.apache.struts2.components.template.Template template)

        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 voidpopulateComponentHtmlId(Form form)
Create HTML id element for the component and populate this component parmaeter map. The order is as follows :-
  1. This component id attribute
  2. [containing_form_id]_[this_component_name]
  3. [this_component_name]

param
form

        if (id != null) {
            // this check is needed for backwards compatibility with 2.1.x
            if (altSyntax()) {
                addParameter("id", findString(id));
            } else {
                addParameter("id", id);
            }
        } else if (form != null) {
            addParameter("id", form.getParameters().get("id") + "_" 
                    + escape(name != null ? findString(name) : null));
        } else {
            addParameter("id", escape(name != null ? findString(name) : null));
        }
    
public voidsetAccesskey(java.lang.String accesskey)

        this.accesskey = accesskey;
    
public voidsetCssClass(java.lang.String cssClass)

        this.cssClass = cssClass;
    
public voidsetCssStyle(java.lang.String cssStyle)

        this.cssStyle = cssStyle;
    
public voidsetDefaultTemplateDir(java.lang.String dir)

        this.defaultTemplateDir = dir;
    
public voidsetDefaultUITheme(java.lang.String theme)

        this.defaultUITheme = theme;
    
public voidsetDisabled(java.lang.String disabled)

        this.disabled = disabled;
    
public voidsetKey(java.lang.String key)

        this.key = key;
    
public voidsetLabel(java.lang.String label)

        this.label = label;
    
public voidsetLabelposition(java.lang.String labelPosition)

        this.labelPosition = labelPosition;
    
public voidsetName(java.lang.String name)

        this.name = name;
    
public voidsetOnblur(java.lang.String onblur)

        this.onblur = onblur;
    
public voidsetOnchange(java.lang.String onchange)

        this.onchange = onchange;
    
public voidsetOnclick(java.lang.String onclick)

        this.onclick = onclick;
    
public voidsetOndblclick(java.lang.String ondblclick)

        this.ondblclick = ondblclick;
    
public voidsetOnfocus(java.lang.String onfocus)

        this.onfocus = onfocus;
    
public voidsetOnkeydown(java.lang.String onkeydown)

        this.onkeydown = onkeydown;
    
public voidsetOnkeypress(java.lang.String onkeypress)

        this.onkeypress = onkeypress;
    
public voidsetOnkeyup(java.lang.String onkeyup)

        this.onkeyup = onkeyup;
    
public voidsetOnmousedown(java.lang.String onmousedown)

        this.onmousedown = onmousedown;
    
public voidsetOnmousemove(java.lang.String onmousemove)

        this.onmousemove = onmousemove;
    
public voidsetOnmouseout(java.lang.String onmouseout)

        this.onmouseout = onmouseout;
    
public voidsetOnmouseover(java.lang.String onmouseover)

        this.onmouseover = onmouseover;
    
public voidsetOnmouseup(java.lang.String onmouseup)

        this.onmouseup = onmouseup;
    
public voidsetOnselect(java.lang.String onselect)

        this.onselect = onselect;
    
public voidsetRequired(java.lang.String required)

        this.required = required;
    
public voidsetRequiredposition(java.lang.String requiredposition)

        this.requiredposition = requiredposition;
    
public voidsetTabindex(java.lang.String tabindex)

        this.tabindex = tabindex;
    
public voidsetTemplate(java.lang.String template)

        this.template = template;
    
public voidsetTemplateDir(java.lang.String templateDir)

        this.templateDir = templateDir;
    
public voidsetTemplateEngineManager(org.apache.struts2.components.template.TemplateEngineManager mgr)

        this.templateEngineManager = mgr;
    
public voidsetTheme(java.lang.String theme)

        this.theme = theme;
    
public voidsetTitle(java.lang.String title)

        this.title = title;
    
public voidsetTooltip(java.lang.String tooltip)

        this.tooltip = tooltip;
    
public voidsetTooltipConfig(java.lang.String tooltipConfig)

        this.tooltipConfig = tooltipConfig;
    
public voidsetValue(java.lang.String value)

        this.value = value;