首页
/ Spring Velocity 支持项目教程

Spring Velocity 支持项目教程

2024-08-07 07:13:00作者:廉彬冶Miranda

1. 项目介绍

Spring Velocity Support 是阿里巴巴贡献的一个开源项目,它基于 Spring Framework 提供了对旧版 Velocity 的支持。由于 Spring 从 5.0 版本起不再内置支持 Velocity 模板引擎,此项目旨在帮助那些仍然依赖于 Velocity 的项目平滑过渡。该项目也作为阿里巴巴 velocity-spring-boot-project 的基础。

2. 项目快速启动

要将 Spring Velocity Support 集成到你的项目中,首先确保你的 pom.xml 文件包含了以下依赖:

<dependencies>
    <!-- 引入 Spring Context Support -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>你的 Spring Framework 版本</version>
    </dependency>

    <!-- 引入 Spring Velocity Support -->
    <dependency>
        <groupId>com.alibaba.spring</groupId>
        <artifactId>spring-velocity-support</artifactId>
        <version>项目的具体版本(例如:1.0.0)</version>
    </dependency>
</dependencies>

接下来,你需要配置 VelocityEngine 和相应的模板路径。在 Spring 配置文件(如:applicationContext.xml 或使用 @Configuration 注解的类)中添加以下配置:

@Configuration
public class VelocityConfig {

    @Value("${velocityEngine.resource.loader.path:/templates/}")
    private String templatePath;

    @Bean(name = "velocityEngine")
    public VelocityEngine velocityEngine() {
        Properties props = new Properties();
        props.setProperty("resource.loader", "class");
        props.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        props.setProperty("input.encoding", "UTF-8");
        props.setProperty("output.encoding", "UTF-8");

        return new VelocityEngine(props);
    }

    @Bean
    public VelocityTemplateViewResolver viewResolver(VelocityEngine engine) {
        VelocityTemplateViewResolver resolver = new VelocityTemplateViewResolver();
        resolver.setEngine(engine);
        resolver.setPrefix(templatePath);
        resolver.setSuffix(".vm");
        resolver.setContentType("text/html;charset=UTF-8");
        resolver.setOrder(1);
        return resolver;
    }
}

现在,你可以创建一个 Velocity 模板(例如:/templates/home.vm),并用 Spring MVC 来处理视图渲染。创建一个控制器方法:

@Controller
public class HomeController {

    @RequestMapping("/home")
    public ModelAndView home() {
        Map<String, Object> model = new HashMap<>();
        model.put("title", "欢迎来到主页");
        return new ModelAndView("home", model);
    }
}

运行项目,访问 /home 将加载并渲染 home.vm 模板。

3. 应用案例和最佳实践

  • 多语言支持:在 Velocity 模板中使用条件判断来选择不同语言的文本资源。
  • 重用组件:创建可重用的 Velocity 非模态块(macro),并在多个模板中导入并使用。
  • 数据预处理:在控制器中预处理模型数据,以减少模板中的业务逻辑。

4. 典型生态项目

  • Spring Boot:虽然 Spring Boot 不再默认支持 Velocity,但通过以上配置,可以在 Spring Boot 项目中使用 Spring Velocity Support 实现 Velocity 功能。
  • FreemarkerThymeleaf:如果你的项目考虑迁移到其他模板引擎,Spring Boot 官方支持 Freemarker 和 Thymeleaf。

完成上述步骤后,你应该能够在你的 Spring 项目中成功地使用 Velocity 作为模板引擎。更多关于 Spring Velocity Support 的详细信息,请参考项目仓库的 Readme 和示例代码。

登录后查看全文
热门项目推荐

项目优选

收起
docsdocs
暂无描述
Markdown
827
5.49 K
kernelkernel
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
494
518
ops-nnops-nn
本项目是CANN提供的神经网络类计算算子库,实现网络在NPU上加速计算。
C++
786
1.58 K
pytorchpytorch
作为 Ascend for PyTorch 社区的核心组件,TorchNPU 是昇腾专为 PyTorch 打造的深度学习适配插件,使 PyTorch 框架能够直接调用昇腾 NPU,为开发者提供昇腾 AI 处理器的超强算力。
Python
803
1.14 K
ops-transformerops-transformer
本项目是CANN提供的transformer类大模型算子库,实现网络在NPU上加速计算。
C++
973
2.29 K
kernelkernel
deepin linux kernel
C
32
16
AscendNPU-IRAscendNPU-IR
AscendNPU-IR是基于MLIR(Multi-Level Intermediate Representation)构建的,面向昇腾亲和算子编译时使用的中间表示,提供昇腾完备表达能力,通过编译优化提升昇腾AI处理器计算效率,支持通过生态框架使能昇腾AI处理器与深度调优
C++
482
312
jiuwenswarmjiuwenswarm
JiuwenSwarm 是一款基于openJiuwen开发的智能AI Agent,它能够将大语言模型的强大能力,通过你日常使用的各类通讯应用,直接延伸至你的指尖。
Python
3.02 K
769
cannbot-skillscannbot-skills
CANNBot 是面向 CANN 开发的用于提升开发效率的系列智能体,本仓库为其提供可复用的 Skills 模块。
Markdown
1.26 K
811
cann-learning-hubcann-learning-hub
CANN 学习中心仓,支持在线互动运行、边学边练,提供教程、示例与优化方案,一站式助力昇腾开发者快速上手。
Jupyter Notebook
648
287