📄 componentdefinition.java
字号:
/**
* Put an attribute in template definition.
* Attribute can be used as content for tag get.
* @param name Attribute name
* @param content Attribute value �
* @param direct Determines how content is handled by get tag: true means content is printed directly; false, the default, means content is included
*/
public void put(String name, Object content, boolean direct) {
put(name, content, direct, null);
}
/**
* Put an attribute in template definition.
* Attribute can be used as content for tag get.
* @param name Attribute name
* @param content Attribute value
* @param direct Determines how content is handled by get tag: true means content is printed directly; false, the default, means content is included
* @param role Determine if content is used by get tag. If user is in role, content is used.
*/
public void put(String name, Object content, boolean direct, String role) {
if (direct == true) { // direct String
put(name, content, "string", role);
} else {
put(name, content, "template", role);
}
}
/**
* Put an attribute in template definition.
* Attribute can be used as content for tag get.
* @param name Attribute name
* @param content Attribute value
* @param type attribute type: template, string, definition
* @param role Determine if content is used by get tag. If user is in role, content is used.
*/
public void put(String name, Object content, String type, String role) {
// Is there a type set ?
// First check direct attribute, and translate it to a valueType.
// Then, evaluate valueType, and create requested typed attribute.
AttributeDefinition attribute = null;
if (content != null
&& type != null
&& !(content instanceof AttributeDefinition)) {
String strValue = content.toString();
if (type.equalsIgnoreCase("string")) {
attribute = new DirectStringAttribute(strValue);
} else if (type.equalsIgnoreCase("page")) {
attribute = new PathAttribute(strValue);
} else if (type.equalsIgnoreCase("template")) {
attribute = new PathAttribute(strValue);
} else if (type.equalsIgnoreCase("instance")) {
attribute = new DefinitionNameAttribute(strValue);
} else if (type.equalsIgnoreCase("definition")) {
attribute = new DefinitionNameAttribute(strValue);
}
}
putAttribute(name, attribute);
}
/**
* Returns a description of the attributes.
*/
public String toString() {
return "{name="
+ name
+ ", path="
+ path
+ ", role="
+ role
+ ", controller="
+ controller
+ ", controllerType="
+ controllerType
+ ", controllerInstance="
+ controllerInstance
+ ", attributes="
+ attributes
+ "}\n";
}
/**
* Get associated controller type.
* Type denote a fully qualified classname.
*/
public String getControllerType() {
return controllerType;
}
/**
* Set associated controller type.
* Type denote a fully qualified classname.
* @param controllerType Typeof associated controller
*/
public void setControllerType(String controllerType) {
this.controllerType = controllerType;
}
/**
* Set associated controller name as an url, and controller
* type as "url".
* Name must be an url (not checked).
* Convenience method.
* @param controller Controller url
*/
public void setControllerUrl(String controller) {
setController(controller);
setControllerType("url");
}
/**
* Set associated controller name as a classtype, and controller
* type as "classname".
* Name denote a fully qualified classname
* Convenience method.
* @param controller Controller classname.
*/
public void setControllerClass(String controller) {
setController(controller);
setControllerType("classname");
}
/**
* Get associated controller local URL.
* URL should be local to webcontainer in order to allow request context followup.
* URL comes as a string.
*/
public String getController() {
return controller;
}
/**
* Set associated controller URL.
* URL should be local to webcontainer in order to allow request context followup.
* URL is specified as a string.
* @param url Url called locally
*/
public void setController(String url) {
this.controller = url;
}
/**
* Get controller instance.
* @return controller instance.
*/
public Controller getControllerInstance() {
return controllerInstance;
}
/**
* Get or create controller.
* Get controller, create it if necessary.
* @return controller if controller or controllerType is set, null otherwise.
* @throws InstantiationException if an error occur while instanciating Controller :
* (classname can't be instanciated, Illegal access with instanciated class,
* Error while instanciating class, classname can't be instanciated.
*/
public Controller getOrCreateController() throws InstantiationException {
if (controllerInstance != null) {
return controllerInstance;
}
// Do we define a controller ?
if (controller == null && controllerType == null) {
return null;
}
// check parameters
if (controllerType != null && controller == null) {
throw new InstantiationException("Controller name should be defined if controllerType is set");
}
controllerInstance = createController(controller, controllerType);
return controllerInstance;
}
/**
* Set controller.
*/
public void setControllerInstance(Controller controller) {
this.controllerInstance = controller;
}
/**
* Create a new instance of controller named in parameter.
* If controllerType is specified, create controller accordingly.
* Otherwise, if name denote a classname, create an instance of it. If class is
* subclass of org.apache.struts.action.Action, wrap controller
* appropriately.
* Otherwise, consider name as an url.
* @param name Controller name (classname, url, ...)
* @param controllerType Expected Controller type
* @return org.apache.struts.tiles.Controller
* @throws InstantiationException if an error occur while instanciating Controller :
* (classname can't be instanciated, Illegal access with instanciated class,
* Error while instanciating class, classname can't be instanciated.
*/
public static Controller createController(String name, String controllerType)
throws InstantiationException {
if (log.isDebugEnabled()) {
log.debug("Create controller name=" + name + ", type=" + controllerType);
}
Controller controller = null;
if (controllerType == null) { // first try as a classname
try {
return createControllerFromClassname(name);
} catch (InstantiationException ex) { // ok, try something else
controller = new UrlController(name);
}
} else if ("url".equalsIgnoreCase(controllerType)) {
controller = new UrlController(name);
} else if ("classname".equalsIgnoreCase(controllerType)) {
controller = createControllerFromClassname(name);
}
return controller;
}
/**
* Create a controller from specified classname
* @param classname Controller classname.
* @return org.apache.struts.tiles.Controller
* @throws InstantiationException if an error occur while instanciating Controller :
* (classname can't be instanciated, Illegal access with instanciated class,
* Error while instanciating class, classname can't be instanciated.
*/
public static Controller createControllerFromClassname(String classname)
throws InstantiationException {
try {
Class requestedClass = RequestUtils.applicationClass(classname);
Object instance = requestedClass.newInstance();
if (log.isDebugEnabled()) {
log.debug("Controller created : " + instance);
}
return (Controller) instance;
} catch (java.lang.ClassNotFoundException ex) {
throw new InstantiationException(
"Error - Class not found :" + ex.getMessage());
} catch (java.lang.IllegalAccessException ex) {
throw new InstantiationException(
"Error - Illegal class access :" + ex.getMessage());
} catch (java.lang.InstantiationException ex) {
throw ex;
} catch (java.lang.ClassCastException ex) {
throw new InstantiationException(
"Controller of class '"
+ classname
+ "' should implements 'Controller' or extends 'Action'");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -