Freemarker 与 spring mvc,那么我的操作会是什么样子?

发布于 2024-09-07 13:15:35 字数 1145 浏览 7 评论 0原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧伤还要旧人安 2024-09-14 13:15:35

控制器不应该了解 Freemarker,它们应该像任何其他控制器一样,像平常一样构造 ModelAndViewModelMapFreeMarkerViewResolver 获取 ModelAndView 中保存的视图名称,并在内部将其解析为 Freemarker Template 对象,将模型渲染为该对象。所有 freemarker 配置都是 FreeMarkerViewResolver 的内部配置,

如果您的上下文未正确连接,那么 FreeMarkerViewResolver 将抛出异常,但您当然不需要任何 freemarker在您的控制器中进行配置。

The controllers should have no knowledge of Freemarker, they should just look like any other controller, constructing the ModelAndView or ModelMap as they normally would. The FreeMarkerViewResolver takes the view name held in the ModelAndView and resolves it to a Freemarker Template object internally, rendering your model into that. All freemarker config is internal to the FreeMarkerViewResolver

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.

相守太难 2024-09-14 13:15:35

我认为你不需要 freemarkerConfig。只需稍微更改您的视图解析器:

<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
    <property name="cache" value="true"/>
    <property name="prefix" value="/WEB-INF/freemarker/"/>
    <property name="suffix" value=".ftl"/>
    <property name="exposeSpringMacroHelpers" value="true"/>
</bean>

现在,如果您打开 hppt://localhost:8080/app/index,您将获得渲染的 /WEB-INF/freemarker/index.ftl

You do not need freemarkerConfig, I think. Just change your view resolver a bit:

<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
    <property name="cache" value="true"/>
    <property name="prefix" value="/WEB-INF/freemarker/"/>
    <property name="suffix" value=".ftl"/>
    <property name="exposeSpringMacroHelpers" value="true"/>
</bean>

Now if you open hppt://localhost:8080/app/index, you will get rendered /WEB-INF/freemarker/index.ftl

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文