Spring MVC 和 Velocity:模板结构

2024-01-19

我想实现这种模板功能:

  • 有一个模板,定义完整 (x)html 网页的页眉、页脚和公共部分
  • 当返回字符串时@Controller它将定义包含到模板特定部分的视图

像这样:

@控制器

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

视图/home_view.vm

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

视图/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,这类似于它的模板系统

我怎样才能做到这一点?


最终解决方案基于@内森·布布纳建议。

项目基于春季MVC模板,与Maven受控资源。
Spring版本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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring MVC 和 Velocity:模板结构 的相关文章

随机推荐

  • Java 线程和同步块

    假设我正在执行一个synchronized某个线程内和内的代码块synchronized我调用一个方法 该方法生成另一个线程来处理需要与第一个方法相同的锁的同步代码块 所以在伪 Java 代码中 public void someMethod
  • 是否可以使用媒体查询将一个 CSS 文件包含到另一个 CSS 文件中?

    我有一个很大的 CSS 文件 我想在使用媒体查询的特殊情况下包含其他 CSS 文件 使用安全吗 import在 CSS 中是这样的 media only screen and max width 480px import url css s
  • 如何在 Windows 中运行 makefile

    我有包含文件的文件夹 如何运行makefile 在控制台中我输入 make它说没有找到 makefile 尝试使用 MSYS http www mingw org wiki MSYS http www mingw org wiki MSYS
  • 如何使 Bootstrap 弹出窗口与单独元素中的 HTML 内容一起使用

    我正在将 Bootstrap 面板与 Bootstrap 弹出框功能相结合 目标是当用户将鼠标悬停在面板标题上时显示弹出窗口 我已经让这个工作了 除了data content 当其中包含大量 HTML 时 该部分会变得非常难以管理 下面是我
  • 将服务器端事件添加到扩展器控件

    我有一个扩展控件 可以提升文本框的OnTextChanged用户完成输入后 500 毫秒发生事件 问题在于OnTextChanged当文本框失去焦点时引发 这会导致问题 因为回发 我想做的是给扩展器控件它自己的服务器端事件 比如说 OnDe
  • 如何使用 ggplot2 剪切、裁剪或白色填充紧紧包围多边形外部的矩形

    我只是想用白色填充简单多边形之外的区域 出于某种原因 它在中心画了一根奇怪的木桩 就像它认为这是一个吸血鬼杀手或其他什么东西一样 搞砸了 我尝试跟随这个帖子 https stackoverflow com questions 2128664
  • Vue.js 路由器:历史模式和 AWS S3 (RoutingRules)

    我有一个使用 Amazon S3 和 Cloudflare 启动并运行的 Vue js 应用程序 当我打开索引并浏览到 dashboard 时 一切正常 但是 当我直接在新选项卡中打开仪表板之类的路线或刷新页面时 我从 S3 收到以下错误
  • RoR 设计:sign_in 总是返回无效的电子邮件/密码

    每次登录时 我都会收到错误消息 表明电子邮件 密码无效 routes devise for users devise scope users do get users sign out gt devise sessions destroy
  • 最好的异常处理策略应该是什么

    我正在开发用户从 UI 调用方法的应用程序 在此我从业务类调用一个方法 该方法调用另一个方法 用户界面 gt 方法1 gt 方法2 gt 方法3 如果任何方法中发生任何异常 我想向用户显示错误消息 我应该直接向调用者方法抛出异常吗 在 UI
  • Discord JS - 如何对同一个嵌入多次做出反应?

    我只拿到了第一个 钱袋子 表情符号对频道中的最新消息做出反应 这是机器人发送的嵌入 但是 我希望机器人对新嵌入做出反应 钱袋子 and ticket 表情符号 到目前为止它会与 钱袋子 表情符号 但是 当它尝试与 ticket 表情符号 如
  • Angular 6 Firebase 快照返回未定义

    我正在将对象上传到我的数据库 然后尝试检索所有项目 在第二步中我遇到错误 我的对象类 export class Data key string name string address string address2 string pscod
  • Sublime Text 2:如何在不移动光标的情况下向上/向下翻页

    我使用的是 OS X 10 8 4 ST2 当我使用 Home 和 End 键时 视口移动并且光标保持不变 这是标准的 Mac 行为 也是我所期望的 但是 当我使用 Page Up pageup pgup 和 Page Down paged
  • Python3 shebang 线未按预期工作

    我在 Solaris 环境中运行 Python 脚本时遇到以下问题 看来我在 shebang 线上做了一些不正确的事情 但我无法判断这是 Python 3 问题还是命令行问题 但我怀疑它与 shebang 行有某种关系 因为当我在命令行上显
  • “砰”或“!”是什么意思?在 git 命令之前?

    正如您从这段摘录中看到的 有一个 在 git 命令之前 重点是什么 alias commitx git add git commit https stackoverflow com a 8956546 1354543 https stack
  • 如何每 10 秒发出一次 Ajax 请求(长轮询除外)?

    我尝试使用以下命令每 10 秒从服务器请求一个 json 对象 setInterval function ajax url success function data do stuff with data 10000 但这不是很有效 我了解
  • 使用 WordNet 确定两个文本之间的语义相似度?

    如何使用 WordNet 确定 python 中两个文本之间的语义相似度 明显的预处理是删除停用词和词干 但是然后呢 我能想到的唯一方法是计算两个文本中每个单词之间的 WordNet 路径距离 这是一元语法的标准 但这些都是大型 400 个
  • 无法解析符号“FusedLocationProviderClient”

    我有一个错误无法解析符号 FusedLocationProviderClient 声明时 private FusedLocationProviderClient mFusedLocationClient 这里也问同样的问题无法解析符号 Fu
  • sqlalchemy 按计数列过滤

    我有一个用户查询 它按每个用户拥有的订单数量 ordersCount 进行过滤 User query filter ordersCount gt 2 如果我运行它 它会显示 where 子句 中的未知列 ordersCount 根据我的经验
  • 捕获文本框滚动事件?

    Textbox or richtextbox 我唯一想要的就是当滚动条移动时触发一个函数 我已经找到了GetScrollPos and SetScrollPos 我想过定期检查滚动条位置 但必须有更好的方法 那么 使用 WinForms 更
  • Spring MVC 和 Velocity:模板结构

    我想实现这种模板功能 有一个模板 定义完整 x html 网页的页眉 页脚和公共部分 当返回字符串时 Controller它将定义包含到模板特定部分的视图 像这样 控制器 RequestMapping value method Reques