61

我有以下代码在我的控制器中设置一个变量:

model.set("type", type);

在 thymeleaf 视图中,我想构造一个带有操作 url 的表单:

/mycontroller/{type}

任何想法如何实现这一目标?我没有运气阅读百里香文档。

4

5 回答 5

121

正如user482745 在评论中建议的那样(现已删除),我之前建议的字符串连接

<form th:action="@{/mycontroller/} + ${type}">

在某些网络环境中会失败。

Thymeleaf 使用LinkExpression它来解析@{..}表达式。在内部,这使用HttpServletResponse#encodeURL(String). 它的 javadoc 状态

对于健壮的会话跟踪,servlet 发出的所有 URL 都应通过此方法运行。否则,URL 重写不能用于不支持 cookie 的浏览器。

在通过 URL 完成会话跟踪的 Web 应用程序中,该部分将附加到在附加@{..}之前发出的字符串${..}。你不想要这个。

相反,请按照文档中的建议使用路径变量

您还可以像普通参数一样以路径变量的形式包含参数,但在 URL 的路径中指定一个占位符:

<a th:href="@{/order/{id}/details(id=3,action='show_all')}">

所以你的例子看起来像

<form th:action="@{/mycontroller/{path}(path=${type})}"> //adding ending curly brace
于 2013-02-18T14:17:31.370 回答
42

如果您不想使用字符串连接(由 Sotirios 提出),您可以在URL 链接中使用表达式预处理

<form th:action="@{/mycontroller/__${type}__}">
于 2014-03-17T12:41:53.010 回答
17

您需要在 @{} 内连接字符串。

<form th:action="@{'/mycontroller/' + ${type}}">

@{} 用于 URL 重写。URL 重写的一部分是跟踪会话。第一次用户请求 URL,应用服务器添加到 url;jsessionid=somehexvalue并生成带有 jsessionid 的 cookie。当客户端在下一次请求期间发送 cookie 时,服务器知道客户端支持 cookie。如果服务器知道客户端支持 cookie,服务器将不会在 URL 中保留 addind jsessionid。

我首选的方法是使用管道语法 (|) 进行文字替换。

<form th:action="@{|/mycontroller/${type}|}">

Thymeleaf 路径变量语法是

<form th:action="@{/mycontroller/{pathParam}(pathParam=${type}}">

参考: Thymeleaf 标准 URL 语法

于 2015-09-25T11:23:57.080 回答
6

你需要的是:

<a th:href="@{/mycontroller/{type}(type=${type})}">

文档:

这里有很大的帮助: http ://www.thymeleaf.org/doc/articles/standardurlsyntax.html 。我从那里使用的是:

您还可以像普通参数一样以路径变量的形式包含参数,但在 URL 的路径中指定一个占位符:

<a th:href="@{/order/{id}/details(id=3,action='show_all')}">

... 更重要的是:一个 URL 表达式,如:

<a th:href="@{/order/details(id=${order.id})}">

于 2017-01-03T08:48:38.867 回答
0
Exception evaluating SpringEL expression: "businessId" (login:50)

我有同样的问题,并通过字符串连接解决,如下所示。

登录控制器.java

@RequestMapping(value = "/login/{businessId}", method = RequestMethod.GET)
    public ModelAndView get(HttpServletRequest request, @PathVariable String businessId) {
        ModelAndView modelAndView = new ModelAndView("login");
        modelAndView.addObject("businessId", businessId);
        return modelAndView;
    }

登录.html

            <form role="form" th:action="@{/login} + '/'+ ${businessId}" th:method="post">
                            <fieldset>
                                <div class="form-group">
                                    <input class="form-control" placeholder="E-mail" name="userName"
                                        type="email"></input>
                                </div>
                                <div class="form-group">
                                    <input class="form-control" placeholder="Password"
                                        name="password" type="password" value=""></input>
                                </div>
                                <div class="checkbox">
                                    <label> <input name="remember" type="checkbox"
                                        value="Remember Me"></input>Remember Me
                                    </label>
                                </div>
                                <!-- Change this to a button or input when using this as a form -->
                                <button id="login" class="btn btn-lg btn-success btn-block" type="submit">Login</button>
                            </fieldset>
            </form>
于 2016-12-16T13:30:10.793 回答