Spirngboot web开发

SpringBoot Web开发

静态资源
  • 默认:静态资源可以放在以下位置,springboot识别优先级从高到低

    • resources/resources下,resources/static下,resources/public下,resources下,wejars/下
  • 自定义:配置文件中配置静态资源路径:spring.mvc.static-path-pattern=/xxx...

模板引擎

templates下的页面只能通过controller或servlet跳转,需要模板引擎的支持。freemark,thymeleaf

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
    <version>2.5.3</version>
</dependency>

导入依赖后,html放入templates下即可

thymeleaf语法
  • 遍历

    <h1 th:each="user:${users}" th:text="${user}"></h1>
    
Druid连接池
  • 项目配置文件配置连接池:spring.dataSource.type: com.alibaba.druid.pool.DruidDataSource即可

  • 数据源配置文件扩展功能,可以配置后台监控,过滤器。log4j等

    @Configuration
    public class DruidConfig {
    
        @Bean
        @ConfigurationProperties(prefix = "spring.datasource")
        public DataSource druidDataSource() {
            return new DruidDataSource();
        }
    
        //后台监控,Druid特有,访问路径:localhost:8080/druid
        @Bean
        public ServletRegistrationBean statViewServlet() {
            ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/**");
            //设置初始化参数
            Map<String, String> initParams = new HashMap<>();
    
            //用户名和密码字段名是固定的
            initParams.put("loginUsername", "admin");
            initParams.put("loginPassword", "123456");
    
            //允许睡可以访问
            initParams.put("allow", "");
            //禁止谁访问
            initParams.put("用户名", "192.168.0.66");
    
    
            bean.setInitParameters(initParams);
            return bean;
        }
    }
    
posted @   jpy  阅读(4)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
点击右上角即可分享
微信分享提示