在Springmvc中获取properties属性

一些关键的属性一般都会拿出来作为配置,比如数据库连接等。在springmvc中也提供了获取property的类,比如@Value来获取。我接触spring很浅,基本上都是百度的问题解决方法,百度到@value的用法,按照说明尝试了两次都失败了。正巧身边又有合适的方法,于是便没有去深入研究为什么失败,这个留在以后研究。下面就是获取代码:

源码来自:https://github.com/thinkgem/jeesite

  1 package com.demo.common.utils;
  2 
  3 import org.apache.commons.io.IOUtils;
  4 import org.slf4j.Logger;
  5 import org.slf4j.LoggerFactory;
  6 import org.springframework.core.io.DefaultResourceLoader;
  7 import org.springframework.core.io.Resource;
  8 import org.springframework.core.io.ResourceLoader;
  9 
 10 import java.io.IOException;
 11 import java.io.InputStream;
 12 import java.util.NoSuchElementException;
 13 import java.util.Properties;
 14 
 15 /**
 16  * Properties文件载入工具类. 可载入多个properties文件, 相同的属性在最后载入的文件中的值将会覆盖之前的值,但以System的Property优先.
 17  * Created by Administrator on 2016/2/23.
 18  */
 19 public class PropertiesLoader {
 20     private static Logger logger = LoggerFactory.getLogger(PropertiesLoader.class);
 21     private static ResourceLoader resourceLoader = new DefaultResourceLoader();
 22     private final Properties properties;
 23 
 24     public PropertiesLoader(String... resourcesRaths) {
 25         properties = loadProperties(resourcesRaths);
 26     }
 27 
 28     public Properties getProperties(){
 29         return properties;
 30     }
 31 
 32     /**
 33      * 取出property,但以System的property优先,取不到返回空字符串
 34      */
 35     private String getValue(String key){
 36         String systemProperty = System.getProperty(key);
 37         if (systemProperty!=null){
 38             return systemProperty;
 39         }
 40         if (properties.containsKey(key)){
 41             return properties.getProperty(key);
 42         }
 43         return "";
 44     }
 45 
 46     /**
 47      * 取出String类型的Property,System的优先
 48      * @throws NoSuchElementException
 49      */
 50     public String getProperty(String key){
 51         String value = getValue(key);
 52         if (value==null){
 53             throw new NoSuchElementException();
 54         }
 55         return value;
 56     }
 57 
 58     /**
 59      * 取出String类型的Property,System的优先,null则返回默认值
 60      */
 61     public String getProperty(String key,String defaultValue){
 62         String value = getValue(key);
 63         return value!=null?value:defaultValue;
 64     }
 65 
 66     /**
 67      * 取出Integer类型的Property,System优先
 68      * @throws NoSuchElementException
 69      */
 70     public Integer getInteger(String key){
 71         String value = getValue(key);
 72         if (value==null){
 73             throw new NoSuchElementException();
 74         }
 75         return Integer.valueOf(value);
 76     }
 77         /**
 78      * 取出Integer类型的Property,System优先,null则返回默认值
 79      */
 80     public Integer getInteger(String key,Integer defaultValue){
 81         String value = getValue(key);
 82 
 83         return value!=null?Integer.valueOf(value):defaultValue;
 84     }
 85 
 86     /**
 87      * 取出Double类型的Property,但以System的Property优先.如果都为Null或内容错误则抛出异常.
 88      */
 89     public Double getDouble(String key) {
 90         String value = getValue(key);
 91         if (value == null) {
 92             throw new NoSuchElementException();
 93         }
 94         return Double.valueOf(value);
 95     }
 96 
 97     /**
 98      * 取出Double类型的Property,但以System的Property优先.如果都为Null则返回Default值,如果内容错误则抛出异常
 99      */
100     public Double getDouble(String key, Integer defaultValue) {
101         String value = getValue(key);
102         return value != null ? Double.valueOf(value) : defaultValue;
103     }
104 
105     /**
106      * 取出Boolean类型的Property,但以System的Property优先.如果都为Null抛出异常,如果内容不是true/false则返回false.
107      */
108     public Boolean getBoolean(String key) {
109         String value = getValue(key);
110         if (value == null) {
111             throw new NoSuchElementException();
112         }
113         return Boolean.valueOf(value);
114     }
115 
116     /**
117      * 取出Boolean类型的Property,但以System的Property优先.如果都为Null则返回Default值,如果内容不为true/false则返回false.
118      */
119     public Boolean getBoolean(String key, boolean defaultValue) {
120         String value = getValue(key);
121         return value != null ? Boolean.valueOf(value) : defaultValue;
122     }
123 
124 
125 
126     /**
127      * 载入多个文件,文件路径使用spring resource格式
128      * @param resourcesRaths
129      * @return
130      */
131     private Properties loadProperties(String[] resourcesRaths) {
132         Properties props = new Properties();
133 
134         for (String location : resourcesRaths) {
135             logger.debug("Loading properties file from:" + location);
136             InputStream is = null;
137             try {
138                 Resource resource = resourceLoader.getResource(location);
139                 is = resource.getInputStream();
140                 props.load(is);
141             } catch (IOException e) {
142                 logger.info("Could not load properties from path:{},{}",location,e.getMessage());
143                 e.printStackTrace();
144             }finally {
145                 IOUtils.closeQuietly(is);
146             }
147         }
148         return props;
149     }
150 }

 





唯有不断学习方能改变! -- Ryan Miao
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring MVC,你可以使用@RequestParam注解来获取表单数据。同时,如果你使用Thymeleaf作为模板引擎,你可以在表单使用Thymeleaf的语法来绑定表单数据。 首先,确保你已经在你的Spring MVC配置配置了Thymeleaf视图解析器。例如,在你的`application.properties`或`application.yml`添加以下配置: ``` spring.thymeleaf.enabled=true spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html ``` 接下来,在你的Controller,使用@RequestParam注解来获取表单数据。例如,假设你有一个表单提交了一个名为"name"的字段,你可以这样获取它的: ```java @PostMapping("/submit") public String handleSubmit(@RequestParam("name") String name) { // 处理表单数据 return "redirect:/success"; } ``` 在上面的例子,`@RequestParam("name")`表示获取名为"name"的表单字段的,并将其赋给`name`变量。 然后,在你的Thymeleaf模板,你可以使用Thymeleaf的语法来绑定表单数据。例如,如果你想在表单显示之前提交的名字,可以使用以下代码: ```html <form th:action="@{/submit}" method="post"> <input type="text" name="name" th:value="${name}" /> <button type="submit">Submit</button> </form> ``` 在上面的例子,`${name}`表示从Controller传递过来的名为"name"的属性。 这样,你就可以在Spring MVC获取和使用Thymeleaf表单数据了。希望对你有所帮助!如果还有其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值