<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%> <spring:url value="user" var="userUrl" /> <a href="${fn:escapeXml(userUrl)}">ユーザ</a>
こんな感じで書くと、自動でコンテキストパスとか付けてくれるから便利。
で、これをFreeMarkerで使う方法があったので一応メモてきな。
WebApplicationContextに下記を追記
WebApplicationContextとはhoge-context.xmlみたいなやつ。<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="cache" value="true"/> <property name="prefix" value=""/> <property name="suffix" value=".ftl"/> <!-- この行を追加してtrueにしておくとSpringFreemarkerMacroが有効になります。 --> <property name="exposeSpringMacroHelpers" value="true"/> </bean>
FreeMarkerを使う方法は他のサイトを見て下さいね!!
あとは使うだけ
こんな感じで使えます。<#import "/spring.ftl" as spring /><a href="<@spring.url '/userUrl'/>">ユーザ</a>
ベンリー
ただ、Apacheでバーチャルホストとか使ってるとコンテキストパスが付いちゃうから、コンテキストパスはROOTで設定しておかないとダメでした。
参考サイト
http://static.springsource.org/spring/docs/2.0.x/reference/view.htmlSpringによるWebアプリケーションスーパーサンプル 第2版 | |
![]() | 株式会社クロノス 阪田 浩一 ソフトバンククリエイティブ 2010-11-12 売り上げランキング : 47256 Amazonで詳しく見る |