Spring MVC &速度:模板结构 [英] Spring MVC & Velocity : Template structure

查看:22
本文介绍了Spring MVC &速度:模板结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现这种模板功能:

I'd like to achieve this kind of template functionality:

  • 具有定义完整 (x)html 网页的页眉、页脚和公共部分的模板
  • 当从 @Controller 返回 String 时,它将定义包含到模板特定部分的视图
  • Have a template defining header, footer, and common parts of complete (x)html webpage
  • When returning String from @Controller it will define the view included to specific part of template

像这样:

@Controller

@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
    return "home_view";
}

views/home_view.vm

<h2>Content title</h2>
<p>Content text</p>

views/template.vm

<html>
    <head>
        <title></title>
    </head>
    <body>
        <!-- Header of page -->
        #include({context variable which contains "home_view"} + ".vm");
        <!-- Footer of page -->
    </body>
</html>

如果有人知道 CakePHP,这类似于它的模板系统

我该怎么做?

推荐答案

基于 @Nathan Bubna 建议的最终解决方案.

Final solution based on @Nathan Bubna suggestion.

项目基于 Spring MVC 模板,具有 Maven 受控资源.
Spring 版本 3.1.1.RELEASE

Project is based on Spring MVC template, with Maven controlled resources.
Spring version 3.1.1.RELEASE

/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>mainServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mainServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

/WEB-INF/spring/servlet/servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing 
        infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
        up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />
    <resources location="/resources/favicon.ico" mapping="/favicon.ico"/>
    <resources location="/resources/favicon.ico" mapping="/favicon.png"/>

    <beans:bean id="velocityConfig"
        class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
        <beans:property name="resourceLoaderPath" value="/WEB-INF/views/" />
    </beans: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. -->
    <beans:bean id="viewResolver"
        class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
        <beans:property name="cache" value="true" />
        <beans:property name="prefix" value="" />
        <beans:property name="layoutUrl" value="layout.vm"></beans:property>
        <beans:property name="suffix" value=".vm" />
    </beans:bean>

    <context:component-scan base-package="com.mypackage.subpackage" />

</beans:beans>

重要的 pom.xml 行

<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity</artifactId>
    <version>1.7</version>
</dependency>
<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity-tools</artifactId>
    <version>2.0</version>
</dependency>

这篇关于Spring MVC &amp;速度:模板结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆