Spring Boot学习(四)之web开发渲染页面 -- Velocity

上篇编写了Spring Boot学习(四)之web开发渲染页面 -- Freemarker

接下来简单介绍一下Velocity整合,跟Freemarker是一样的,没有什么太大区别

下面开始看pol. xml引入的jar

需把

<dependencies>  
    <dependency>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-freemarker</artifactId>  
    </dependency>  
  </dependencies>  

替换为

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-velocity</artifactId>
</dependency>

index.ftl改为index.vm

内容为:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8" />
    <title></title>
</head>
<body>
Velocity模板
<h1>${name}</h1>
</body>
</html>

启动项目:

访问http://localhost:8080/

结果显示为:

筱进GG

 在spring boot的application.properties属性文件中为velocity提供了一些常用的配置,如下:

spring.velocity.allow-request-override=false # Set whether HttpServletRequest attributes are allowed to override (hide) controller generated model attributes of the same name.
spring.velocity.allow-session-override=false # Set whether HttpSession attributes are allowed to override (hide) controller generated model attributes of the same name.
spring.velocity.cache= # Enable template caching.
spring.velocity.charset=UTF-8 # Template encoding.
spring.velocity.check-template-location=true # Check that the templates location exists.
spring.velocity.content-type=text/html # Content-Type value.
spring.velocity.date-tool-attribute= # Name of the DateTool helper object to expose in the Velocity context of the view.
spring.velocity.enabled=true # Enable MVC view resolution for this technology.
spring.velocity.expose-request-attributes=false # Set whether all request attributes should be added to the model prior to merging with the template.
spring.velocity.expose-session-attributes=false # Set whether all HttpSession attributes should be added to the model prior to merging with the template.
spring.velocity.expose-spring-macro-helpers=true # Set whether to expose a RequestContext for use by Spring's macro library, under the name "springMacroRequestContext".
spring.velocity.number-tool-attribute= # Name of the NumberTool helper object to expose in the Velocity context of the view.
spring.velocity.prefer-file-system-access=true # Prefer file system access for template loading. File system access enables hot detection of template changes.
spring.velocity.prefix= # Prefix that gets prepended to view names when building a URL.
spring.velocity.properties.*= # Additional velocity properties.
spring.velocity.request-context-attribute= # Name of the RequestContext attribute for all views.
spring.velocity.resource-loader-path=classpath:/templates/ # Template path.
spring.velocity.suffix=.vm # Suffix that gets appended to view names when building a URL.
spring.velocity.toolbox-config-location= # Velocity Toolbox config location. For instance `/WEB-INF/toolbox.xml`
spring.velocity.view-names= # White list of view names that can be resolved.   


就大功搞成了,这个跟freemarker差不多不做过多的介绍了!

 

Web Spring Boot 是一种用于快速开发 Web 应用程序的框架。它是基于 Spring 框架的轻量级解决方案,提供了强大的开发工具和功能,可帮助开发人员更高效地构建和部署网页应用。 Web Spring Boot 的实践过程通常包括以下几个步骤: 1. 配置项目环境:首先,我们需要配置项目的开发环境。我们可以使用 Maven 或 Gradle 来管理项目依赖,并选择适合的开发工具,如 Eclipse 或 IntelliJ IDEA。 2. 创建 Spring Boot 项目:创建一个基于 Spring BootWeb 项目。我们可以通过 Spring Initializr 网站或使用命令行工具来创建项目。 3. 定义数据模型:根据应用需求,我们可以使用 Java 类来定义数据模型。这些类通常称为实体类,我们可以使用注解来指定数据表、字段以及关系等。 4. 编写控制器:控制器是处理用户请求的核心部分。我们可以使用注解来标识请求 URL、请求类型以及方法,然后在方法中编写业务逻辑。控制器的返回值通常是一个视图,我们可以通过模板引擎将动态数据渲染到视图中。 5. 配置视图模板:视图模板是用于渲染动态内容的文件。Spring Boot 支持多种模板引擎,如 Thymeleaf、Freemarker 和 Velocity。我们可以选择适合自己的模板引擎,并进行相应的配置。 6. 完成业务逻辑:根据应用需求,我们可以编写其他的业务逻辑代码,如数据验证、数据处理、权限控制等。 7. 部署和测试:完成开发后,我们可以将项目部署到服务器上,并进行测试。可以使用内嵌的 Tomcat 容器来启动项目,并使用 Postman 或浏览器来发送请求,验证项目的功能和性能。 Web Spring Boot 的优势在于提供了快速开发的能力,减少了繁琐的配置工作,让开发人员更专注于业务逻辑的实现。它还提供了一系列的自动化工具和库,如自动配置、自动刷新等,大大提高了开发效率。此外,Spring Boot 还提供了丰富的扩展和集成能力,可以与其他框架和服务进行无缝集成,满足不同项目的需求。 总而言之,Web Spring Boot 是一种强大且高效的开发框架,适用于开发各种规模的 Web 应用程序。通过合理的配置和编写代码,开发人员可以快速构建出功能完善、高性能的网页应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值