|一、属性文件读取
1、首先创建属性文件res.properties
2、在容器配置xml文件加载文件
//解析属性配置文件
<context:property-placeholder location="classpath:res.properties"/>
到这,框架已搭建完成
3、开始读取文件
使用@Value(" ")
//口诀:先声明、再注入,后调用
@Value("${updatePath}")
private String path;
System.out.println(path);
二、面向对象开发(微信小程序、短信对象…)
1、创建一个阿里云类
(3、el表达式,定义数据在res.properties文件中)
2、调用
//口诀:先声明、再注入,后调用
@AutoParams
private AliyunConfig aliyunConfig; System.out.println(aliyunConfig.getAccessToken());
二、静态资源映射
当一个项目中有许多css样式、img和js脚本时,此时spring会拦截,所以需要用静态映射,例如:
1、在某个jsp页面,导入html的img图片
2、报错spring拦截所有资源,因为
解决方案一:“.do”(使用这个时,controller中的方法必须以"save.do",才能访问)
第二种:追加servlet-mapping
//所有以css文件结尾的
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
//开头以img图片,*所有的文件
<servlet-mapping>
<servlet-name>default</servlet-name>//dafault在容器的xml中
<url-pattern>/img/*</url-pattern>//以img开头,*代表所有的文件
</servlet-mapping>
3、容器xml中引入组件
<!--文件处理 -->
<mvc:default-servlet-handler/>
三、访问指定路径的文件——很重要
1、相对路径在WEB-INF下访问路径
在容器xml中配置(当有二级目录时,加上二级目录的名称):
1、一层目录url地址
2、多层目录的url地址
//静态资源——location:本地;mapping:映射
//一颗*只能在当前目录寻找,两颗*代表无限递归
**<mvc:resources location="/WEB-INF/img/" mapping="/img/**"></mvc:resources> **
2、指定路径
//一颗*只能在当前目录寻找,两颗*代表无限递归
**<mvc:resources location="file:F:\hello\" mapping="/hello/**"></mvc:resources>**