Package org.apache.struts2.components
Class Component
java.lang.Object
org.apache.struts2.components.Component
- Direct Known Subclasses:
ContextBean
,Else
,ElseIf
,I18n
,If
,Include
,OptGroup
,Param
,Property
,Push
,UIBean
Base class to extend for UI components.
This class is a good extension point when building reusable UI components.
This class is a good extension point when building reusable UI components.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected ActionMapper
static final String
protected boolean
protected boolean
protected boolean
protected ValueStack
protected static ConcurrentMap<Class<?>,
Collection<String>> Caches information about common tag's attributes to reduce scanning for annotation @StrutsTagAttributeprotected boolean
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
addAllAttributes
(Map<String, Object> additionalAttributes) Adds all the given attributes to this component's own attributes.void
addParameter
(String key, Object value) Adds the given key and value to this component's own parameter.protected String
completeExpression
(String expr) Adds the surrounding %{ } to the expression for proper processing.void
copyAttributes
(Map<String, Object> attributesToCopy) 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, jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res, Map<String, Object> parameters, String scheme, boolean includeContext, boolean encodeResult, boolean forceAddSchemeHostAndPort, boolean escapeAmp) Renders an action URL by consulting theActionMapper
.protected String
determineNamespace
(String namespace, ValueStack stack, jakarta.servlet.http.HttpServletRequest req) Determines the namespace of the current page being renderdd.boolean
Callback for the end tag of this component.protected boolean
Callback for the start tag of this component.boolean
Override to set if body content should be HTML-escaped.protected StrutsException
fieldError
(String field, String errorMsg, Exception e) Constructs aRuntimeException
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
Finds a value from the OGNL stack based on the given expression.protected Object
Evaluates the OGNL stack to find an Object of the given type.protected Object
Evaluates the OGNL stack to find an Object value.Gets the attributes.Gets the component stack of this component.protected String
getNamespace
(ValueStack stack) boolean
getStack()
Gets the OGNL value stack associated with this component.protected Collection<String>
If needed caches all methods annotated by given annotation to avoid further scansprotected boolean
isAcceptableExpression
(String expression) Checks if expression doesn't contain vulnerable codeboolean
isValidTagAttribute
(String attrName) Checks if provided name is a valid tag's attributeprotected void
Pops the component stack.void
setActionMapper
(ActionMapper mapper) void
setDevMode
(String devMode) void
setEscapeHtmlBody
(String escapeHtmlBody) void
setNotExcludedAcceptedPatterns
(NotExcludedAcceptedPatternsChecker notExcludedAcceptedPatterns) void
setPerformClearTagStateForTagPoolingServers
(boolean performClearTagStateForTagPoolingServers) Request that the tag state be cleared duringStrutsBodyTagSupport.doEndTag()
processing, which may help with certain edge cases with tag logic running on servers that implement JSP Tag Pooling.void
setThrowExceptionsOnELFailure
(String throwException) void
setUrlHelper
(UrlHelper urlHelper) boolean
Callback for the start tag of this component.protected String
stripExpression
(String expression) If %{...} is applied, simply strip the "%{" and "}" off.protected String
Constructs a string representation of the given exception.boolean
usesBody()
Overwrite to set if body should be used.
-
Field Details
-
COMPONENT_STACK
- See Also:
-
standardAttributesMap
Caches information about common tag's attributes to reduce scanning for annotation @StrutsTagAttribute -
devMode
protected boolean devMode -
escapeHtmlBody
protected boolean escapeHtmlBody -
stack
-
attributes
-
actionMapper
-
throwExceptionOnELFailure
protected boolean throwExceptionOnELFailure -
performClearTagStateForTagPoolingServers
protected boolean performClearTagStateForTagPoolingServers
-
-
Constructor Details
-
Component
Constructor.- Parameters:
stack
- OGNL value stack.
-
-
Method Details
-
setDevMode
-
setActionMapper
-
setThrowExceptionsOnELFailure
-
setEscapeHtmlBody
-
setUrlHelper
-
setNotExcludedAcceptedPatterns
public void setNotExcludedAcceptedPatterns(NotExcludedAcceptedPatternsChecker notExcludedAcceptedPatterns) -
getStack
Gets the OGNL value stack associated with this component.- Returns:
- the OGNL value stack associated with this component.
-
getComponentStack
Gets the component stack of this component.- Returns:
- the component stack of this component, never null.
-
start
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
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
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
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
Evaluates the OGNL stack to find a String value.- Parameters:
expr
- OGNL expression.- Returns:
- the String value found.
-
findString
Evaluates the OGNL stack to find a String value.
If the given expression is null a error is logged and aRuntimeException
is thrown constructed with a messaged based on the given field and errorMsg parameter.- Parameters:
expr
- OGNL expression.field
- field name used when throwingRuntimeException
.errorMsg
- error message used when throwingRuntimeException
.- Returns:
- the String value found.
- Throws:
StrutsException
- is thrown in case of expression is null.
-
fieldError
Constructs aRuntimeException
based on the given information.
A message is constructed and logged at ERROR level before being returned as aRuntimeException
.- Parameters:
field
- field name used when throwingRuntimeException
.errorMsg
- error message used when throwingRuntimeException
.e
- the caused exception, can be null.- Returns:
- the constructed
StrutsException
.
-
findValue
Finds a value from the OGNL stack based on the given expression. Will always evaluateexpr
against stack except whenexpr
is null. If %{...} is applied, simply strip it off.- Parameters:
expression
- the expression. Returns null if expr is null.- Returns:
- the value, null if not found.
-
stripExpression
If %{...} is applied, simply strip the "%{" and "}" off.- Parameters:
expression
- the expression (must be not null)- Returns:
- the stripped expression
-
completeExpression
Adds the surrounding %{ } to the expression for proper processing.- Parameters:
expr
- the expression.- Returns:
- the modified expression wrapped with %{...}
-
findValue
Evaluates the OGNL stack to find an Object value.
Function just like
findValue(String)
except that if the given expression is null a error is logged and aRuntimeException
is thrown constructed with a messaged based on the given field and errorMsg parameter.- Parameters:
expr
- OGNL expression.field
- field name used when throwingRuntimeException
.errorMsg
- error message used when throwingRuntimeException
.- Returns:
- the Object found, is never null.
- Throws:
StrutsException
- is thrown in case of not found in the OGNL stack, or expression is null.
-
findValue
Evaluates the OGNL stack to find an Object of the given type. Will evaluateexpression
the portion wrapped with %{...} against stack if evaluating to String.class, else the wholeexpression
is evaluated against the stack.- Parameters:
expression
- 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, jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res, Map<String, Object> parameters, String scheme, boolean includeContext, boolean encodeResult, boolean forceAddSchemeHostAndPort, boolean escapeAmp) Renders an action URL by consulting theActionMapper
.- Parameters:
action
- the actionnamespace
- the namespacemethod
- the methodreq
- HTTP requestres
- HTTP responseparameters
- parametersscheme
- http or httpsincludeContext
- should the context path be included or notencodeResult
- should the url be encodedforceAddSchemeHostAndPort
- should the scheme host and port be forcedescapeAmp
- should ampersand (&) be escaped to &- Returns:
- the action url.
-
determineNamespace
protected String determineNamespace(String namespace, ValueStack stack, jakarta.servlet.http.HttpServletRequest req) Determines the namespace of the current page being renderdd. Useful for Form, URL, and href generations.- Parameters:
namespace
- the namespacestack
- OGNL value stackreq
- HTTP request- Returns:
- the namepsace of the current page being rendered, is never null.
-
getNamespace
-
copyAttributes
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 component will be set in the parameters Map.- Parameters:
attributesToCopy
- the attributes to copy.
-
toString
Constructs a string representation of the given exception.- Parameters:
t
- the exception- Returns:
- the exception as a string.
-
getAttributes
Gets the attributes.- Returns:
- the attributes. It's never null.
-
addAllAttributes
Adds all the given attributes to this component's own attributes.- Parameters:
additionalAttributes
- the attributes to add.
-
addParameter
Adds the given key and value to this component's own parameter.
If the provided key is null nothing happens. 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 associated with the key.
-
usesBody
public boolean usesBody()Overwrite to set if body should be used.- Returns:
- always false for this component.
-
escapeHtmlBody
public boolean escapeHtmlBody()Override to set if body content should be HTML-escaped.- Returns:
- always true (default) for this component.
- Since:
- 6.0.0
-
isValidTagAttribute
Checks if provided name is a valid tag's attribute- Parameters:
attrName
- String name of attribute- Returns:
- true if attribute with the same name was already defined
-
getStandardAttributes
If needed caches all methods annotated by given annotation to avoid further scans- Returns:
- list of attributes
-
setPerformClearTagStateForTagPoolingServers
public void setPerformClearTagStateForTagPoolingServers(boolean performClearTagStateForTagPoolingServers) Request that the tag state be cleared duringStrutsBodyTagSupport.doEndTag()
processing, which may help with certain edge cases with tag logic running on servers that implement JSP Tag Pooling. Note: All Tag classes that extendStrutsBodyTagSupport
must implement a setter for this attribute (same name), and it must be defined at the Tag class level. Defining a setter in the superclass alone is insufficient (results in "Cannot find a setter method for the attribute").- Parameters:
performClearTagStateForTagPoolingServers
- true if tag state should be cleared, false otherwise.
-
getPerformClearTagStateForTagPoolingServers
public boolean getPerformClearTagStateForTagPoolingServers() -
isAcceptableExpression
Checks if expression doesn't contain vulnerable code- Parameters:
expression
- of the component- Returns:
- true|false
- Since:
- 6.0.0
-