第一个springBoot项目,使用模板渲染

1. 创建一个 maven 工程


2. pom.xml  添加以下内容 到 </project> 上面 ,等他下载完成所有的包


  <description>Demo project for Spring WebMvc</description>
  <!-- Inherit defaults from Spring Boot -->  
    <parent>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-parent</artifactId>  
        <version>1.4.0.BUILD-SNAPSHOT</version>  
    </parent>  
  
    <!-- Add typical dependencies for a web application -->  
    <dependencies>  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-web</artifactId>  
        </dependency>  
        <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-tomcat</artifactId>
       <scope>provided</scope>
</dependency>
    </dependencies>  
  
    <!-- Package as an executable jar -->  
    <build>  
        <plugins>  
            <plugin>  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-maven-plugin</artifactId>  
            </plugin>  
            <plugin>
  <artifactId>maven-compiler-plugin</artifactId>
  <configuration>
     <source>1.8</source>
     <target>1.8</target>
  </configuration>
</plugin>
        </plugins>  
    </build>  
  
    <!-- Add Spring repositories -->  
    <!-- (you don't need this if you are using a .RELEASE version) -->  
    <repositories>  
        <repository>  
            <id>spring-snapshots</id>  
            <url>http://repo.spring.io/snapshot</url>  
            <snapshots><enabled>true</enabled></snapshots>  
        </repository>  
        <repository>  
            <id>spring-milestones</id>  
            <url>http://repo.spring.io/milestone</url>  
        </repository>  
    </repositories>  
    <pluginRepositories>  
        <pluginRepository>  
            <id>spring-snapshots</id>  
            <url>http://repo.spring.io/snapshot</url>  
        </pluginRepository>  
        <pluginRepository>  
            <id>spring-milestones</id>  
            <url>http://repo.spring.io/milestone</url>  
        </pluginRepository>  
    </pluginRepositories>  

3. 新建一个class Applickation 做为启动类 。 通过main方法启动

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;


@SpringBootApplication  
public class Application extends SpringBootServletInitializer{


  @Override
   protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
       return application.sources(Application.class);
   }

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


}


4.新建一个controller


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
public class HelloController {


    @RequestMapping("/hello/{name}")
    public String hello(@PathVariable("name") String name, Model model) {
        model.addAttribute("name", name);
        return "hello";
    }

    @RequestMapping("/hellorest")
    @ResponseBody
    public String hellorest() {
        return "hello";
    }

}



5.resources 下创建一个templates, 新建 hello.html  ,内容贴下面


<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>

6. 通过main方法启动 Application 



7.查看控制台 出现以下内容 说明启动成功




8.浏览器访问 http://localhost:8080/hello/summer 




这样就成功实现了 将name 渲染到 hello.html 中










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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值