Mapleyang

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
统计
 

相比于springmvc-web开发要解决的问题:

  • 导入静态资源

  • 指定首页

  • jsp。模版引擎Thymeleaf

  • 装配扩展SpringMVC

  • 增删改查

  • 截器和国际化

 

静态资源

idea按两下shift,搜索webautoConfiguration - WebMvcAutoConfigurationAdapter - addResourceHandlers

复制代码
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    if (!this.resourceProperties.isAddMappings()) {
        logger.debug("Default resource handling disabled");
        return;
    }
    Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
    CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
    // 第一种方式 webjars
    if (!registry.hasMappingForPattern("/webjars/**")) {
        customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
                                             .addResourceLocations("classpath:/META-INF/resources/webjars/")
                                             .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
    }
    // 第二种方式
    String staticPathPattern = this.mvcProperties.getStaticPathPattern();
    if (!registry.hasMappingForPattern(staticPathPattern)) {
        customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)
                                             .addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
                                             .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
    }
}
复制代码

 

  • 第一个if:没有

  • 第二个if:webjars不推荐使用,官网导包,url:META-INF/resources/webjars/

  • 第三个if:优先级:resources > static(默认) > public

    1. classPath :/**

    2. classpath:/META-INF/resources/

    3. classpath:/resources/

    4. classpath:/static/(默认创建)

    5. classpath:/public/

 

在pom.xml中导入jquery    https://www.webjars.org/

<dependency>
       <groupId>org.webjars.npm</groupId>
       <artifactId>jquery</artifactId>
       <version>3.6.0</version>
</dependency>

 

 

Thymeleaf

  • 注意:template/**下的任何页面都需要Controller来跳转才能访问,不能直接访问
复制代码
<!--thymeleaf模版引擎在,都是基于3.x版本开发-->
<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
复制代码

 

第一次使用,传msg,接受:th:text="${msg}",(thymeleaf独有的语法)

复制代码
//在templates目录下的所有页面,只能通过Controller来跳转
//这个需要模板引擎的支持
@Controller
public class IndexController {
    @RequestMapping("/text")
    public String index(Model model){
        model.addAttribute("msg","<h1>烟雨如花</h1>");
        //Arrays.asList可以把一个数组转化成一个集合
        model.addAttribute("users", Arrays.asList("烟雨如花","赤色夫人"));
        return "text";
    }
}
复制代码

 

复制代码
<!DOCTYPE html>
<!--thymeleaf约束-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>thymeleaf</title>
</head>
<body>
<!--所有的html元素都可以被Th接管:th:xx元素名-->
<!--test=默认是转义文本-->
<h1 th:text="${msg}"></h1>
<!--utest=默认是不转义文本-->
<h1 th:utext="${msg}"></h1>
<h1>
    <!--遍历往前写item-->
    遍历一 推荐这么使用:
    <h2 th:each="user:${users}" th:text="${user}"></h2><br>
    遍历二:
    <h2 th:each="user:${users}" >[[${user}]]</h2>
</h1>
</body>
</html>
复制代码

 

简单的表达式:

  Varlable Expressions:(变量表达式)${…}
  Selection Varlable Expressions选择变量表达式:*{…}
  Message Expressions消息表达式:# {...}
  Link URL Expressions链接URL表达式:@{...}
  Fragment Expressions分段表达式:~{...}

简单的表达式:
    Varlable Expressions:(变量表达式)${…}
    Selection Varlable Expressions选择变量表达式:*{…}
    Message Expressions消息表达式:# {...}
    Link URL Expressions链接URL表达式:@{...}
    Fragment Expressions分段表达式:~{...}

 

posted on   折木~  阅读(27)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
 
点击右上角即可分享
微信分享提示