Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 如何从thymeleaf处理器内的应用程序获取url?_Spring_Thymeleaf - Fatal编程技术网

Spring 如何从thymeleaf处理器内的应用程序获取url?

Spring 如何从thymeleaf处理器内的应用程序获取url?,spring,thymeleaf,Spring,Thymeleaf,在我当前的spring项目中,我创建了一个定制的thymeleaf处理器。此处理器的实现需要检索应用程序的url,即为jsp页面中的${pageContext.request.contextPath}返回的值。有人知道如何得到它吗?内部视图: 在Thymeleaf中,JSP的${pageContext.request.contextPath}等价物将是@{},即,如果您在JSP中有一个类似${pageContext.request.contextPath}/login的url,那么在Thymel

在我当前的spring项目中,我创建了一个定制的thymeleaf处理器。此处理器的实现需要检索应用程序的url,即为jsp页面中的
${pageContext.request.contextPath}
返回的值。有人知道如何得到它吗?

内部视图:

在Thymeleaf中,JSP的
${pageContext.request.contextPath}
等价物将是
@{}
,即,如果您在JSP中有一个类似
${pageContext.request.contextPath}/login
的url,那么在Thymeleaf中它将是
@{/login}
。trymeleaf使用/[-]]/语法计算javascript中的变量,如
var url=/*[[@{/login}]]*/

参考:

内部处理器类:

首先需要将上下文强制转换为IWebContext,然后通过调用getRequest获取HttpServletRequest对象,最后通过调用request.getContextPath()获取上下文路径。您可能希望使用instanceof操作符检查上下文是否是IWebContext的一种类型,我在这里跳过了这一点

    final HttpServletRequest request = ((IWebContext) context).getRequest();
    System.out.println("contextpath: " + request.getContextPath());

好的,但这是我在视图中已经使用的。我想在java类中为我的自定义处理器检索此信息。很抱歉我的误解。答案已更新,请立即检查。:-)