https://blog.csdn.net/whatlookingfor/article/details/51538995

在SpringMVC框架中使用Freemarker试图时,要获取根路径的方式如下:

<!-- FreeMarker视图解析 如返回userinfo。。在这里配置后缀名ftl和视图解析器。。 -->
<bean id="viewResolverFtl"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
<property name="suffix" value=".ftl" />
<property name="contentType" value="text/html;charset=UTF-8" />
<property name="exposeRequestAttributes" value="true" />
<property name="exposeSessionAttributes" value="true" />
<property name="exposeSpringMacroHelpers" value="true" />
<property name="requestContextAttribute" value="request" />
<property name="cache" value="true" />
<property name="order" value="0" />
</bean>

其中property name=”requestContextAttribute” value=”request”是关键。 
意思是把Spring的RequestContext对象暴露为变量request 
利用${request.contextPath}来获取应用程序的contextPath

如果是集成了Springboot,在配置文件中,只需要设置 
spring.freemarker.request-context-attribute=request 即可

ftl中的页面设置如下:

根路径配置:
spring.freemarker.request-context-attribute=request
#freemarker 配置
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.expose-spring-macro-helpers=true
spring.freemarker.request-context-attribute=request
spring.freemarker.charset=utf-8
spring.freemarker.cache=false
spring.freemarker.content-type=text/html
spring.freemarker.suffix= .ftl
spring.freemarker.template-loader-path=classpath:/templates/ #应用名称
server.servlet.context-path=/

其中property name=”requestContextAttribute” value=”request”是关键。 
意思是把Spring的RequestContext对象暴露为变量request 
利用${request.contextPath}来获取应用程序的contextPath

SpringMVC中Freemarker获取项目根目录的更多相关文章

  1. Java中动态获取项目根目录的绝对路径

    https://www.cnblogs.com/zhouqing/archive/2012/11/10/2757774.html 序言 在开发过程中经常会用到读写文件,其中就必然涉及路径问题.使用固定 ...

  2. Weblogic部署web项目获取项目根目录为null

    写在前面 图片上传功能, web项目部署在本地Tomcat上并没有问题, 但是打成war包部署到Linux服务器Weblogic下却出现如题问题, 导致图片上传失败. 问题代码 String real ...

  3. java 获取项目根目录

    代码入下: request.getSession().getServletContext().getRealPath(); 这里的getRealPath()括号里面可以输入你想得到的具体目录. 需要注 ...

  4. Java获取项目根目录等其他系统属性

    一 相对路径的获得 说明:相对路径(即不写明时候到底相对谁)均可通过以下方式获得(不论是一般的java项目还是web项目) String relativelyPath=System.getProper ...

  5. c# 获取项目根目录方法

    编写程序的时候,经常需要用的项目根目录.自己总结如下 1.取得控制台应用程序的根目录方法     方法1.Environment.CurrentDirectory 取得或设置当前工作目录的完整限定路径 ...

  6. .net获取项目根目录方法集合

    这篇文章是别的博客复下来,收藏的: 编写程序的时候,经常需要用的项目根目录.自己总结如下 1.取得控制台应用程序的根目录方法     方法1.Environment.CurrentDirectory ...

  7. PHP中如何获取网站根目录物理路径

    在php程序开发中经常需要获取当前网站的目录,我们可以通过常量定义获取站点根目录物理路径,方便在程序中使用. 下面介绍几种常用的获取网站根目录的方法. php获取网站根目录方法一: <?php ...

  8. Winform中怎样获取项目图片资源并转换为Image对象

    场景 DevExpress的TreeList怎样给树节点设置图标: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10274554 ...

  9. springmvc中freemarker的搭建

    在springmvc框架已经搭好的前提下,导入freemarker.jar,此处版本是2.3. 主要的配置工作都在spring-servlet.xml中,请看代码: <!--配置试图解析器 -- ...

随机推荐

  1. DOM编程 --《高性能JavaScript》

    1.重绘和重排 浏览器下载完页面的所有组件 —— HTML标记,CSS,JavaScript,图片,会解析并生成两个内部数据结构. DOM树 表示页面结构 渲染树(CSS) 表示DOM节点如何显示 当 ...

  2. openwrt 模拟i2c驱动(一)

    一:加载i2c driver kmod-i2c-core................................................ I2C support kmod-i2c-al ...

  3. WTF

    WTF ,luna黑色主题比较sublime 还是差点!

  4. Xamarin.Android 记事本(一)

    导读 1.视图及数据库的创建 2.listview数据绑定 3.listview的点击事件 正文 如何创建一个listview,大家可以看这里,基本流程操作是一模一样的,我就不多说了,然后就是新建一个 ...

  5. passive aggressive(pa)和average perceptron(ap)

    passive aggressive(pa)和average perceptron(ap)

  6. 集群环境搭建-SSH免密码登陆(二)

    1.打开sshd配置 命令: vi /etc/ssh/sshd_config 找到以下内容,并去掉注释符”#“ RSAAuthentication yes PubkeyAuthentication y ...

  7. Linux系统(Centos)下安装Java环境配置步骤详述

    1.首先要去下载好JDK,Java SE 8的官方网址是http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2 ...

  8. bzoj3727: PA2014 Final Zadanie

    我真是SB之神呢这么SB的题都不会 肯定是先无脑正向思考,罗列下关系式: b[1]=∑a[i]*dep[i]=∑tot[i] (i!=1) b[i]=b[fa]-tot[i]+(tot[1]-tot[ ...

  9. SDUT 3035 你猜我猜不猜你猜不猜(字符串 规律性)

    你猜我猜不猜你猜不猜 Time Limit: 2000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 In the past 39th annual ACM in ...

  10. dedecms时间格式调用标签汇总

    1.时间格式{dede:field name='pubdate' function='strftime("%Y年%m月%d日 %H:%M:%S","@me")' ...