配置文件路径:

配置内容:

方法一:

Action内被调用的函数添加下段代码:

Properties props = new Properties();
props.load(UploadFileAction.class.getClassLoader().getResourceAsStream("/struts/struts-config.properties"));
System.out.println(props.getProperty("destPath"));

执行效果:

为了便于其他Action重用,可以将此部分写成一个工具类:

package com.struts2demo.demo.util;

import com.opensymphony.xwork2.Action;

import java.io.IOException;
import java.util.Properties;

public class StrutsConfig {

    public Properties GetProperties(Class actionClass) throws IOException {
        Properties props = new Properties();
        props.load(actionClass.getClassLoader().getResourceAsStream("/struts/struts-config.properties"));
        return props;
    }

}

然后Action里如此调用即可:

Properties props = new StrutsConfig().GetProperties(UploadFileAction.class);
System.out.println(props.getProperty("destPath"));

方法二:@ConfigurationProperties + @PropertySource

package com.example.spdemo.controller;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@ConfigurationProperties
@PropertySource("/struts/struts-config.properties")
public class Index {

    private String destPath;

    @RequestMapping("/")
    public String getProps() {
        return destPath;
    }

    public String getDestPath() {
        return destPath;
    }

    public void setDestPath(String destPath) {
        this.destPath = destPath;
    }

}

对于非application.properties的配置,需@PropertySource指明配置文件的路径;否则默认是从application.properties中读取配置,不需要写该注解。

方法三:spring的xml配置中配置上properties,然后通过@Value注入(该方法在Spring MVC中验证过,但是不知道集成struts2后是否受影响)

spring-context.xml

<util:properties id="APP_PROP" local-override="true" location="classpath:owlforest.properties" />

owlforest.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/linfu_test_db?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

代码样例:

package com.owlforest.www.config.dao;

import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
public class DataSourceConfiguration {
    @Value("#{APP_PROP['jdbc.driver']}")
    private String driver;

    @Value("#{APP_PROP['jdbc.url']}")
    private String url;

    @Value("#{APP_PROP['jdbc.username']}")
    private String username;

    @Value("#{APP_PROP['jdbc.password']}")
    private String password;

    public DruidDataSource createDataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setDefaultAutoCommit(true);
        return dataSource;
    }
}

 方法四:使用spring的PropertiesFactoryBean

    @Bean
    public PropertiesFactoryBean getProperties() {
        PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
        propertiesFactoryBean.setLocations(new ClassPathResource("application.properties"));
        return propertiesFactoryBean;
    }

调用:

    @Autowired
    private PropertiesFactoryBean propertiesFactoryBean;

    public String getProperties(String key) throws IOException {
        Properties properties = propertiesFactoryBean.getObject();
        return properties.getProperty(key);
    }

 方法五:不使用自动装配,使用classLoader获取Properties

MailUtil.class.getClassLoader().getResourceAsStream("/config.properties")

Struts2学习:Action获取properties文件的值的更多相关文章

  1. Java学习-021-Properties 获取配置项对应的值

    在日常的脚本编写过程中,通常会获取配置文件中的配置项,以执行相应的业务逻辑. 小二上码...若有不足之处,敬请大神指正,不胜感激! 获取配置项值的源码如下所示: /** * Get value fro ...

  2. 【Properties】获取Properties文件

    获取Properties文件 package com.chinamobile.epic.tako.v2.query.commons; import org.springframework.core.i ...

  3. 获取properties文件的内容

    获取properties文件的内容 public void test() throws Exception{ String resource = "application.propertie ...

  4. spring Controller 层注解获取 properties 里面的值

    前言:最近在做一个项目,想要在 controller 层直接通过注解 @Value("")来获取 properties 里面配置的属性. 这个其实和 springmvc.sprin ...

  5. struts2,action上传文件

    通过servlet实现文件上传,可以用用servlet接受到request的值的话:主要是这句话 List<?> items = upload.parseRequest(request); ...

  6. Spring获取properties文件中的属性

    1.前言 本文主要是对这两篇blog的整理,感谢作者的分享 Spring使用程序方式读取properties文件 Spring通过@Value注解注入属性的几种方式 2.配置文件 applicatio ...

  7. 获取.properties配置文件属性值

    public class TestProperties { /** * * @Title: printAllProperty * @Description: 输出所有配置信息 * @param pro ...

  8. spring通过静态方法获得properties文件的值

    获得spring bean方法 @Component public class BeanUtils implements ApplicationContextAware { private stati ...

  9. struts2学习笔记之十:文件上传

    Struts2的上传 1.Struts2默认采用了apache commons-fileupload 2.Struts2支持三种类型的上传组件 3.需要引入commons-fileupload相关依赖 ...

随机推荐

  1. SPA项目中,404页面 和 登陆页面 对应的路由,应该怎样控制?

    SPA项目中,404页面 和 登陆页面 对应的路由,应该怎样控制? 可以这样做: 登陆之前,所有页面跳到 登陆页面:包括随便输入的路由地址. 登陆后,跳到相应页面:随便输入的.不存在的路由地址,才跳到 ...

  2. 利用pipeline批量插入数据到redis

    在推荐系统中,推荐候选集格式一般是,itemid itemid_list.要把itemid作为key,推荐列表作为value批量插入到redis. 比如文件cf.data为: cf_763500210 ...

  3. vs2013 使用vs2017的localdb

    应用vs203进行MVC开发时,进行数据库初始化的时候,默认使用电脑中高版本的localdb(v12),在修改web.config中的链接串时报错,也无法使能数据库迁移, 解决方法:在数据库初始化之前 ...

  4. spring-AOP框架(基于AspectJ注解配置AOP)

    基于AspectJ注解配置AOP 1.加入jar包: 要在Spring应用中使用AspectJ注解,必须在classpath下包含AspectJ类库:aopalliance.jar.aspectj.w ...

  5. [转]Scrapy简单入门及实例讲解

    Scrapy简单入门及实例讲解 中文文档:   http://scrapy-chs.readthedocs.io/zh_CN/0.24/ Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用 ...

  6. excel技巧--单元格合并与拆分

    如果要将上图的地区列做成下图的合并单一列: 有如下做法: (以下图表格为例) 1.选择要排序的表格,点击“开始”-->排序和筛选-->自定义排序.在对话框选择“业务项目”进行排序: 2.选 ...

  7. Delphi调用DLL中的接口

    问题描述: 具体问题就是在隐式使用接口变量后,在FreeLibrary执行后,就会出现一个非法访址的错误. 这个错误的原因就是在FreeLibrary后,DLL以的代码均为不可用状态,而在代码执行完整 ...

  8. nodejs在后台运行

    安装forever npm install forever -g 使用 forever 启动 app forever start app.js 启动app并输出日志 forever start -l ...

  9. sql 语句收集

    SELECT type as 'plan cache store', buckets_count  FROM sys.dm_os_memory_cache_hash_tables WHERE type ...

  10. response.sendRedirect传递参数和转向

    response.sendRedirect是通过浏览器来做转向的. 假设在A.jsp页面设置request.setAttribute("username","admin& ...