Spring Boot 从零搭建一个财务管理系统笔记(一) ---基础环境和简单demo

1。新建spring-boot项目。File-New-Project 选择 Spring Initializr -填写包名项目名-选择Web勾选Spring Web

2.新建完成后pom文件引用thymeleaf模板(用于解析html和静态资源的模板)

 <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-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

3.新建controller

@Controller
public class IndexConntroller {

    @RequestMapping(value = "/index")
    public String index(Model model){
        model.addAttribute("name","测试传值");
        return "index";
    }

    @RequestMapping(value = "/forms")
    public String forms(Model model){
        return "forms";
    }

    @RequestMapping(value = "/charts")
    public String charts(Model model){
        return "charts";
    }

    @RequestMapping(value = "/tables")
    public String tables(Model model){
        return "tables";
    }

    @RequestMapping(value = "/login")
    public String login(Model model){
        return "login";
    }

    @RequestMapping(value = "/register")
    public String register(Model model){
        return "register";
    }
}

5.项目目录结构如下

目录中html和static资源文件为网上模板文件。

测试传值是否正常

 <div class="sidenav-header-inner text-center"><img src="img/avatar-7.jpg" alt="person" class="img-fluid rounded-circle">
            <h2 class="h5" th:text="${name}"></h2><span>开发管理者</span>
          </div>

index.html使用表达式获取

静态资源获取

    <link rel="stylesheet" href="css/style.default.css" id="theme-stylesheet">
    <!-- Custom stylesheet - for your changes-->
    <link rel="stylesheet" href="css/custom.css">

6.启动项目访问测试

 

7.访问http://127.0.0.1:8080/index

值获取,与静态文件都可以了。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
当然,我可以为你提供一个Spring Boot Starter Data Redis的Demo。以下是一个简单的演示: 首先,确保你已经在你的项目中添加了`spring-boot-starter-data-redis`依赖。 接下来,创建一个Redis配置类,比如`RedisConfig`: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.GenericToStringSerializer; @Configuration public class RedisConfig { @Bean public RedisConnectionFactory redisConnectionFactory() { return new LettuceConnectionFactory(); } @Bean public RedisTemplate<String, Object> redisTemplate() { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory()); redisTemplate.setValueSerializer(new GenericToStringSerializer<>(Object.class)); return redisTemplate; } } ``` 然后,创建一个简单的控制器类,比如`RedisController`: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class RedisController { private final RedisTemplate<String, Object> redisTemplate; @Autowired public RedisController(RedisTemplate<String, Object> redisTemplate) { this.redisTemplate = redisTemplate; } @GetMapping("/set/{key}/{value}") public void setValue(@PathVariable String key, @PathVariable String value) { redisTemplate.opsForValue().set(key, value); } @GetMapping("/get/{key}") public Object getValue(@PathVariable String key) { return redisTemplate.opsForValue().get(key); } } ``` 最后,启动你的Spring Boot应用程序,并通过以下URL进行测试: 设置值:`http://localhost:8080/set/{key}/{value}` 获取值:`http://localhost:8080/get/{key}` 请确保替换`{key}`和`{value}`为实际的键和值。 这只是一个简单的示例,你可以根据你的需求进一步扩展和定制。希望对你有所帮助!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值