初学springboot

本文介绍了Spring Boot整合HTML和JSP的方法。整合HTML需在pom.xml添加依赖,创建HTML文件和控制层;整合JSP同样要在pom.xml导入依赖,创建JSP文件,配置application.properties,还需对webapp文件夹进行配置。最后给出了相应的访问地址。

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

html

在pom.xml文件中添加依赖
 <!-- thymeleaf模板引擎依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
在main/resources/templates下建一个html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8"/>
    <title>yyyy</title>

</head>
<body>
<h1 th:text="${message}">333</h1>
<h1>hello mmmm</h1>
</body>
</html>
在main/java/com.springboot.demo建一个控制层Controller/hello.java
@Controller
@RequestMapping("/hh")
public class hello {

    @RequestMapping("/aa")
    public  String hello(){
        return  "hello";
    }
    @RequestMapping("/hello1")
    public  String hello1(Model model){
        model.addAttribute("message","http:/www.ityouknow.com");
        return  "hello";
    }
  • 这个时候只需要在浏览器上打上localhost:8080/hh/aa或者localhost:8080/hh/hello1即可

jsp

也需要在pom.xml中导入依赖
</dependency>
        <!--配置支持jsp(JSP和Thymeleaf都属于View层技术,二者二选其一)如果这两个依赖共存时,idea会优先选择thymeleaf 不再支持jsp的页面的展示-->
        <!--<dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>-->
        
        <!-- thymeleaf模板引擎依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
在main下建/webapp/WEB-INF/index.jsp

这个时候webapp文件夹是灰色的,需要按住ctrl+alt+shift+s键配置一些东西来点亮它
在这里插入图片描述

接着在resources下/templates的application.properties添加前缀和后缀
spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp
在Controller层的hello.java中
@Controller
@RequestMapping("/hh")
public class hello {
@RequestMapping("/bb")
    public  String hello2(Model model){
        model.addAttribute("message","http:/www.ityouknow.com");
        return  "index";
    }
}
  • 在浏览器中输入localhost:8080/hh/bb
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值