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 @ 2021-08-23 16:40  jpy  阅读(15)  评论(0)    收藏  举报