Springboot官网学习(7、Web应用程序【五Spring Web MVC 之模板引擎以及静态资源】)

18 篇文章 0 订阅
17 篇文章 0 订阅

模板引擎,相信在此之前,我们最熟悉的应该是jsp了吧,SSH或者SSM项目基本使用的都是jsp,而今天的springboot却让我们不用jsp,已知原因如下:

对于Jetty和Tomcat,如果使用war,它应该可以工作。使用可执行jar时,不支持JSP。
Undertow不支持JSP。
创建自定义error.jsp页面不会覆盖默认视图以进行错误处理。 应改用自定义错误页面。

由于spring boot支持 Netty,Tomcat,Jetty和Undertow,所以从大局考虑,介意开发者不使用jsp了,
那么我们今天就是用thymeleaf模板引擎,也是springboot推荐得一种。
创建一个maven项目,

1、然后在pom文件中加入web依赖和模板依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.osy</groupId>
    <artifactId>SringbootGetStart</artifactId>
    <version>1.0-SNAPSHOT</version>


    <!--Springboot的必须必须依赖,他是parent标签,有兴趣点击进去,他帮我们维护了很多包-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
    </parent>
	<!--指定阿里云仓库-->
    <repositories>
        <repository>
            <id>alimaven</id>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        </repository>
    </repositories>


    <dependencies>
   		 <!--web的依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--thymeleaf模板引擎依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>
2、创建引用启动类:
package com.osy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@EnableWebMvc // 启用webMvc
@SpringBootApplication // 启动应用注解
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}
3、在src/main/resources下面创建static和templates目录,并且在static创建index.js和templates下创建index.html

在这里插入图片描述

4、创建IndexController.java
package com.osy.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class IndexController {

    @RequestMapping("index")
    public String index(){
        return "index";
    }
}
5、启动项目,访问http://localhost:8080/index

在这里插入图片描述
访问成功!
这里我使用的2.3.1,对于js或者css刚开始是无法访问的,官方说的放在static或者public下面就是静态资源,可以访问的,但是我的这里是404,原因未知,所以就自定义了静态资源访问。

package com.osy.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfiguration {

    @Bean
    public WebMvcConfigurer webMvcConfigurer() {
        return new WebMvcConfigurer() {
            // 添加静态资源注册
            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
            }

        };
    }
}

如果你的项目也访问不到,不妨这样试试。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值