SpringMVC中Freemarker获取项目根目录

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

原文地址:https://www.cnblogs.com/newlangwen/p/9269755.html

时间: 2024-08-01 10:54:33

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

c# 获取项目根目录方法

编写程序的时候,经常需要用的项目根目录.自己总结如下 1.取得控制台应用程序的根目录方法     方法1.Environment.CurrentDirectory 取得或设置当前工作目录的完整限定路径     方法2.AppDomain.CurrentDomain.BaseDirectory 获取基目录,它由程序集冲突解决程序用来探测程序集 2.取得Web应用程序的根目录方法     方法1.HttpRuntime.AppDomainAppPath.ToString();//获取承载在当前应用程

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

在php程序开发中经常需要获取当前网站的目录,我们可以通过常量定义获取站点根目录物理路径,方便在程序中使用. 下面介绍几种常用的获取网站根目录的方法. php获取网站根目录方法一: <?php define("WWWROOT",str_ireplace(str_replace("/","\\",$_SERVER['PHP_SELF']),'',__FILE__)."\\"); echo WWWROOT ; ?> p

springmvc中freemarker的搭建

在springmvc框架已经搭好的前提下,导入freemarker.jar,此处版本是2.3. 主要的配置工作都在spring-servlet.xml中,请看代码: <!--配置试图解析器 --> <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="order" value="

jsp 中 关于获取项目路径问题

今天调试代码的时候发现个很有意思的问题,测试了好半天也没找到原因,所以只能先记录下来了. jsp 中代码如下: <%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE html> <html lang="en"> <head> <%@ include file="/common/config.jsp" %>

js获取项目根目录的方法

getRootPath = function(){ //获取当前网址,如: http://localhost:8080/ems/Pages/Basic/Person.jsp var curWwwPath = window.document.location.href; //获取主机地址之后的目录,如: /ems/Pages/Basic/Person.jsp var pathName = window.document.location.pathname; var pos = curWwwPath

JS获取项目根目录

function getRootPath(){ //获取当前网址,如: http://localhost:8088/test/test.jsp var curPath=window.document.location.href; //获取主机地址之后的目录,如: test/test.jsp var pathName=window.document.location.pathname; var pos=curPath.indexOf(pathName); //获取主机地址,如: http://lo

获取项目根目录(非tomact)

String path; public void main(String[] args) { File file=new File(""); path=file.getAbsolutePath(); syso(path); } 结果: D:\svnout\borncrm-biz

weiapi 获取项目根目录

无法使用: Server.Map("~"); Server.Map("~/"); Server.Map("./"); Server.Map("."); 正确方式: var serverFile = System.Web.Hosting.HostingEnvironment.MapPath("~/");

c# 如何获取项目的根目录

编写程序的时候,经常需要用的项目根目录.自己总结如下 1.取得控制台应用程序的根目录方法     方法1.Environment.CurrentDirectory 取得或设置当前工作目录的完整限定路径     方法2.AppDomain.CurrentDomain.BaseDirectory 获取基目录,它由程序集冲突解决程序用来探测程序集 2.取得Web应用程序的根目录方法     方法1.HttpRuntime.AppDomainAppPath.ToString();//获取承载在当前应用程