Freemarker 与 spring mvc,那么我的操作会是什么样子?
在我的 appname-servlet.xml
中,我有:
<!-- freemarker config -->
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/freemarker/"/>
</bean>
<!--
View resolvers can also be configured with ResourceBundles or XML files. If you need
different view resolving based on Locale, you have to use the resource bundle resolver.
-->
<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"/>
<!-- if you want to use the Spring FreeMarker macros, set this property to true -->
<property name="exposeSpringMacroHelpers" value="true"/>
</bean>
所以我的 HomeController.java
的索引视图位于:/web-inf/freemarker/index.ftl
我希望有人可以创建一个非常简单的 Index 操作,它将创建一个 ModelAndView
并使用 freemarker
。
我不确定事情将如何连接在一起,谢谢!
In my appname-servlet.xml
I have:
<!-- freemarker config -->
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/freemarker/"/>
</bean>
<!--
View resolvers can also be configured with ResourceBundles or XML files. If you need
different view resolving based on Locale, you have to use the resource bundle resolver.
-->
<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"/>
<!-- if you want to use the Spring FreeMarker macros, set this property to true -->
<property name="exposeSpringMacroHelpers" value="true"/>
</bean>
So I have my HomeController.java
's index view at: /web-inf/freemarker/index.ftl
I am hoping someone can create a dead simple Index action that will create a ModelAndView
and use freemarker
.
I'm not sure how things will wire together, thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
控制器不应该了解 Freemarker,它们应该像任何其他控制器一样,像平常一样构造
ModelAndView
或ModelMap
。FreeMarkerViewResolver
获取ModelAndView
中保存的视图名称,并在内部将其解析为 FreemarkerTemplate
对象,将模型渲染为该对象。所有 freemarker 配置都是FreeMarkerViewResolver
的内部配置,如果您的上下文未正确连接,那么
FreeMarkerViewResolver
将抛出异常,但您当然不需要任何 freemarker在您的控制器中进行配置。The controllers should have no knowledge of Freemarker, they should just look like any other controller, constructing the
ModelAndView
orModelMap
as they normally would. TheFreeMarkerViewResolver
takes the view name held in theModelAndView
and resolves it to a FreemarkerTemplate
object internally, rendering your model into that. All freemarker config is internal to theFreeMarkerViewResolver
If your context is not wired up correctly, then the
FreeMarkerViewResolver
will throw an exception to that effect, but you certainly do not require any freemarker config in your controllers.我认为你不需要 freemarkerConfig。只需稍微更改您的视图解析器:
现在,如果您打开 hppt://localhost:8080/app/index,您将获得渲染的 /WEB-INF/freemarker/index.ftl
You do not need freemarkerConfig, I think. Just change your view resolver a bit:
Now if you open hppt://localhost:8080/app/index, you will get rendered /WEB-INF/freemarker/index.ftl