Thymeleaf 中 #request.getParameters() 的解决方法
Workaround for #request.getParameters() in Thymeleaf
我们正在使用 CAS 5.2.3,它使用升级版的 Thymeleaf。 Thymeleaf 限制了对某些请求功能的访问——“#request.getParameters()”就是其中之一。有什么解决方法吗?我在尝试访问它时遇到以下错误 - "Access to request parameters is forbidden in this context. Note some restrictions apply to variable access. For example, direct access to request parameters is forbidden in preprocessing and unescaped expressions, in TEXT template mode, in fragment insertion specifications and in some specific attribute processors."
好问题。几个月前遇到过这个问题,可以解决。
看了他们的源代码后,我发现他们只限制在特定标签上使用#request.getParameters()
,他们并没有禁止在某些情况下使用#request.getParameters()
。
在我的用例中,我能够使用 CData 绕过此检查。不确定它是否适用于您的用例,因为您没有提供任何代码示例....
无论如何,下面的例子想要根据参数 url
将用户重定向到另一个页面
这是一个在 CAS 5 中损坏的示例代码。2.x,但在 CAS 5 中有效。1.x:
<html>
<head>
<title> Deforestation </title>
</head>
<body th:attr="onload='window.location.href=\''+${#request.getParameter('url')}+'\''">
</body>
</html>
这里有一个变通代码:
<html>
<head>
<title> Deforestation </title>
</head>
<body>
Logging out. Please wait...
<script th:inline="javascript">
/*<![CDATA[*/
location.href = /*[[( ${#request.getParameter('url')} )]]*/ ;
/*]]>*/
</script>
</body>
</html>
如果这没有解决您的问题,请提供您的源代码,以便我们更好地查看问题。
注意:现在禁止使用此内容是出于安全原因,使用此解决方法可能会损害安全标准,请记住在必要时清理用户输入
编辑:
根据评论,虽然不优雅,但也许像下面这样的东西会起作用?
<html>
<head>
<title> Data attribute </title>
</head>
<body>
<span id="foobarid"> </span>
<script th:inline="javascript">
/*<![CDATA[*/
$('#foobarid').data('foo-bar',/*[[( ${#request.getParameter('foo') == 'bar'} )]]*/);
/*]]>*/
</script>
</body>
</html>
显然,在这个简单的要求上可能会花费数小时,甚至数天的时间,Thymeleaf 在后来的版本中将其作为一个缺乏记录的烦恼而引入。我花了几个小时从 5 年前的旧 Thymeleaf 版本升级,并陷入了同样的问题。缺乏文件也没有。当我终于停下来思考一下时,我意识到解决这个问题就像使用带有 JSP.
的 核心标签一样简单
只需在允许访问请求参数的更高级别html标签中使用th:with
标签,如下所示:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns="http://www.w3.org/1999/xhtml"
th:with="action=${param.action != null ?
param.action[0] : 'none'}">
<head>
...
在此示例中,请求参数 action
存储在名为 action
的页面上下文局部变量中(该名称可以是您选择的任何唯一名称)。这个变量现在可以在你定义它的高级 html 标签下的任何地方访问。它甚至可以在声明后的同一 html 标记中的后续 th:xx
标记中使用。所以基本上,现在您可以这样做:
<th:block th:switch="${action}">
<title th:case="'edit'" th:text="#{page.contacts.edit}"></title>
<title th:case="'delete'" th:text="#{page.contacts.delete}"></title>
<title th:case="*" th:text="#{page.contacts.add}"></title>
</th:block>
甚至通过简单地将您的变量引用为 ${action}
来调用参数化片段。在这个例子中,我有一个导航栏模板片段,它接受三个参数。最后一个是请求参数,否则由于较新的 Thymeleaf 请求对象访问限制而无法访问。
<div id="wrapper">
<div class="nav" th:replace="html/fragments/navbar ::
navbar('html/contact','contact-html', ${action})">
</div>
<div class="content">
...
如果您需要更多请求参数和更多本地页面上下文变量,只需使用逗号分隔 th:with
标记中的声明,如下所示:
th:with="action=${param.action == null ? 'none': param.action[0]},
self=${param.self == null ? 'none': param.self[0]}"
我希望这能为你们节省一些宝贵的时间,并消除重构旧 Thymeleaf html 页面的挫败感。它绝对胜过页面中那些极其难以管理的 CDATA
scriptlet。
我们正在使用 CAS 5.2.3,它使用升级版的 Thymeleaf。 Thymeleaf 限制了对某些请求功能的访问——“#request.getParameters()”就是其中之一。有什么解决方法吗?我在尝试访问它时遇到以下错误 - "Access to request parameters is forbidden in this context. Note some restrictions apply to variable access. For example, direct access to request parameters is forbidden in preprocessing and unescaped expressions, in TEXT template mode, in fragment insertion specifications and in some specific attribute processors."
好问题。几个月前遇到过这个问题,可以解决。
看了他们的源代码后,我发现他们只限制在特定标签上使用#request.getParameters()
,他们并没有禁止在某些情况下使用#request.getParameters()
。
在我的用例中,我能够使用 CData 绕过此检查。不确定它是否适用于您的用例,因为您没有提供任何代码示例....
无论如何,下面的例子想要根据参数 url
这是一个在 CAS 5 中损坏的示例代码。2.x,但在 CAS 5 中有效。1.x:
<html>
<head>
<title> Deforestation </title>
</head>
<body th:attr="onload='window.location.href=\''+${#request.getParameter('url')}+'\''">
</body>
</html>
这里有一个变通代码:
<html>
<head>
<title> Deforestation </title>
</head>
<body>
Logging out. Please wait...
<script th:inline="javascript">
/*<![CDATA[*/
location.href = /*[[( ${#request.getParameter('url')} )]]*/ ;
/*]]>*/
</script>
</body>
</html>
如果这没有解决您的问题,请提供您的源代码,以便我们更好地查看问题。
注意:现在禁止使用此内容是出于安全原因,使用此解决方法可能会损害安全标准,请记住在必要时清理用户输入
编辑:
根据评论,虽然不优雅,但也许像下面这样的东西会起作用?
<html>
<head>
<title> Data attribute </title>
</head>
<body>
<span id="foobarid"> </span>
<script th:inline="javascript">
/*<![CDATA[*/
$('#foobarid').data('foo-bar',/*[[( ${#request.getParameter('foo') == 'bar'} )]]*/);
/*]]>*/
</script>
</body>
</html>
显然,在这个简单的要求上可能会花费数小时,甚至数天的时间,Thymeleaf 在后来的版本中将其作为一个缺乏记录的烦恼而引入。我花了几个小时从 5 年前的旧 Thymeleaf 版本升级,并陷入了同样的问题。缺乏文件也没有。当我终于停下来思考一下时,我意识到解决这个问题就像使用带有 JSP.
的只需在允许访问请求参数的更高级别html标签中使用th:with
标签,如下所示:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns="http://www.w3.org/1999/xhtml"
th:with="action=${param.action != null ?
param.action[0] : 'none'}">
<head>
...
在此示例中,请求参数 action
存储在名为 action
的页面上下文局部变量中(该名称可以是您选择的任何唯一名称)。这个变量现在可以在你定义它的高级 html 标签下的任何地方访问。它甚至可以在声明后的同一 html 标记中的后续 th:xx
标记中使用。所以基本上,现在您可以这样做:
<th:block th:switch="${action}">
<title th:case="'edit'" th:text="#{page.contacts.edit}"></title>
<title th:case="'delete'" th:text="#{page.contacts.delete}"></title>
<title th:case="*" th:text="#{page.contacts.add}"></title>
</th:block>
甚至通过简单地将您的变量引用为 ${action}
来调用参数化片段。在这个例子中,我有一个导航栏模板片段,它接受三个参数。最后一个是请求参数,否则由于较新的 Thymeleaf 请求对象访问限制而无法访问。
<div id="wrapper">
<div class="nav" th:replace="html/fragments/navbar ::
navbar('html/contact','contact-html', ${action})">
</div>
<div class="content">
...
如果您需要更多请求参数和更多本地页面上下文变量,只需使用逗号分隔 th:with
标记中的声明,如下所示:
th:with="action=${param.action == null ? 'none': param.action[0]},
self=${param.self == null ? 'none': param.self[0]}"
我希望这能为你们节省一些宝贵的时间,并消除重构旧 Thymeleaf html 页面的挫败感。它绝对胜过页面中那些极其难以管理的 CDATA
scriptlet。