SpringBoot +BootStrap 使用thymeleaf模板跳页面

博客主要介绍了Spring Boot使用Thymeleaf跳转到List页面的注意事项。一是方法上,返回页面需添加斜杠/;二是配置文件application.yml中,templates后面不能加斜杠/;此外还提到因MyBatis逆向文件需修改pom文件扫描文件,以及注释resources相关内容。

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

怎么跳到list页面

@RequestMapping("/list")
    public String list(HttpServletRequest request, HttpServletResponse response){
        List<ScheduleTrigger>  s = scheduleTriggerService.queryScheduleTriggerLst();
        request.setAttribute("teachers",s);
        return "/list";
    }

注意点

1.方法注意

返回的页面不像ssm一样写一个名字 需要添加一个斜杠/

2.配置文件注意 application.yml

templates后面不能加斜杠/

spring:
  thymeleaf:
    cache: false
    prefix: classpath:/templates
    suffix: .html
    encoding: UTF-8
    mode: LEGACYHTML5

pom文件的修改

因为mybat逆向文件的原因要扫描文件 

resources里面是这样的(把后面那个resources注释掉)

<think>好的,用户想要用Spring Boot、Bootstrap、MySQL和Thymeleaf构建课程管理系统,需要教程或示例。我得先整理相关技术栈的实现步骤,结合之前的引用内容来提供指导。 首先,根据引用[4],Spring Boot可以整合Spring Data JPA、Hibernate操作数据库,Thymeleaf做前端,Bootstrap美化界面。所以项目结构应该包括MVC架构,实体类、仓库接口、控制器、模板和配置。 然后数据库配置方面,引用[5]提到了在application.properties中配置MySQL连接,包括URL、用户名、密码和驱动类。需要确保用户正确设置这些参数,并且创建对应的数据库。 接下来,实体类对应课程信息,使用JPA注解定义表和字段。Repository接口继承JpaRepository,这样就能获得基本的CRUD操作。这部分需要示例代码,展示如何定义实体和仓库。 控制器部分,处理HTTP请求,比如显示课程列表、添加、编辑、删除课程。引用[2]中的MultipartFile处理文件上传,但用户需求是课程管理,可能不需要文件上传,但可以保留作为扩展。不过用户的问题可能更关注CRUD操作,所以重点在GET和POST方法,如保存课程、删除课程等。 前端使用Thymeleaf模板,结合Bootstrap样式。需要创建HTML页面使用Bootstrap的导航栏、表格和表单组件。引用[3]中的Thymeleaf示例可能涉及数据绑定和迭代,比如用th:each循环显示课程列表。 分页和排序功能可以参考引用[1]中的分页示例,使用Spring Data的Pageable接口,结合Thymeleaf的分页控件。这可能作为进阶内容提到,但用户的问题可能优先基础功能,所以可以放在相关扩展问题里。 另外,用户提供的引用中有文件上传的例子,如引用[2],但课程管理系统是否需要文件上传?可能用户没有提到,但可以建议如果有需要,可以参考这部分实现。不过当前问题应聚焦在核心功能上。 测试部分需要确保各层正常工作,比如服务层、控制层,以及前端页面的交互。用户可能需要了解如何运行应用,访问不同路由,比如/courses显示列表,/add显示添加表单等。 安全方面,引用[4]提到Spring Security,但用户的问题中没有明确需要身份验证,是否需要包含?可能作为扩展建议,比如添加登录功能,但基础教程可能先不涉及,放在相关问题里。 最后,需要生成相关问题,比如如何添加分页、搜索功能,或者整合安全模块,参考之前的引用内容,引导用户进一步学习。 总结步骤:配置项目依赖、数据库设置、实体类、仓库、控制器、模板页面、运行测试。每个部分给出代码示例,并引用对应的引用来源。</think>### Spring Boot + Bootstrap + MySQL + Thymeleaf 课程管理系统实现步骤 #### 1. 项目初始化与依赖配置 - **Spring Initializr** 创建项目,选择依赖: ```xml <!-- Spring Boot Starter Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Data JPA --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- MySQL 驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- Thymeleaf --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ``` 引用[4]提到通过Spring Data JPA和Hibernate操作数据库[^4]。 #### 2. 数据库配置 在 `application.properties` 中配置MySQL连接: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/course_db spring.datasource.username=root spring.datasource.password=your_password spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.hibernate.ddl-auto=update ``` 引用[5]提供了数据库配置的示例[^5]。 #### 3. 实体类定义 创建课程实体类 `Course.java`: ```java @Entity public class Course { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private String instructor; private int credit; // getter/setter省略 } ``` #### 4. 数据访问层 定义仓库接口 `CourseRepository.java`: ```java public interface CourseRepository extends JpaRepository<Course, Long> { } ``` #### 5. 控制器实现 编写控制器 `CourseController.java`: ```java @Controller public class CourseController { @Autowired private CourseRepository courseRepository; @GetMapping("/courses") public String listCourses(Model model) { model.addAttribute("courses", courseRepository.findAll()); return "course-list"; } @GetMapping("/add") public String showAddForm(Model model) { model.addAttribute("course", new Course()); return "add-course"; } @PostMapping("/save") public String saveCourse(@ModelAttribute Course course) { courseRepository.save(course); return "redirect:/courses"; } @GetMapping("/delete/{id}") public String deleteCourse(@PathVariable Long id) { courseRepository.deleteById(id); return "redirect:/courses"; } } ``` #### 6. 前端页面Thymeleaf + Bootstrap) - **课程列表页** `course-list.html`: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <div class="container mt-4"> <h2>课程列表</h2> <a href="/add" class="btn btn-primary mb-3">新增课程</a> <table class="table"> <tr> <th>课程名称</th> <th>授课教师</th> <th>学分</th> <th>操作</th> </tr> <tr th:each="course : ${courses}"> <td th:text="${course.name}"></td> <td th:text="${course.instructor}"></td> <td th:text="${course.credit}"></td> <td> <a th:href="@{/delete/{id}(id=${course.id})}" class="btn btn-danger">删除</a> </td> </tr> </table> </div> </body> </html> ``` - **添加课程页** `add-course.html`: ```html <form th:action="@{/save}" th:object="${course}" method="post"> <div class="mb-3"> <label class="form-label">课程名称</label> <input type="text" class="form-control" th:field="*{name}"> </div> <div class="mb-3"> <label class="form-label">授课教师</label> <input type="text" class="form-control" th:field="*{instructor}"> </div> <div class="mb-3"> <label class="form-label">学分</label> <input type="number" class="form-control" th:field="*{credit}"> </div> <button type="submit" class="btn btn-success">保存</button> </form> ``` 引用[2]展示了ThymeleafBootstrap结合的表单处理[^2]。 #### 7. 运行与测试 启动Spring Boot应用后,访问 `http://localhost:8080/courses` 即可管理课程数据。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值