Velocity中VelocityLayoutViewResolver类源码

来源:互联网 发布:vb什么时候吃效果好 编辑:程序博客网 时间:2024/05/18 22:43

Velocity中VelocityLayoutViewResolver类源码,代码中部分方法有删除

public class VelocityLayoutViewResolver extends VelocityViewResolver {    private String layoutUrl;    private String layoutKey;    private String screenContentKey;    protected Class requiredViewClass() {        return VelocityLayoutView.class;    }    protected AbstractUrlBasedView buildView(String viewName) throws Exception {        VelocityLayoutView view = (VelocityLayoutView)super.buildView(viewName);        if(this.layoutUrl != null) {            view.setLayoutUrl(this.layoutUrl);        }        if(this.layoutKey != null) {            view.setLayoutKey(this.layoutKey);        }        if(this.screenContentKey != null) {            view.setScreenContentKey(this.screenContentKey);        }        return view;    }}

public class VelocityViewResolver extends AbstractTemplateViewResolver {    private String dateToolAttribute;    private String numberToolAttribute;    private String toolboxConfigLocation;    public VelocityViewResolver() {        this.setViewClass(this.requiredViewClass());    }    protected Class requiredViewClass() {        return VelocityView.class;    }    protected void initApplicationContext() {        super.initApplicationContext();        if(this.toolboxConfigLocation != null) {            if(VelocityView.class.equals(this.getViewClass())) {                this.logger.info("Using VelocityToolboxView instead of default VelocityView due to specified toolboxConfigLocation");                this.setViewClass(VelocityToolboxView.class);            } else if(!VelocityToolboxView.class.isAssignableFrom(this.getViewClass())) {                throw new IllegalArgumentException("Given view class [" + this.getViewClass().getName() + "] is not of type [" + VelocityToolboxView.class.getName() + "], which it needs to be in case of a specified toolboxConfigLocation");            }        }    }    protected AbstractUrlBasedView buildView(String viewName) throws Exception {        VelocityView view = (VelocityView)super.buildView(viewName);        view.setDateToolAttribute(this.dateToolAttribute);        view.setNumberToolAttribute(this.numberToolAttribute);        if(this.toolboxConfigLocation != null) {            ((VelocityToolboxView)view).setToolboxConfigLocation(this.toolboxConfigLocation);        }        return view;    }}


public class AbstractTemplateViewResolver extends UrlBasedViewResolver {    private boolean exposeRequestAttributes = false;    private boolean allowRequestOverride = false;    private boolean exposeSessionAttributes = false;    private boolean allowSessionOverride = false;    private boolean exposeSpringMacroHelpers = true;    protected Class requiredViewClass() {        return AbstractTemplateView.class;    }    protected AbstractUrlBasedView buildView(String viewName) throws Exception {        AbstractTemplateView view = (AbstractTemplateView)super.buildView(viewName);        view.setExposeRequestAttributes(this.exposeRequestAttributes);        view.setAllowRequestOverride(this.allowRequestOverride);        view.setExposeSessionAttributes(this.exposeSessionAttributes);        view.setAllowSessionOverride(this.allowSessionOverride);        view.setExposeSpringMacroHelpers(this.exposeSpringMacroHelpers);        return view;    }}

public class UrlBasedViewResolver extends AbstractCachingViewResolver implements Ordered {    public static final String REDIRECT_URL_PREFIX = "redirect:";    public static final String FORWARD_URL_PREFIX = "forward:";    private Class viewClass;    private String prefix = "";    private String suffix = "";    private String[] viewNames = null;    private String contentType;    private boolean redirectContextRelative = true;    private boolean redirectHttp10Compatible = true;    private String requestContextAttribute;    private int order = 2147483647;    private final Map<String, Object> staticAttributes = new HashMap();    private Boolean exposePathVariables;    public void setViewClass(Class viewClass) {        if(viewClass != null && this.requiredViewClass().isAssignableFrom(viewClass)) {            this.viewClass = viewClass;        } else {            throw new IllegalArgumentException("Given view class [" + (viewClass != null?viewClass.getName():null) + "] is not of type [" + this.requiredViewClass().getName() + "]");        }    }    protected Class requiredViewClass() {        return AbstractUrlBasedView.class;    }    public void setPrefix(String prefix) {        this.prefix = prefix != null?prefix:"";    }    public void setAttributesMap(Map<String, ?> attributes) {        if(attributes != null) {            this.staticAttributes.putAll(attributes);        }    }    protected void initApplicationContext() {        super.initApplicationContext();        if(this.getViewClass() == null) {            throw new IllegalArgumentException("Property \'viewClass\' is required");        }    }    protected View createView(String viewName, Locale locale) throws Exception {        if(!this.canHandle(viewName, locale)) {            return null;        } else {            String forwardUrl;            if(viewName.startsWith("redirect:")) {                forwardUrl = viewName.substring("redirect:".length());                RedirectView view = new RedirectView(forwardUrl, this.isRedirectContextRelative(), this.isRedirectHttp10Compatible());                return this.applyLifecycleMethods(viewName, view);            } else if(viewName.startsWith("forward:")) {                forwardUrl = viewName.substring("forward:".length());                return new InternalResourceView(forwardUrl);            } else {                return super.createView(viewName, locale);            }        }    }    protected boolean canHandle(String viewName, Locale locale) {        String[] viewNames = this.getViewNames();        return viewNames == null || PatternMatchUtils.simpleMatch(viewNames, viewName);    }    protected View loadView(String viewName, Locale locale) throws Exception {        AbstractUrlBasedView view = this.buildView(viewName);        View result = this.applyLifecycleMethods(viewName, view);        return view.checkResource(locale)?result:null;    }    private View applyLifecycleMethods(String viewName, AbstractView view) {        return (View)this.getApplicationContext().getAutowireCapableBeanFactory().initializeBean(view, viewName);    }    protected AbstractUrlBasedView buildView(String viewName) throws Exception {        AbstractUrlBasedView view = (AbstractUrlBasedView)BeanUtils.instantiateClass(this.getViewClass());        view.setUrl(this.getPrefix() + viewName + this.getSuffix());        String contentType = this.getContentType();        if(contentType != null) {            view.setContentType(contentType);        }        view.setRequestContextAttribute(this.getRequestContextAttribute());        view.setAttributesMap(this.getAttributesMap());        if(this.exposePathVariables != null) {            view.setExposePathVariables(this.exposePathVariables.booleanValue());        }        return view;    }}

public abstract class AbstractCachingViewResolver extends WebApplicationObjectSupport implements ViewResolver {    public static final int DEFAULT_CACHE_LIMIT = 1024;    private volatile int cacheLimit = 1024;    private boolean cacheUnresolved = true;    private final Map<Object, View> viewCache = new LinkedHashMap(1024, 0.75F, true) {        protected boolean removeEldestEntry(Entry<Object, View> eldest) {            return this.size() > AbstractCachingViewResolver.this.getCacheLimit();        }    };    public View resolveViewName(String viewName, Locale locale) throws Exception {        if(!this.isCache()) {            return this.createView(viewName, locale);        } else {            Object cacheKey = this.getCacheKey(viewName, locale);            Map var4 = this.viewCache;            synchronized(this.viewCache) {                View view = (View)this.viewCache.get(cacheKey);                if(view == null && (!this.cacheUnresolved || !this.viewCache.containsKey(cacheKey))) {                    view = this.createView(viewName, locale);                    if(view != null || this.cacheUnresolved) {                        this.viewCache.put(cacheKey, view);                        if(this.logger.isTraceEnabled()) {                            this.logger.trace("Cached view [" + cacheKey + "]");                        }                    }                }                return view;            }        }    }    protected Object getCacheKey(String viewName, Locale locale) {        return viewName + "_" + locale;    }    public void removeFromCache(String viewName, Locale locale) {        if(!this.isCache()) {            this.logger.warn("View caching is SWITCHED OFF -- removal not necessary");        } else {            Object cacheKey = this.getCacheKey(viewName, locale);            Map var5 = this.viewCache;            Object cachedView;            synchronized(this.viewCache) {                cachedView = this.viewCache.remove(cacheKey);            }            if(cachedView == null) {                if(this.logger.isDebugEnabled()) {                    this.logger.debug("No cached instance for view \'" + cacheKey + "\' was found");                }            } else if(this.logger.isDebugEnabled()) {                this.logger.debug("Cache for view " + cacheKey + " has been cleared");            }        }    }    public void clearCache() {        this.logger.debug("Clearing entire view cache");        Map var1 = this.viewCache;        synchronized(this.viewCache) {            this.viewCache.clear();        }    }    protected View createView(String viewName, Locale locale) throws Exception {        return this.loadView(viewName, locale);    }    protected abstract View loadView(String var1, Locale var2) throws Exception;}

public abstract class WebApplicationObjectSupport extends ApplicationObjectSupport implements ServletContextAware {    private ServletContext servletContext;    public final void setServletContext(ServletContext servletContext) {        if(servletContext != this.servletContext) {            this.servletContext = servletContext;            if(servletContext != null) {                this.initServletContext(servletContext);            }        }    }    protected void initApplicationContext(ApplicationContext context) {        super.initApplicationContext(context);        if(this.servletContext == null && context instanceof WebApplicationContext) {            this.servletContext = ((WebApplicationContext)context).getServletContext();            if(this.servletContext != null) {                this.initServletContext(this.servletContext);            }        }    }    protected final WebApplicationContext getWebApplicationContext() throws IllegalStateException {        ApplicationContext ctx = this.getApplicationContext();        if(ctx instanceof WebApplicationContext) {            return (WebApplicationContext)this.getApplicationContext();        } else if(this.isContextRequired()) {            throw new IllegalStateException("WebApplicationObjectSupport instance [" + this + "] does not run in a WebApplicationContext but in: " + ctx);        } else {            return null;        }    }    protected final ServletContext getServletContext() throws IllegalStateException {        if(this.servletContext != null) {            return this.servletContext;        } else {            ServletContext servletContext = this.getWebApplicationContext().getServletContext();            if(servletContext == null && this.isContextRequired()) {                throw new IllegalStateException("WebApplicationObjectSupport instance [" + this + "] does not run within a ServletContext. Make sure the object is fully configured!");            } else {                return servletContext;            }        }    }    protected final File getTempDir() throws IllegalStateException {        return WebUtils.getTempDir(this.getServletContext());    }}

public abstract class ApplicationObjectSupport implements ApplicationContextAware {    protected final Log logger = LogFactory.getLog(this.getClass());    private ApplicationContext applicationContext;    private MessageSourceAccessor messageSourceAccessor;    public final void setApplicationContext(ApplicationContext context) throws BeansException {        if(context == null && !this.isContextRequired()) {            this.applicationContext = null;            this.messageSourceAccessor = null;        } else if(this.applicationContext == null) {            if(!this.requiredContextClass().isInstance(context)) {                throw new ApplicationContextException("Invalid application context: needs to be of type [" + this.requiredContextClass().getName() + "]");            }            this.applicationContext = context;            this.messageSourceAccessor = new MessageSourceAccessor(context);            this.initApplicationContext(context);        } else if(this.applicationContext != context) {            throw new ApplicationContextException("Cannot reinitialize with different application context: current one is [" + this.applicationContext + "], passed-in one is [" + context + "]");        }    }    protected boolean isContextRequired() {        return false;    }    protected Class requiredContextClass() {        return ApplicationContext.class;    }    protected void initApplicationContext(ApplicationContext context) throws BeansException {        this.initApplicationContext();    }      public final ApplicationContext getApplicationContext() throws IllegalStateException {        if(this.applicationContext == null && this.isContextRequired()) {            throw new IllegalStateException("ApplicationObjectSupport instance [" + this + "] does not run in an ApplicationContext");        } else {            return this.applicationContext;        }    }    protected final MessageSourceAccessor getMessageSourceAccessor() throws IllegalStateException {        if(this.messageSourceAccessor == null && this.isContextRequired()) {            throw new IllegalStateException("ApplicationObjectSupport instance [" + this + "] does not run in an ApplicationContext");        } else {            return this.messageSourceAccessor;        }    }}

public interface ApplicationContextAware extends Aware {    void setApplicationContext(ApplicationContext var1) throws BeansException;}



0 0
原创粉丝点击