com.opensymphony.webwork.components
Class Component

java.lang.Object
  extended by com.opensymphony.webwork.components.Component
Direct Known Subclasses:
ActionComponent, AppendIterator, ApplyDecoratorBean, Bean, Date, Else, ElseIf, I18n, If, Include, IteratorComponent, MergeIterator, OptGroup, Param, Property, Push, Set, Text, UIBean, URL

public class Component
extends Object

Base class to extend for UI components.

This class is a good extension point when building reuseable UI components.

Version:
$Date: 2007-11-12 23:50:43 +0800 (Mon, 12 Nov 2007) $ $Id: Component.java 2962 2007-11-12 15:50:43Z tm_jee $
Author:
plightbo, tm_jee

Field Summary
static String COMPONENT_STACK
           
protected  String id
           
protected  Map parameters
           
protected  OgnlValueStack stack
           
 
Constructor Summary
Component(OgnlValueStack stack)
          Constructor.
 
Method Summary
 void addAllParameters(Map params)
          Add's all the given parameters to this componenets own parameters.
 void addParameter(String key, Object value)
          Add's the given key and value to this components own parameter.
 boolean altSyntax()
          Is the altSyntax enabled?

See webwork.properties where the altSyntax flag is defined.

 void copyParams(Map params)
          Pushes this component's parameter Map as well as the component itself on to the stack and then copies the supplied parameters over.
protected  String determineActionURL(String action, String namespace, String method, HttpServletRequest req, HttpServletResponse res, Map parameters, String scheme, boolean includeContext, boolean encodeResult, boolean escapeAmp)
          Renders an action URL by consulting the ActionMapper.
protected  String determineNamespace(String namespace, OgnlValueStack stack, HttpServletRequest req)
          Determines the namespace of the current page being renderdd.
 boolean end(Writer writer, String body)
          Callback for the end tag of this component.
protected  boolean end(Writer writer, String body, boolean popComponentStack)
          Callback for the start tag of this component.
protected  WebWorkException fieldError(String field, String errorMsg, Exception e)
          Constructs?a WebWorkException based on the given information.
protected  Component findAncestor(Class clazz)
          Finds the nearest ancestor of this component stack.
protected  String findString(String expr)
          Evaluates the OGNL stack to find a String value.
protected  String findString(String expr, String field, String errorMsg)
          Evaluates the OGNL stack to find a String value.
protected  Object findValue(String expr)
          Finds a value from the OGNL stack based on the given expression.
protected  Object findValue(String expr, Class toType)
          Evaluates the OGNL stack to find an Object of the given type.
protected  Object findValue(String expr, String field, String errorMsg)
          Evaluates the OGNL stack to find an Object value.
 Stack getComponentStack()
          Get's the component stack of this component.
 String getId()
          Get's the id for referencing element.
 Map getParameters()
          Get's the parameters.
 OgnlValueStack getStack()
          Get's the OGNL value stack assoicated with this component.
protected  void popComponentStack()
          Pops the component stack.
 void setId(String id)
          id for referencing element.
 boolean start(Writer writer)
          Callback for the start tag of this component.
protected  String toString(Throwable t)
          Constructs a string representation of the given exception.
 boolean usesBody()
          Overwrite to set if body shold be used.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COMPONENT_STACK

public static final String COMPONENT_STACK
See Also:
Constant Field Values

stack

protected OgnlValueStack stack

parameters

protected Map parameters

id

protected String id
Constructor Detail

Component

public Component(OgnlValueStack stack)
Constructor.

Parameters:
stack - OGNL value stack.
Method Detail

getStack

public OgnlValueStack getStack()
Get's the OGNL value stack assoicated with this component.

Returns:
the OGNL value stack assoicated with this component.

getComponentStack

public Stack getComponentStack()
Get's the component stack of this component.

Returns:
the component stack of this component, never null.

start

public boolean start(Writer writer)
Callback for the start tag of this component. Should the body be evaluated?

Parameters:
writer - the output writer.
Returns:
true if the body should be evaluated

end

public boolean end(Writer writer,
                   String body)
Callback for the end tag of this component. Should the body be evaluated again?

NOTE: will pop component stack.

Parameters:
writer - the output writer.
body - the rendered body.
Returns:
true if the body should be evaluated again

end

protected boolean end(Writer writer,
                      String body,
                      boolean popComponentStack)
Callback for the start tag of this component. Should the body be evaluated again?

NOTE: has a parameter to determine to pop the component stack.

Parameters:
writer - the output writer.
body - the rendered body.
popComponentStack - should the component stack be popped?
Returns:
true if the body should be evaluated again

popComponentStack

protected void popComponentStack()
Pops the component stack.


findAncestor

protected Component findAncestor(Class clazz)
Finds the nearest ancestor of this component stack.

Parameters:
clazz - the class to look for, or if assignable from.
Returns:
the component if found, null if not.

findString

protected String findString(String expr)
Evaluates the OGNL stack to find a String value.

Parameters:
expr - OGNL expression.
Returns:
the String value found.

findString

protected String findString(String expr,
                            String field,
                            String errorMsg)
Evaluates the OGNL stack to find a String value.

If the given expression is null a error is logged and a WebWorkException is thrown constructed with a messaged based on the given field and errorMsg paramter.

Parameters:
expr - OGNL expression.
field - field name used when throwing WebWorkException.
errorMsg - error message used when throwing WebWorkException.
Returns:
the String value found.
Throws:
WebWorkException - is thrown in case of expression is null.

fieldError

protected WebWorkException fieldError(String field,
                                      String errorMsg,
                                      Exception e)
Constructs?a WebWorkException based on the given information.

A message is constructed and logged at ERROR level before being returned as a WebWorkException.

Parameters:
field - field name used when throwing WebWorkException.
errorMsg - error message used when throwing WebWorkException.
e - the caused exception, can be null.
Returns:
the constructed WebWorkException.

findValue

protected Object findValue(String expr)
Finds a value from the OGNL stack based on the given expression.

Parameters:
expr - the expression. Returns null if expr is null.
Returns:
the value, null if not found.

altSyntax

public boolean altSyntax()
Is the altSyntax enabled?

See webwork.properties where the altSyntax flag is defined. Note: Since WebWork 2.17 the altSyntax is default true.

Returns:
true if the altSyntax is enabled.

findValue

protected Object findValue(String expr,
                           String field,
                           String errorMsg)
Evaluates the OGNL stack to find an Object value.

If the given expression is null a error is logged and a WebWorkException is thrown constructed with a messaged based on the given field and errorMsg paramter.

Parameters:
expr - OGNL expression.
field - field name used when throwing WebWorkException.
errorMsg - error message used when throwing WebWorkException.
Returns:
the Object found, is never null.
Throws:
WebWorkException - is thrown in case of not found in the OGNL stack, or expression is null.

findValue

protected Object findValue(String expr,
                           Class toType)
Evaluates the OGNL stack to find an Object of the given type.

This method only supports the altSyntax. So this should be set to true.

Parameters:
expr - OGNL expression.
toType - the type expected to find.
Returns:
the Object found, or null if not found.

determineActionURL

protected String determineActionURL(String action,
                                    String namespace,
                                    String method,
                                    HttpServletRequest req,
                                    HttpServletResponse res,
                                    Map parameters,
                                    String scheme,
                                    boolean includeContext,
                                    boolean encodeResult,
                                    boolean escapeAmp)
Renders an action URL by consulting the ActionMapper.

Parameters:
action - the action
namespace - the namespace
method - the method
req - HTTP request
res - HTTP response
parameters - parameters
scheme - http or https
includeContext - should the context path be included or not
encodeResult - should the url be encoded
Returns:
the action url.

determineNamespace

protected String determineNamespace(String namespace,
                                    OgnlValueStack stack,
                                    HttpServletRequest req)
Determines the namespace of the current page being renderdd. Useful for Form, URL, and href generations.

Parameters:
namespace - the namespace
stack - OGNL value stack
req - HTTP request
Returns:
the namepsace of the current page being rendered, is never null.

copyParams

public void copyParams(Map params)
Pushes this component's parameter Map as well as the component itself on to the stack and then copies the supplied parameters over. Because the component's parameter Map is pushed before the component itself, any key-value pair that can't be assigned to componet will be set in the parameters Map.

Parameters:
params - the parameters to copy.

toString

protected String toString(Throwable t)
Constructs a string representation of the given exception.

Parameters:
t - the exception
Returns:
the exception as a string.

getParameters

public Map getParameters()
Get's the parameters.

Returns:
the parameters. Is never null.

addAllParameters

public void addAllParameters(Map params)
Add's all the given parameters to this componenets own parameters.

Parameters:
params - the parameters to add.

addParameter

public void addParameter(String key,
                         Object value)
Add's the given key and value to this components own parameter.

If the provided key is null nothing happends. If the provided value is null any existing parameter with the given key name is removed.

Parameters:
key - the key of the new parameter to add.
value - the value assoicated with the key.

getId

public String getId()
Get's the id for referencing element.

Returns:
the id for referencing element.

setId

public void setId(String id)
id for referencing element. For UI and form tags it will be used as HTML id attribute


usesBody

public boolean usesBody()
Overwrite to set if body shold be used.

Returns:
always false for this component.

WebWork Project Page