Thymeleaf construct URL with variable

后端 未结 5 902
夕颜
夕颜 2020-12-01 01:24

I have the following code setting a variable in my controller:

model.set(\"type\", type);

In the thymeleaf view I want to construct a form

5条回答
  •  再見小時候
    2020-12-01 01:56

    As user482745 suggests in the comments (now deleted), the string concatenation I previously suggested

    will fail in some web contexts.

    Thymeleaf uses LinkExpression that resolves the @{..} expression. Internally, this uses HttpServletResponse#encodeURL(String). Its javadoc states

    For robust session tracking, all URLs emitted by a servlet should be run through this method. Otherwise, URL rewriting cannot be used with browsers which do not support cookies.

    In web applications where the session tracking is done through the URL, that part will be appended to the string emitted for @{..} before the ${..} is appended. You don't want this.

    Instead, use path variables as suggested in the documentation

    You can also include parameters in the form of path variables similarly to normal parameters but specifying a placeholder inside your URL’s path:

    
    

    So your example would look like

     //adding ending curly brace
    

提交回复
热议问题