📄 inserttag.java
字号:
* @return appropriate TagHandler
* @throws JspException InstantiationException Can't create requested controller
*/
public TagHandler processAsDefinitionOrURL(String name)
throws JspException {
try {
ComponentDefinition definition =
TilesUtil.getDefinition(
name,
pageContext.getRequest(),
pageContext.getServletContext());
if (definition != null) {
return processDefinition(definition);
}
} catch (DefinitionsFactoryException ex) {
// silently failed, because we can choose to not define a factory.
}
// no definition found, try as url
return processUrl(name);
}
/**
* Process typed attribute according to its type.
* @param value Typed attribute to process.
* @return appropriate TagHandler.
* @throws JspException - Throws by underlying nested call to processDefinitionName()
*/
public TagHandler processTypedAttribute(AttributeDefinition value)
throws JspException {
if (value instanceof DirectStringAttribute) {
return new DirectStringHandler((String) value.getValue());
} else if (value instanceof DefinitionAttribute) {
return processDefinition((ComponentDefinition) value.getValue());
} else if (value instanceof DefinitionNameAttribute) {
return processDefinitionName((String) value.getValue());
}
return new InsertHandler(
(String) value.getValue(),
role,
getController());
}
/**
* Do an include of specified page.
* This method is used internally to do all includes from this class. It delegates
* the include call to the TilesUtil.doInclude().
* @param page The page that will be included
* @param flush If the writer should be flushed before the include
* @throws ServletException - Thrown by call to pageContext.include()
* @throws IOException - Thrown by call to pageContext.include()
*/
protected void doInclude(String page, boolean flush)
throws ServletException, IOException {
TilesUtil.doInclude(page, pageContext, flush);
}
/////////////////////////////////////////////////////////////////////////////
/**
* Inner Interface.
* Sub handler for tag.
*/
protected interface TagHandler {
/**
* Create ComponentContext for type depicted by implementation class.
*/
public int doStartTag() throws JspException;
/**
* Do include for type depicted by implementation class.
*/
public int doEndTag() throws JspException;
/**
* Add a component parameter (attribute) to subContext.
*/
public void putAttribute(String name, Object value);
} // end inner interface
/////////////////////////////////////////////////////////////////////////////
/**
* Real handler, after attribute resolution.
* Handle include sub-component.
*/
protected class InsertHandler implements TagHandler {
protected String page;
protected ComponentContext currentContext;
protected ComponentContext subCompContext;
protected String role;
protected Controller controller;
/**
* Constructor.
* Create insert handler using Component definition.
*/
public InsertHandler(
Map attributes,
String page,
String role,
Controller controller) {
this.page = page;
this.role = role;
this.controller = controller;
subCompContext = new ComponentContext(attributes);
}
/**
* Constructor.
* Create insert handler to insert page at specified location.
*/
public InsertHandler(String page, String role, Controller controller) {
this.page = page;
this.role = role;
this.controller = controller;
subCompContext = new ComponentContext();
}
/**
* Create a new empty context.
*/
public int doStartTag() throws JspException {
// Check role
HttpServletRequest request =
(HttpServletRequest) pageContext.getRequest();
if (role != null && !request.isUserInRole(role)) {
return SKIP_BODY;
}
// save current context
this.currentContext = getCurrentContext();
return EVAL_BODY_INCLUDE;
}
/**
* Add attribute to sub context.
* Do nothing.
*/
public void putAttribute(String name, Object value) {
subCompContext.putAttribute(name, value);
}
/**
* Include requested page.
*/
public int doEndTag() throws JspException {
// Check role
HttpServletRequest request =
(HttpServletRequest) pageContext.getRequest();
if (role != null && !request.isUserInRole(role)) {
return EVAL_PAGE;
}
try {
if (log.isDebugEnabled()) {
log.debug("insert page='" + page + "'.");
}
// set new context for included component.
pageContext.setAttribute(
ComponentConstants.COMPONENT_CONTEXT,
subCompContext,
PageContext.REQUEST_SCOPE);
// Call controller if any
if (controller != null) {
try {
controller.execute(
subCompContext,
(HttpServletRequest) pageContext.getRequest(),
(HttpServletResponse) pageContext.getResponse(),
pageContext.getServletContext());
} catch (Exception e) {
throw new ServletException(e);
}
}
// include requested component.
if (flush) {
pageContext.getOut().flush();
}
doInclude(page, flush);
} catch (IOException e) {
String msg =
"Can't insert page '" + page + "' : " + e.getMessage();
log.error(msg, e);
throw new JspException(msg);
} catch (IllegalArgumentException e) {
// Can't resolve page uri, should we ignore it?
if (!(page == null && isErrorIgnored)) {
String msg =
"Can't insert page '"
+ page
+ "'. Check if it exists.\n"
+ e.getMessage();
log.error(msg, e);
throw new JspException(msg,e);
}
} catch (ServletException e) {
Throwable cause = e;
if (e.getRootCause() != null) {
cause = e.getRootCause();
}
String msg =
"ServletException in '" + page + "': " + cause.getMessage();
log.error(msg, e);
throw new JspException(msg,e);
} finally {
// restore old context only if currentContext not null
// (bug with Silverstream ?; related by Arvindra Sehmi 20010712)
if (currentContext != null) {
pageContext.setAttribute(
ComponentConstants.COMPONENT_CONTEXT,
currentContext,
PageContext.REQUEST_SCOPE);
}
}
return EVAL_PAGE;
}
/**
* Process an exception.
* Depending of debug attribute, print full exception trace or only
* its message in output page.
* @param ex Exception
* @param msg An additional message to show in console and to propagate if we can't output exception.
* @deprecated This method will be removed in a release after Struts 1.2.
*/
protected void processException(Throwable ex, String msg)
throws JspException {
try {
if (msg == null) {
msg = ex.getMessage();
}
if (log.isDebugEnabled()) { // show full trace
log.debug(msg, ex);
pageContext.getOut().println(msg);
ex.printStackTrace(
new PrintWriter(pageContext.getOut(), true));
} else { // show only message
pageContext.getOut().println(msg);
}
} catch (IOException ioex) { // problems. Propagate original exception
pageContext.setAttribute(
ComponentConstants.EXCEPTION_KEY,
ex,
PageContext.REQUEST_SCOPE);
throw new JspException(msg,ioex);
}
}
}
/**
* Parse the list of roles and return <code>true</code> or <code>false</code> based on whether
* the user has that role or not.
* @param role Comma-delimited list of roles.
* @param request The request.
*/
static public boolean userHasRole(
HttpServletRequest request,
String role) {
StringTokenizer st = new StringTokenizer(role, ",");
while (st.hasMoreTokens()) {
if (request.isUserInRole(st.nextToken())) {
return true;
}
}
return false;
}
/////////////////////////////////////////////////////////////////////////////
/**
* Handle insert direct string.
*/
protected class DirectStringHandler implements TagHandler {
/** Object to print as a direct string */
private Object value;
/**
* Constructor.
*/
public DirectStringHandler(Object value) {
this.value = value;
}
/**
* Do nothing, there is no context for a direct string.
*/
public int doStartTag() throws JspException {
return SKIP_BODY;
}
/**
* Add attribute to sub context.
* Do nothing.
*/
public void putAttribute(String name, Object value) {
}
/**
* Print String in page output stream.
*/
public int doEndTag() throws JspException {
try {
if (flush) {
pageContext.getOut().flush();
}
pageContext.getOut().print(value);
} catch (IOException ex) {
if (log.isDebugEnabled()) {
log.debug("Can't write string '" + value + "' : ", ex);
}
pageContext.setAttribute(
ComponentConstants.EXCEPTION_KEY,
ex,
PageContext.REQUEST_SCOPE);
throw new JspException(
"Can't write string '" + value + "' : " + ex.getMessage(), ex);
}
return EVAL_PAGE;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -