04点睛Spring4.1-资源调用

4.1 Resource

  • spring用来调用外部资源数据的方式
  • 支持调用文件或者是网址
  • 在系统中调用properties文件可参考<<02点睛Spring4.1-Java Config>>中结合@PropertySourceEnvironment来使用
  • 也可以使用@Value来注入资源,@Value的使用将在<<13点睛Spring4.1-Spring EL>>章节中有更详细的使用

4.2 示例

4.2.1 新增commons-io到maven依赖

需使用commons-io的IOUtils工具类将InputStream转换成String 在pom.xml的中添加如下

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.3</version>
</dependency>

4.2.2 新建测试用info.txt

sadfasdfasdfasdfasdfsad
sadfasdfasdfasdfasdfsad
sadfasdfasdfasdfasdfsad

4.2.3 测试

package com.wisely.resource;

import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.stereotype.Component;

@Component
public class Main {
    @Value("classpath:com/wisely/resource/info.txt")
    private Resource info;

    public static void main(String[] args) throws IOException {
        AnnotationConfigApplicationContext context =
                        new AnnotationConfigApplicationContext("com.wisely.resource");
        Main main = context.getBean(Main.class);
        System.out.println(main.injectInfo());
        System.out.println("----------------------------");

        //classpath: spring的一个模拟协议,类似于http:
        Resource file = context.getResource("classpath:com/wisely/resource/info.txt");
        System.out.println(IOUtils.toString(file.getInputStream()));
        System.out.println("----------------------------");

        Resource url = (UrlResource) context.getResource("http://www.baidu.com");
        System.out.println(IOUtils.toString(url.getInputStream()));
        context.close();
    }

    public String injectInfo() throws IOException{
        return IOUtils.toString(info.getInputStream());
    }

}

输出结果

sadfasdfasdfasdfasdfsad
sadfasdfasdfasdfasdfsad
sadfasdfasdfasdfasdfsad
----------------------------
sadfasdfasdfasdfasdfsad
sadfasdfasdfasdfasdfsad
sadfasdfasdfasdfasdfsad
----------------------------
<!DOCTYPE html><!--STATUS OK--><html><head>
.......

新书推荐《JavaEE开发的颠覆者: Spring Boot实战》,涵盖Spring 4.x、Spring MVC 4.x、Spring Boot企业开发实战。

 

京东地址:http://item.jd.com/11894632.html

当当地址:http://product.dangdang.com/23926195.html

亚马逊地址:http://www.amazon.cn/图书/dp/B01D5ZBFUK/ref=zg_bsnr_663834051_6 

淘宝地址:https://item.taobao.com/item.htm?id=528426235744&ns=1&abbucket=8#detail

 

 

 

或自己在京东、淘宝、亚马逊、当当、互动出版社搜索自选。

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想在 `el-form-item` 中添加一个“显示/隐藏密码”按钮,让用户可以轻松地切换密码输入框的可见性,可以使用 `el-input` 组件的 `show-password` 属性和 `append` 插槽。 具体的实现步骤如下: 1. 在 `el-form-item` 中使用 `el-input` 组件,并将其 `type` 属性设置为密码类型,同时将其 `v-model` 绑定到你的数据模型中。 ```html <el-form-item label="密码"> <el-input type="password" v-model="password"></el-input> </el-form-item> ``` 2. 在 `el-input` 组件中添加 `show-password` 属性,以启用“显示/隐藏密码”按钮。 ```html <el-form-item label="密码"> <el-input type="password" v-model="password" show-password></el-input> </el-form-item> ``` 3. 在 `el-input` 组件中使用 `append` 插槽创建一个按钮,当点击该按钮时,可以切换密码输入框的可见性。 ```html <el-form-item label="密码"> <el-input type="password" v-model="password" show-password> <template v-slot:append> <i class="el-icon-view" v-show="!showPassword" @click="showPassword = true"></i> <i class="el-icon-view-off" v-show="showPassword" @click="showPassword = false"></i> </template> </el-input> </el-form-item> ``` 4. 在你的组件中定义一个 `showPassword` 变量,并在点击按钮时切换它的值。 ```javascript data() { return { password: '', showPassword: false } } ``` 这样,当用户点击“显示密码”按钮时,密码输入框将变为文本输入框,用户可以轻松地查看密码。而当用户再次点击该按钮时,密码输入框将再次变为密码输入框。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值