Spring Boot渲染Web页面

渲染Web页面

在之前的示例中,我们都是通过@RestController来处理请求,所以返回的内容为json对象。那么如果需要渲染html页面的时候,要如何实现呢?
模板引擎
在动态HTML实现上Spring Boot依然可以完美胜任,并且提供了多种模板引擎的默认配置支持,所以在推荐的模板引擎下,我们可以很快的上手开发动态网站。
Spring Boot提供了默认配置的模板引擎主要有以下几种:
• Thymeleaf
• FreeMarker
• Velocity
• Groovy
• Mustache
Spring Boot建议使用这些模板引擎,避免使用JSP,若一定要使用JSP将无法实现Spring Boot的多种特性,具体可见后文:支持JSP的配置
当你使用上述模板引擎中的任何一个,它们默认的模板配置路径为:src/main/resources/templates。当然也可以修改这个路径,具体如何修改,可在后续各模板引擎的配置属性中查询并修改。

使用Freemarker模板引擎渲染web视图

pom文件引入
        <!-- 引入freeMarker的依赖包. -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
后台代码
package com.cc.springboot;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.w3c.dom.ls.LSInput;

@Controller
public class IndexControler {

    @RequestMapping("/index")
    public String index(ModelMap map) {
        map.addAttribute("name", "kevin");
        map.put("sex",1);//0 男 1 女 其他
        List<String> userList=new ArrayList<String>();
        userList.add("hubbert");
        userList.add("cai");
        userList.add("xu");
        map.addAttribute("userList", userList);
        return "index";
    }

}
前台代码

在src/main/resources/创建一个templates文件夹,后缀为*.ftl

index.ftl页面:

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title></title>
</head>
<body>
      ${name}
      <#if sex==1><#elseif sex==2><#else>
      其他
      </#if>

      <#list userList as user>
      ${user}
      </#list> 

</body> 
</html>

访问:http://localhost:8080/index
这里写图片描述

Freemarker配置

新建application.properties文件

########################################################
###FREEMARKER (FreeMarkerAutoConfiguration)
########################################################
spring.freemarker.allow-request-override=false
spring.freemarker.cache=true
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
#spring.freemarker.prefix=
#spring.freemarker.request-context-attribute=
#spring.freemarker.settings.*=
spring.freemarker.suffix=.ftl
spring.freemarker.template-loader-path=classpath:/templates/
#comma-separated list
#spring.freemarker.view-names= # whitelist of view names that can be resolved

使用JSP渲染Web视图

建一个打包类型为war类型的maven项目。
这里写图片描述

pom文件引入以下依赖
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>
    </parent>
    <dependencies>
        <!-- SpringBoot 核心组件 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
    </dependencies>

这里写图片描述

在application.properties创建以下配置
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

然后建一个WEB-INF/jsp/index.jsp
这里写图片描述

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
   this is springboot 
</body>
</html>

然后运行访问http://127.0.0.1:8080/index
这里写图片描述

顺便推荐一个写的挺好的博客:Spring Boot干货系列:(五)开发Web应用之JSP篇

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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、付费专栏及课程。

余额充值