How to show values from property file in JSP in a spring MVC app

后端 未结 5 824
孤街浪徒
孤街浪徒 2020-12-04 20:14

I\'m setting my properties in app-servlet.xml with a bean like this:

    

        
5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-04 20:59

    in Spring version 4, you find the property file :

    1) xml mode

                    
                       
                          
                              
                                
                                 classpath:mongodb.remote.properties  
                                    
                                  /WEB-INF/mongodb.remote.properties  
                              
                          
                      
    ----------------------------------------------------------------------------------------
    

    2) programmatic mode:

        If you have for example this package : com.profile.config, com.profile.controller, ecc.. 
        it's not problem if you put only com.profile, it's ok !!! Now
    
    
        @Configuration
        @ComponentScan(basePackages = "com.profile")
        /** resources folder & default package maven project*/
        @PropertySource(value = { "classpath:mongodb.remote.properties" }) 
        public class MyPropertySourcesPlaceholderConfigurer {
    
    
            @Bean
            public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
                return new PropertySourcesPlaceholderConfigurer();
            }
        }
    
    
        ---------------------------------------------------------------------------------
        Your property file
    
        label.test.val=this is the property file value!!!!!
        ---------------------------------------------------------------------------------
    
        @Controller
        public class LabelsAndValuesController {
    
    
             @Value("${label.test.val}")
             String test;
    
        }
    

    output:

        ---------------------------------------------------------------------------------
        this is the property file value!!!!!
        ---------------------------------------------------------------------------------
    

提交回复
热议问题