1

I use Spring Boot 1.5.3.RELEASE. This is class MvcConfiguration

package hello;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.util.Locale;

@Configuration
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter {

    private static final Logger logger = LoggerFactory.getLogger(MvcConfiguration.class);

    @Bean
    public ViewResolver getViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");
        logger.info("Register ViewResolver success.");
        return resolver;
    }

    @Override
    public void configureDefaultServletHandling(
            DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver slr = new SessionLocaleResolver();
        slr.setDefaultLocale(Locale.US);
        return slr;
    }

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
        lci.setParamName("locale");
        return lci;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localeChangeInterceptor());
        super.addInterceptors(registry);
    }

}

Controller:

package hello;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(path = "/")
public class HomeController {

    @RequestMapping(method = RequestMethod.GET)
    public String home(){
        return "currency";
    }

}

When debug app, I see log

2017-05-02 07:41:48.970  INFO 3333 --- [           main] hello.MvcConfiguration                   : Register ViewResolver success.

but webapp return plain JSP text: enter image description here

I added necessary dependencies at build.gradle. I prefer run web app on standalone Tomcat. How to fix it?

Vy Do
  • 46,709
  • 59
  • 215
  • 313
  • did you referred the sample projects shared on [Github](https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples) – Rajith Pemabandu May 02 '17 at 00:50
  • This is source code: https://github.com/donhuvy/temp_erp/blob/master/src/main/java/hello/MvcConfiguration.java#L27 – Vy Do May 02 '17 at 00:55
  • Possible duplicate of [JSP file not rendering in Spring Boot web application](http://stackoverflow.com/questions/20602010/jsp-file-not-rendering-in-spring-boot-web-application) – no id May 02 '17 at 01:05
  • I have been add necessary dependencies: https://github.com/donhuvy/temp_erp/blob/master/build.gradle#L33 – Vy Do May 02 '17 at 01:11
  • Have you tried to take a look here: https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-tomcat-jsp – Moshe Arad May 02 '17 at 02:34
  • I also tried it. – Vy Do May 02 '17 at 03:46
  • @DoNhuVy If you want to run with embedded tomcat then you need to include tomcat-embed-jasper dependency. If you want to run in standalone tomcat then jstl compile time dependency is needed. I have tested your code by deploying in tomcat 8. Please check my answer below. – abaghel May 02 '17 at 04:54

2 Answers2

1

You need to add dependency for tomcat-embed-jasper. Dependencies in your build.gradle file should be like below.

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    // JPA Data (We are going to use Repositories, Entities, Hibernate, etc...)
    compile 'org.springframework.boot:spring-boot-starter-data-jpa'
    compile 'mysql:mysql-connector-java'
    compile 'javax.servlet:jstl:1.2'
    providedRuntime 'org.apache.tomcat.embed:tomcat-embed-jasper'
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

Now you can run your Spring Boot application like below.

gradle bootRun

If you want to deploy in standalone tomcat then don't add tomcat-embed-jasper dependency. You would need jstl jar in your war file.

gradle build
abaghel
  • 14,783
  • 2
  • 50
  • 66
1

According to Spring Boot Documentation:

JSPs should be avoided if possible, there are several known limitations when using them with embedded servlet containers.

here is the link:

http://docs.spring.io/spring-boot/docs/1.5.3.RELEASE/reference/htmlsingle/#boot-features-spring-mvc-template-engines

Moshe Arad
  • 3,587
  • 4
  • 18
  • 33