解决spring boot 无法访问静态页面的问题(一)

本文介绍了在使用Spring Initializr创建Spring Boot项目后,遇到无法访问静态HTML页面的问题。通过查看控制台日志,发现因HTML标签不完整导致SAXParseException。解决方案是对HTML文件进行修正,确保所有元素都有正确闭合标签。同时分析了Thymeleaf模板引擎对HTML5的严格解析方式,提出可以通过设置模板模式来避免此类问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.采用idea 中的Spring initializr创建Spring boot 项目
2.添加pom依赖
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--用来渲染页面 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
3.新建一个Controller
@Controller
public class HelloController {
   @RequestMapping(value = "/index",method = RequestMethod.GET)
   public String hellohtml ()
   {
       return "/index";
   }
}
4. 在resources中的templates文件夹下添加index.html文件
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" >
    <title>Title</title>
</head>
<body>
这个是首页
</body>
</html>
5. 准备就绪,运行Application文件出现异常。

错误信息

6. 查看控台发现日志信息如下

org.xml.sax.SAXParseException: 元素类型 “meta” 必须由匹配的结束标记 "</meta>" 终止。

7. 解决:将上面html文件做出如下修改
<meta charset="UTF-8" >  更改前
<meta charset="UTF-8" /> 更改后
8. 分析: thymeleaf模板引擎默认是Template modes:HTML5解析的,解析比较严格。当然除此之外,使thymeleaf模板实现html5标签的非严格检查也可以。
Spring Boot 中访问静态页面非常简单,只需要将静态页面放置在指定的目录下即可。默认情况下,Spring Boot静态资源放置在`/static`、`/public`、`/resources`或`/META-INF/resources`目录下。 例如,假设您有个名为`index.html`的静态页面,您可以将其放置在`/src/main/resources/static`目录下,然后通过以下方式访问: ``` http://localhost:8080/index.html ``` 如果您的静态页面放置在其他目录下,可以通过在`application.properties`文件中设置`spring.resources.static-locations`属性来指定静态资源的路径,例如: ``` spring.resources.static-locations=classpath:/static/,classpath:/public/,file:/path/to/your/static/files/ ``` 其中,`classpath:/static/`和`classpath:/public/`是默认的静态资源路径,`file:/path/to/your/static/files/`为您自定义的静态资源路径。 需要注意的是,在访问静态资源时,Spring Boot 会根据静态资源的文件扩展名自动推断其`Content-Type`,例如,`.html`扩展名的文件会被推断为`text/html`类型。如果您的静态资源不是按照扩展名来命名的,需要在`application.properties`文件中配置`spring.mvc.static-path-pattern`属性来指定静态资源的URL。例如,如果您希望访问`.txt`扩展名的静态资源,可以将`spring.mvc.static-path-pattern`设置为`/resources/**`,然后将`.txt`文件放置在`/src/main/resources/resources`目录下,通过以下方式访问: ``` http://localhost:8080/resources/yourfile.txt ``` 希望以上内容能够帮助到您。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值