コントローラーで変数を設定する次のコードがあります。
model.set("type", type);
thymeleaf ビューで、アクション URL を含むフォームを作成したい:
/mycontroller/{type}
これを達成する方法はありますか?私は幸運にもタイムリーフのドキュメントを読みました。
user482745がコメントで示唆しているように(現在は削除されています)、以前に提案した文字列連結
<form th:action="@{/mycontroller/} + ${type}">
一部の Web コンテキストでは失敗します。
Thymeleaf は式LinkExpression
を解決するために使用し@{..}
ます。内部的に、これは を使用しHttpServletResponse#encodeURL(String)
ます。そのjavadocの状態
堅牢なセッション トラッキングを行うには、サーブレットによって発行されるすべての URL をこのメソッドで実行する必要があります。そうしないと、Cookie をサポートしていないブラウザーでは URL 書き換えを使用できません。
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
文字列連結 ( Sotirios が提案)を使用したくない場合は、URL リンクで式の前処理を使用できます。
<form th:action="@{/mycontroller/__${type}__}">
必要なものは次のとおりです。
<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})}">
Exception evaluating SpringEL expression: "businessId" (login:50)
私は同じ問題を抱えており、以下のような文字列連結で解決しています。
LoginController.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;
}
login.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>