public class JFinalViewResolver
extends org.springframework.web.servlet.view.AbstractTemplateViewResolver
关键配置:
1:setDevMode(true) 设置支持热加载模板文件
2:addSharedFunction(file) 添加共享函数文件
3:setSourceFactory(new ClassPathSourceFactory()),从 class path 与 jar 包中加载模板文件
一般用于 sprint boot
4:setSessionInView(true) 设置在模板中可通过 #(session.value) 访问 session 中的数据
5:setCreateSession(boolean) 用来设置 request.getSession(boolean) 调时的参数
6:setBaseTemplatePath(path) 设置模板文件所在的基础路径,通常用于 spring mvc
默认值为 web 根路径,一般不需要设置
| 限定符和类型 | 字段和说明 |
|---|---|
static Engine |
engine |
| 构造器和说明 |
|---|
JFinalViewResolver() |
| 限定符和类型 | 方法和说明 |
|---|---|
void |
addDirective(String directiveName,
Class<? extends Directive> directiveClass)
添加自定义指令
|
void |
addDirective(String directiveName,
Directive directive)
已过时。
|
static void |
addExtensionMethod(Class<?> targetClass,
Class<?> extensionClass)
添加扩展方法
|
static void |
addExtensionMethod(Class<?> targetClass,
Object objectOfExtensionClass)
添加扩展方法
|
void |
addSharedFunction(String fileName)
添加 shared function 文件,可调用多次添加多个文件
|
void |
addSharedMethod(Class<?> sharedMethodFromClass)
添加共享方法
|
void |
addSharedMethod(Object sharedMethodFromObject)
添加共享方法
|
void |
addSharedObject(String name,
Object object)
添加共享对象
|
Engine |
getEngine() |
protected void |
initServletContext(javax.servlet.ServletContext servletContext)
spring 回调,利用 ServletContext 做必要的初始化工作
|
protected org.springframework.web.servlet.View |
loadView(String viewName,
Locale locale)
支持 jfinal enjoy、jsp、freemarker、velocity 四类模板共存于一个项目中
注意:这里采用识别 ".jsp"、".ftl"、".vm" 模板后缀名的方式来实现功能
所以 jfinal enjoy 模板不要采用上述三种后缀名,否则功能将失效
还要注意与 jsp、freemarker、velocity 以外类型模板共存使用时
需要改造该方法
|
static JFinalViewResolver |
me()
me 会保存在第一次被创建对象
|
protected Class<?> |
requiredViewClass() |
void |
setBaseTemplatePath(String baseTemplatePath)
设置模板基础路径
|
void |
setCreateSession(boolean createSession)
在使用 request.getSession(createSession) 时传入
用来指示 session 不存在时是否立即创建
|
void |
setDatePattern(String datePattern)
设置 #date(...)
|
void |
setDevMode(boolean devMode)
设置开发模式,值为 true 时支持模板文件热加载
|
void |
setEncoding(String encoding)
设置 encoding
|
void |
setSessionInView(boolean sessionInView)
设置为 true 时支持在模板中使用 #(session.value) 形式访问 session 中的数据
|
void |
setSharedFunction(String sharedFunctionFiles)
设置 shared function 文件,多个文件用逗号分隔
主要用于 Spring MVC 的 xml 配置方式
Spring Boot 的代码配置方式可使用 addSharedFunction(...)
|
void |
setSharedFunctionList(List<String> sharedFunctionList)
通过 List 配置多个 shared function file
配置示例:
|
void |
setSourceFactory(ISourceFactory sourceFactory)
设置 ISourceFactory 用于为 engine 切换不同的 ISource 实现类
配置为 ClassPathSourceFactory 时特别注意:
由于在 initServletContext() 通过如下方法中已设置了 baseTemplatePath 值:
setBaseTemplatePath(servletContext.getRealPath("/"))
而 ClassPathSourceFactory 在 initServletContext() 方法中设置的
值之下不能工作,所以在本方法中通过如下方法清掉了该值:
setBaseTemplatePath(null)
这种处理方式适用于绝大部分场景,如果在使用 ClassPathSourceFactory 的同时
仍然需要设置 baseTemplatePath,则在调用该方法 “之后” 通过如下代码再次配置:
setBaseTemplatePath(value)
|
void |
setToClassPathSourceFactory()
设置为 ClassPathSourceFactory 的快捷方法
ClassPathSourceFactory 将从 CLASSPATH 与 jar 包中读取模板
|
buildView, setAllowRequestOverride, setAllowSessionOverride, setExposeRequestAttributes, setExposeSessionAttributes, setExposeSpringMacroHelpersapplyLifecycleMethods, canHandle, createView, getAttributesMap, getCacheKey, getContentType, getExposeContextBeansAsAttributes, getExposedContextBeanNames, getExposePathVariables, getOrder, getPrefix, getRedirectHosts, getRequestContextAttribute, getSuffix, getViewClass, getViewNames, initApplicationContext, instantiateView, isRedirectContextRelative, isRedirectHttp10Compatible, setAttributes, setAttributesMap, setContentType, setExposeContextBeansAsAttributes, setExposedContextBeanNames, setExposePathVariables, setOrder, setPrefix, setRedirectContextRelative, setRedirectHosts, setRedirectHttp10Compatible, setRequestContextAttribute, setSuffix, setViewClass, setViewNamesclearCache, getCacheFilter, getCacheLimit, isCache, isCacheUnresolved, removeFromCache, resolveViewName, setCache, setCacheFilter, setCacheLimit, setCacheUnresolvedgetServletContext, getTempDir, getWebApplicationContext, initApplicationContext, isContextRequired, setServletContextpublic static final Engine engine
public static JFinalViewResolver me()
public Engine getEngine()
public void setDevMode(boolean devMode)
public void setSharedFunction(String sharedFunctionFiles)
public void setSharedFunctionList(List<String> sharedFunctionList)
配置示例:
_layout.html
_paginate.html
public void addSharedFunction(String fileName)
public void addDirective(String directiveName, Class<? extends Directive> directiveClass)
@Deprecated public void addDirective(String directiveName, Directive directive)
public void addSharedMethod(Object sharedMethodFromObject)
public void addSharedMethod(Class<?> sharedMethodFromClass)
public static void addExtensionMethod(Class<?> targetClass, Object objectOfExtensionClass)
public static void addExtensionMethod(Class<?> targetClass, Class<?> extensionClass)
public void setSourceFactory(ISourceFactory sourceFactory)
配置为 ClassPathSourceFactory 时特别注意:
由于在 initServletContext() 通过如下方法中已设置了 baseTemplatePath 值:
setBaseTemplatePath(servletContext.getRealPath("/"))
而 ClassPathSourceFactory 在 initServletContext() 方法中设置的
值之下不能工作,所以在本方法中通过如下方法清掉了该值:
setBaseTemplatePath(null)
这种处理方式适用于绝大部分场景,如果在使用 ClassPathSourceFactory 的同时
仍然需要设置 baseTemplatePath,则在调用该方法 “之后” 通过如下代码再次配置:
setBaseTemplatePath(value)
public void setToClassPathSourceFactory()
public void setBaseTemplatePath(String baseTemplatePath)
public void setSessionInView(boolean sessionInView)
public void setCreateSession(boolean createSession)
public void setEncoding(String encoding)
public void setDatePattern(String datePattern)
protected Class<?> requiredViewClass()
requiredViewClass 在类中 org.springframework.web.servlet.view.AbstractTemplateViewResolverprotected org.springframework.web.servlet.View loadView(String viewName, Locale locale) throws Exception
loadView 在类中 org.springframework.web.servlet.view.UrlBasedViewResolverExceptionprotected void initServletContext(javax.servlet.ServletContext servletContext)
initServletContext 在类中 org.springframework.web.context.support.WebApplicationObjectSupportCopyright © 2024. All rights reserved.