Spring 3.1.2 の使用
コントローラに関連する URL を作成できるように、jsp からコントローラ (メソッドではない) RequestMapping の注釈付きの値をどのように参照しますか。メソッド レベルのリクエスト マッピングが参照されると、リンクが機能しなくなるため、リンクをこれに含めることはできません。
例 (このコントローラーのマッピングは「/foo/test」であることに注意してください):
@Controller
@RequestMapping("/foo/test")
public class FooController {
@RequestMapping(value="/this", method=RequestMethod.GET)
public String getThis() {
return "test/this";
}
@RequestMapping(value="/that", method=RequestMethod.GET)
public String getThat() {
return "test/that";
}
@RequestMapping(value="/other", method=RequestMethod.GET)
public String getOther() {
return "test/other";
}
}
...そして私のthis.jspから:
<%@ taglib uri="http://www.springframework.org/tags" prefix="s"%>
<s:url value="${controllerRequestMapping}" var="baseUrl"/>
<a href="${baseUrl}/that">That</a>
<a href="${baseUrl}/other">Other</a>
RequestMapping にアクセスする必要があるのは、ビューが複数のコントローラーからアクセスされる可能性があるためです。このコントローラのマッピングは「/bar/test」であることに注意してください!
@Controller
@RequestMapping("/bar/test")
public class BarController {
@RequestMapping(value="/this", method=RequestMethod.GET)
public String getThis() {
return "test/this";
}
@RequestMapping(value="/that", method=RequestMethod.GET)
public String getThat() {
return "test/that";
}
@RequestMapping(value="/other", method=RequestMethod.GET)
public String getOther() {
return "test/other";
}
}
コントローラー レベルのリクエスト マッピングの一部にはパス変数も含まれているため、リクエスト マッピングの文字列値だけを取得することはできません。次のように解決する必要があります。
@Controller
@RequestMapping("/something/{anything}/test/")
public class SomethingController {
...
}
アップデート
おそらく、Spring URL タグの前に Controller リクエスト マッピングをコンテキスト パスに追加してコンテキスト パスを変更する方法があれば、問題は解決するでしょう。
contextPath = contextPath/controllerRequestMapping
次に、Spring が現在のコンテキスト パスを自動的に取得すると信じているため、次のようなことができます。
<%@ taglib uri="http://www.springframework.org/tags" prefix="s"%>
<a href="<s:url value="/that"/>">That</a>
<a href="<s:url value="/other"/>">Other</a>
もちろん、これが最適です。アイデア?想い…?
前もって感謝します!