Sunday, May 21, 2017

Spring MVC - Read Properties file

1. I've a Maven project.

2. I've created a properties file i.e. message.properties under resources folder.

3. My project structure snapshot is as follows:



4. Contents of message.properties:

index.page=home
salutation.page=welcome
salutation.text=Welcome to Spring MVC framework
name=MY NAME

5. base-package entry of servlet-context.xml is as follows: [All config files are here]

<context:component-scan base-package="com.controller" />

6. I'll use @PropertySource and @Value annotation.

7. I'll create a class with @PropertySource annotaion and a bean returning PropertySourcesPlaceholderConfigurer. This step is mandatory to retrieve values from properties file using @Value annotation.

package com.controller.utility;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

@Configuration
@PropertySource("classpath:message.properties")
public class MessageUtil {
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}

8. Now values are available using @Value annotation from any class.

    @Value("${index.page}")
    public String INDEX;
  
    @Value("${salutation.page}")
    public String SALUTATION;
  
    @Value("${salutation.text}")
    public String SALUTATION_TEXT;
  
    @Value("${name}")
    public String name;

9. I've used these values in a controller class for request mapping.

    package com.controller.mapping;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@Controller
public class HomeController {

    @Value("${index.page}")
    public String INDEX;
  
    @Value("${salutation.page}")
    public String SALUTATION;
  
    @Value("${salutation.text}")
    public String SALUTATION_TEXT;
  
    @Value("${name}")
    public String name;
  

  
    @RequestMapping(value={"/welcome", "/index"}, method={RequestMethod.GET, RequestMethod.POST})  
    public String salutation(Model model) {
        model.addAttribute("text", SALUTATION_TEXT);
        model.addAttribute("name", name);
      
        return SALUTATION;
    }
}

Thursday, May 11, 2017

Spring MVC Config - Put 3 files in WEB-INF folder

1. root-context.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
   
    <!-- Root Context: defines shared resources visible to all other web components -->
       
</beans> 



2. servlet-context.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
  
    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>
  
    <context:component-scan base-package="com.quynguyenblog.springmvc.controller" />
  
</beans:beans>



3. web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/root-context.xml</param-value>
    </context-param>
  
    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
      
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>