📄 inserttag.java
字号:
// Check if a name is defined
if (nestedTag.getName() == null) {
throw new JspException("Error - PutList : attribute name is not defined. It is mandatory as the list is added as attribute of 'insert'.");
}
// now add attribute to enclosing parent (i.e. : this object).
putAttribute(nestedTag.getName(), nestedTag.getList());
}
/**
* Method calls by nested ≶putList> tags.
* A new list is added to current insert object.
*/
public void putAttribute(PutListTag nestedTag) throws JspException {
// Check role
HttpServletRequest request =
(HttpServletRequest) pageContext.getRequest();
String role = nestedTag.getRole();
if (role != null && !request.isUserInRole(role)) {
// not allowed : skip attribute
return;
}
putAttribute(nestedTag.getName(), nestedTag.getList());
}
/**
* Get current component context.
*/
private ComponentContext getCurrentContext() {
if (cachedCurrentContext == null) {
cachedCurrentContext =
(ComponentContext) pageContext.getAttribute(
ComponentConstants.COMPONENT_CONTEXT,
PageContext.REQUEST_SCOPE);
}
return cachedCurrentContext;
}
/**
* Get instantiated Controller.
* Return controller denoted by controllerType, or <code>null</code> if controllerType
* is null.
* @throws JspException If controller can't be created.
*/
private Controller getController() throws JspException {
if (controllerType == null) {
return null;
}
try {
return ComponentDefinition.createController(
controllerName,
controllerType);
} catch (InstantiationException ex) {
throw new JspException(ex);
}
}
/**
* Process the start tag by checking tag's attributes and creating appropriate handler.
* Possible handlers :
* <ul>
* <li> URL
* <li> definition
* <li> direct String
* </ul>
* Handlers also contain sub-component context.
*/
public int doStartTag() throws JspException {
// Additional fix for Bug 20034 (2005-04-28)
cachedCurrentContext = null;
// Check role immediatly to avoid useless stuff.
// In case of insertion of a "definition", definition's role still checked later.
// This lead to a double check of "role" ;-(
HttpServletRequest request =
(HttpServletRequest) pageContext.getRequest();
if (role != null && !request.isUserInRole(role)) {
processEndTag = false;
return SKIP_BODY;
}
try {
tagHandler = createTagHandler();
} catch (JspException e) {
if (isErrorIgnored) {
processEndTag = false;
return SKIP_BODY;
} else {
throw e;
}
}
return tagHandler.doStartTag();
}
/**
* Process the end tag by including the template.
* Simply call the handler doEndTag
*/
public int doEndTag() throws JspException {
if (!processEndTag) {
releaseInternal();
return EVAL_PAGE;
}
int res = tagHandler.doEndTag();
// Reset properties used by object, in order to be able to reuse object.
releaseInternal();
return res;
}
/**
* Process tag attribute and create corresponding tag handler.
*/
public TagHandler createTagHandler() throws JspException {
// Check each tag attribute.
// page Url attribute must be the last checked because it can appears concurrently
// with others attributes.
if (definitionName != null) {
return processDefinitionName(definitionName);
} else if (attribute != null) {
return processAttribute(attribute);
} else if (beanName != null) {
return processBean(beanName, beanProperty, beanScope);
} else if (name != null) {
return processName(name);
} else if (page != null) {
return processUrl(page);
} else {
throw new JspException("Error - Tag Insert : At least one of the following attribute must be defined : template|page|attribute|definition|name|beanName. Check tag syntax");
}
}
/**
* Process an object retrieved as a bean or attribute.
* Object can be a typed attribute, a String, or anything else.
* If typed attribute, use associated type.
* Otherwise, apply toString() on object, and use returned string as a name.
* @throws JspException - Throws by underlying nested call to
* processDefinitionName()
*/
public TagHandler processObjectValue(Object value) throws JspException {
// First, check if value is one of the Typed Attribute
if (value instanceof AttributeDefinition) {
// We have a type => return appropriate IncludeType
return processTypedAttribute((AttributeDefinition) value);
} else if (value instanceof ComponentDefinition) {
return processDefinition((ComponentDefinition) value);
}
// Value must denote a valid String
return processAsDefinitionOrURL(value.toString());
}
/**
* Process name.
* Search in following order :
* <ul>
* <li>Component context - if found, process it as value.</li>
* <li>definitions factory</li>
* <li>URL</li>
* <li></li>
* </ul>
*
* @return appropriate tag handler.
* @throws JspException - Throws by underlying nested call to
* processDefinitionName()
*/
public TagHandler processName(String name) throws JspException {
Object attrValue = getCurrentContext().getAttribute(name);
if (attrValue != null) {
return processObjectValue(attrValue);
}
return processAsDefinitionOrURL(name);
}
/**
* Process the url.
* @throws JspException If failed to create controller
*/
public TagHandler processUrl(String url) throws JspException {
return new InsertHandler(url, role, getController());
}
/**
* Process tag attribute "definition".
* First, search definition in the factory, then create handler from this definition.
* @param name Name of the definition.
* @return Appropriate TagHandler.
* @throws JspException- NoSuchDefinitionException No Definition found for name.
* @throws JspException- FactoryNotFoundException Can't find Definitions factory.
* @throws JspException- DefinedComponentFactoryException General error in factory.
* @throws JspException InstantiationException Can't create requested controller
*/
protected TagHandler processDefinitionName(String name)
throws JspException {
try {
ComponentDefinition definition =
TilesUtil.getDefinition(
name,
(HttpServletRequest) pageContext.getRequest(),
pageContext.getServletContext());
if (definition == null) { // is it possible ?
throw new NoSuchDefinitionException();
}
return processDefinition(definition);
} catch (NoSuchDefinitionException ex) {
throw new JspException(
"Error - Tag Insert : Can't get definition '"
+ definitionName
+ "'. Check if this name exist in definitions factory.", ex);
} catch (FactoryNotFoundException ex) {
throw new JspException(ex.getMessage());
} catch (DefinitionsFactoryException ex) {
if (log.isDebugEnabled()) {
ex.printStackTrace();
}
// Save exception to be able to show it later
pageContext.setAttribute(
Globals.EXCEPTION_KEY,
ex,
PageContext.REQUEST_SCOPE);
throw new JspException(ex);
}
}
/**
* End of Process tag attribute "definition".
* Overload definition with tag attributes "template" and "role".
* Then, create appropriate tag handler.
* @param definition Definition to process.
* @return Appropriate TagHandler.
* @throws JspException InstantiationException Can't create requested controller
*/
protected TagHandler processDefinition(ComponentDefinition definition)
throws JspException {
// Declare local variable in order to not change Tag attribute values.
String role = this.role;
String page = this.page;
Controller controller = null;
try {
controller = definition.getOrCreateController();
// Overload definition with tag's template and role.
if (role == null) {
role = definition.getRole();
}
if (page == null) {
page = definition.getTemplate();
}
if (controllerName != null) {
controller =
ComponentDefinition.createController(
controllerName,
controllerType);
}
// Can check if page is set
return new InsertHandler(
definition.getAttributes(),
page,
role,
controller);
} catch (InstantiationException ex) {
throw new JspException(ex);
}
}
/**
* Process a bean.
* Get bean value, eventually using property and scope. Found value is process by processObjectValue().
* @param beanName Name of the bean
* @param beanProperty Property in the bean, or null.
* @param beanScope bean scope, or null.
* @return Appropriate TagHandler.
* @throws JspException - NoSuchDefinitionException No value associated to bean.
* @throws JspException an error occur while reading bean, or no definition found.
* @throws JspException - Throws by underlying nested call to processDefinitionName()
*/
protected TagHandler processBean(
String beanName,
String beanProperty,
String beanScope)
throws JspException {
Object beanValue =
TagUtils.getRealValueFromBean(
beanName,
beanProperty,
beanScope,
pageContext);
if (beanValue == null) {
throw new JspException(
"Error - Tag Insert : No value defined for bean '"
+ beanName
+ "' with property '"
+ beanProperty
+ "' in scope '"
+ beanScope
+ "'.");
}
return processObjectValue(beanValue);
}
/**
* Process tag attribute "attribute".
* Get value from component attribute.
* Found value is process by processObjectValue().
* @param name Name of the attribute.
* @return Appropriate TagHandler.
* @throws JspException - NoSuchDefinitionException No Definition found for name.
* @throws JspException - Throws by underlying nested call to processDefinitionName()
*/
public TagHandler processAttribute(String name) throws JspException {
Object attrValue = getCurrentContext().getAttribute(name);
if (attrValue == null) {
throw new JspException(
"Error - Tag Insert : No value found for attribute '"
+ name
+ "'.");
}
return processObjectValue(attrValue);
}
/**
* Try to process name as a definition, or as an URL if not found.
* @param name Name to process.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -