Quellcode durchsuchen

0122 thymeleaf demo

Qing vor 1 Jahr
Ursprung
Commit
ee80371779

+ 17 - 0
springmvc-demo/pom.xml

@@ -142,6 +142,23 @@
             <version>2.16.0</version>
         </dependency>
 
+        <dependency>
+            <groupId>org.webjars</groupId>
+            <artifactId>webjars-locator</artifactId>
+            <version>0.50</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.webjars</groupId>
+            <artifactId>bootstrap</artifactId>
+            <version>5.3.2</version>
+        </dependency>
+        <dependency>
+            <groupId>org.webjars</groupId>
+            <artifactId>jquery</artifactId>
+            <version>3.7.1</version>
+        </dependency>
+
     </dependencies>
     <build>
         <finalName>springmvc-demo</finalName>

+ 24 - 1
springmvc-demo/src/main/java/com/sf/anno/SpringMvcConfig.java

@@ -2,9 +2,14 @@ package com.sf.anno;
 
 import org.springframework.context.annotation.ComponentScan;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
 import org.springframework.context.annotation.Bean;
 import org.springframework.web.servlet.ViewResolver;
+import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+import org.springframework.web.servlet.resource.PathResourceResolver;
+import org.springframework.web.servlet.resource.WebJarsResourceResolver;
 import org.thymeleaf.spring6.SpringTemplateEngine;
 import org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver;
 import org.thymeleaf.spring6.view.ThymeleafViewResolver;
@@ -18,7 +23,7 @@ import org.thymeleaf.templateresolver.ITemplateResolver;
 @ComponentScan("com.sf.controller")
 // mvc注解驱动
 @EnableWebMvc
-public class SpringMvcConfig {
+public class SpringMvcConfig implements WebMvcConfigurer {
     // 扫描组件
     // 视图解析器
     @Bean
@@ -53,4 +58,22 @@ public class SpringMvcConfig {
         viewResolver.setTemplateEngine(templateEngine);
         return viewResolver;
     }
+
+    @Override
+    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
+        configurer.enable();
+    }
+
+    // WebMvcConfigurer 是针对mvc进行更多配置的一个接口
+    // 我们需要设置资源处理规则时  需要实现
+    //   对于接口提供的参数 ResourceHandlerRegistry 是一个注册中心
+    @Override
+    public void addResourceHandlers(ResourceHandlerRegistry registry) {
+        // 我们可以使用链式编程的方法
+        // 当多个方法返回的都是同一类型/同一对象 ResourceHandlerRegistration
+        registry.addResourceHandler("/webjars/**")
+                .addResourceLocations("/webjars/").resourceChain(false)
+                .addResolver(new WebJarsResourceResolver())
+                .addResolver(new PathResourceResolver());
+    }
 }

+ 67 - 0
springmvc-demo/src/main/java/com/sf/controller/RoleController.java

@@ -0,0 +1,67 @@
+package com.sf.controller;
+
+import com.sf.entity.Role;
+import com.sf.service.RoleService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+
+import java.util.List;
+
+// 在正确的包路径下 创建正确的名字
+// 创建类之后 要思考 需要处理的是页面还是数据
+@Controller
+@RequestMapping("/role")
+public class RoleController {
+
+    @Autowired
+    private RoleService roleService;
+
+    // 方法 需要返回string  是页面的名字
+    @GetMapping("/list")
+    public String list(Model model) {
+        List<Role> roleList = roleService.queryRoles();
+        model.addAttribute("roleList", roleList);
+        return "demo/list";
+    }
+
+    @GetMapping("/delete")
+    public String delete(@RequestParam("id") int id) {
+        // 常见的删除处理 是删除数据后 重新请求列表页
+        roleService.delete(id);
+        return "redirect:/role/list";
+    }
+
+    @GetMapping("/toAdd")
+    public String toAdd() {
+        System.out.println("toAdd");
+        return "demo/add";
+    }
+
+    @PostMapping("/add")
+    public String add(Role role) {
+        System.out.println("add param: " + role);
+        roleService.add(role);
+        return "redirect:/role/list";
+    }
+
+    @GetMapping("/toUpdate")
+    public String toUpdate(@RequestParam("id") int id, Model model) {
+        System.out.println("toUpdate id: " + id);
+        Role role = roleService.queryRoleById(id);
+        model.addAttribute("role", role);
+        return "demo/update";
+    }
+
+    @PostMapping("/update")
+    public String update(Role role) {
+        System.out.println("update param: " + role);
+        roleService.update(role);
+        return "redirect:/role/list";
+    }
+
+}

+ 17 - 0
springmvc-demo/src/main/java/com/sf/dao/RoleDao.java

@@ -0,0 +1,17 @@
+package com.sf.dao;
+
+import com.sf.entity.Role;
+
+import java.util.List;
+
+public interface RoleDao {
+
+    List<Role> findAll();
+    Role findById(int id);
+
+    int delete(int id);
+
+    int add(Role role);
+
+    int update(Role role);
+}

+ 18 - 0
springmvc-demo/src/main/java/com/sf/entity/Role.java

@@ -0,0 +1,18 @@
+package com.sf.entity;
+
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.io.Serializable;
+
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+@Builder
+public class Role implements Serializable {
+    private int id;
+    private String name;
+    private String desc;
+}

+ 18 - 0
springmvc-demo/src/main/java/com/sf/service/RoleService.java

@@ -0,0 +1,18 @@
+package com.sf.service;
+
+import com.sf.entity.Role;
+
+import java.util.List;
+
+public interface RoleService {
+
+    List<Role> queryRoles();
+
+    Role queryRoleById(int id);
+
+    int delete(int id);
+
+    int add(Role role);
+
+    int update(Role role);
+}

+ 42 - 0
springmvc-demo/src/main/java/com/sf/service/impl/RoleServiceImpl.java

@@ -0,0 +1,42 @@
+package com.sf.service.impl;
+
+import com.sf.dao.RoleDao;
+import com.sf.entity.Role;
+import com.sf.service.RoleService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+
+// 创建实现类时  先给一个注解
+@Service
+public class RoleServiceImpl implements RoleService {
+
+    @Autowired
+    private RoleDao roleDao;
+
+    @Override
+    public List<Role> queryRoles() {
+        return roleDao.findAll();
+    }
+
+    @Override
+    public Role queryRoleById(int id) {
+        return roleDao.findById(id);
+    }
+
+    @Override
+    public int delete(int id) {
+        return roleDao.delete(id);
+    }
+
+    @Override
+    public int add(Role role) {
+        return roleDao.add(role);
+    }
+
+    @Override
+    public int update(Role role) {
+        return roleDao.update(role);
+    }
+}

+ 26 - 0
springmvc-demo/src/main/resources/mapper/roleMapper.xml

@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.sf.dao.RoleDao">
+    <select id="findAll" resultType="com.sf.entity.Role">
+        select * from role
+    </select>
+
+    <select id="findById" parameterType="int" resultType="com.sf.entity.Role">
+        select * from role where id = #{id}
+    </select>
+
+    <delete id="delete" parameterType="int">
+        delete from role where id = #{id}
+    </delete>
+
+    <insert id="add" parameterType="com.sf.entity.Role">
+        insert into role(`name`,`desc`) values (#{name},#{desc})
+    </insert>
+
+    <update id="update" parameterType="com.sf.entity.Role">
+        update role
+        set `name` = #{name},
+            `desc` = #{desc}
+        where id = #{id}
+    </update>
+</mapper>

+ 10 - 0
springmvc-demo/src/main/resources/spring-mvc.xml

@@ -26,4 +26,14 @@
         <property name="templateEngine" ref="templateEngine" />
         <property name="characterEncoding" value="UTF-8"/>
     </bean>
+
+    <mvc:default-servlet-handler />
+    <mvc:resources mapping="/webjars/**" location="/webjars/">
+        <mvc:resource-chain resource-cache="false" auto-registration="false">
+            <mvc:resolvers>
+                <bean class="org.springframework.web.servlet.resource.WebJarsResourceResolver"></bean>
+                <bean class="org.springframework.web.servlet.resource.PathResourceResolver"></bean>
+            </mvc:resolvers>
+        </mvc:resource-chain>
+    </mvc:resources>
 </beans>

+ 37 - 0
springmvc-demo/src/main/webapp/WEB-INF/templates/demo/add.html

@@ -0,0 +1,37 @@
+<!DOCTYPE html>
+<html lang="en" xmlns:th="http://www.thymeleaf.org">
+<head>
+    <meta charset="UTF-8">
+    <title>添加角色</title>
+    <link rel="stylesheet" th:href="@{/webjars/bootstrap/5.3.2/css/bootstrap.css}">
+</head>
+<body class="container">
+<br>
+<h3>添加角色</h3>
+<br>
+
+<form th:action="@{/role/add}" method="post" class="form-horizontal">
+    <div class="form-group">
+        <label class="col-sm-2 control-label">名字:</label>
+        <div class="col-sm-5">
+            <input type="text" id="name" name="name" class="form-control">
+        </div>
+        <br>
+
+        <label class="col-sm-2 control-label">描述:</label>
+        <div class="col-sm-5">
+            <input type="text" id="desc" name="desc" class="form-control">
+        </div>
+    </div>
+
+    <br>
+    <div class="form-group">
+        <div class="col-sm-offset-2 col-sm-10">
+            <input type="submit" value="提交" class="btn btn-info">
+        </div>
+    </div>
+</form>
+
+
+</body>
+</html>

+ 46 - 0
springmvc-demo/src/main/webapp/WEB-INF/templates/demo/list.html

@@ -0,0 +1,46 @@
+<!DOCTYPE html>
+<html lang="en" xmlns:th="http://www.thymeleaf.org">
+<head>
+    <meta charset="UTF-8">
+    <title>角色列表</title>
+    <!-- 需要外部引入css  link关联标签  css是层叠样式表 -->
+    <link rel="stylesheet" th:href="@{/webjars/bootstrap/5.3.2/css/bootstrap.css}">
+</head>
+<body class="container">
+
+<h2>角色列表如下</h2>
+
+<!--
+     先引入命名空间  xmlns:th="http://www.thymeleaf.org"
+     分析需求  对于多条数据的显示  需要动态获取它的个数 然后拼接标签
+     数据按照list返回  List<Role>
+-->
+<table class="table table-hover">
+  <thead>
+  <tr>
+    <th>名字</th>
+    <th>描述</th>
+  </tr>
+  </thead>
+  <tbody>
+  <tr th:each="role:${roleList}">
+    <td th:text="${role.name}">name</td>
+    <td th:text="${role.desc}">desc</td>
+      <td class="col-sm-2">
+          <!-- http://localhost:8080/springmvc_demo/role/toUpdate?id=1 -->
+          <a th:href="@{/role/toUpdate(id=${role.id})}">编辑</a>
+      </td>
+      <td class="col-sm-2">
+          <!-- http://localhost:8080/springmvc_demo/role/delete?id=1 -->
+          <a th:href="@{/role/delete(id=${role.id})}">删除</a>
+      </td>
+  </tr>
+  </tbody>
+</table>
+
+<div>
+    <a th:href="@{/role/toAdd}">添加</a>
+</div>
+
+</body>
+</html>

+ 43 - 0
springmvc-demo/src/main/webapp/WEB-INF/templates/demo/update.html

@@ -0,0 +1,43 @@
+<!DOCTYPE html>
+<html lang="en" xmlns:th="http://www.thymeleaf.org">
+<head>
+    <meta charset="UTF-8">
+    <title>修改角色</title>
+    <link rel="stylesheet" th:href="@{/webjars/bootstrap/5.3.2/css/bootstrap.css}">
+</head>
+<body class="container">
+<br>
+<h3>修改角色</h3>
+<br>
+
+<!-- 添加和更新 往往有相同的页面效果 但区别在于 添加的数据都是空  更新的数据是回显-->
+<form th:action="@{/role/update}" th:object="${role}" method="post" class="form-horizontal">
+    <div class="form-group">
+        <label class="col-sm-2 control-label">名字:</label>
+        <div class="col-sm-5">
+            <input type="text" id="name" name="name" th:value="*{name}" class="form-control" readonly>
+            <!--            <input type="text" id="name" name="name" th:value="${role.name}" class="form-control">-->
+        </div>
+        <br>
+
+        <label class="col-sm-2 control-label">描述:</label>
+        <div class="col-sm-5">
+            <input type="text" id="desc" name="desc" th:value="*{desc}" class="form-control">
+        </div>
+
+        <div>
+            <input type="hidden" id="id" name="id" th:value="*{id}">
+        </div>
+    </div>
+
+    <br>
+    <div class="form-group">
+        <div class="col-sm-offset-2 col-sm-10">
+            <input type="submit" value="提交" class="btn btn-info">
+        </div>
+    </div>
+</form>
+
+
+</body>
+</html>

+ 4 - 0
springmvc-demo/src/main/webapp/WEB-INF/templates/hello.html

@@ -8,5 +8,9 @@
 Hello,
 <p th:text="${name}">name</p>
 <!--<p th:utext="${name}" >name</p>-->
+
+<br>
+<a th:href="@{/testList}">testList</a><br>
+<a th:href="@{/role/list}">roleList</a><br>
 </body>
 </html>