0

When I am hitting the URL it is downloading the JSP page but when I am using HTML page, it gets rendered in the browser. || HTML 1 - 0 JSP ||

Project structure

enter image description here

Application properties:

spring.mvc.view.prefix=/views/
spring.mvc.view.suffix=.html

Controller

enter image description here

It worked when I have added tomcat-embed-jasper dependency. Why does that happened?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Ashutosh Sharma
  • 118
  • 2
  • 18
  • As JSP will get compiled to Servlet first and to run the servlet, we need a servlet enabled container. i.e. we need a container which can process the servlets and Tomcat is one of them. You can remove the embed tomcat and can run outside tomcat by building a war too.... – DEBENDRA DHINDA May 03 '19 at 11:14

5 Answers5

1

- It worked when I have added tomcat-embed-jasper dependency. Why does that happened?

Because it needs a webapp container.

  • As JSP will get compiled to Servlet first and to run the servlet, we need a servlet enabled container. i.e. we need a container which can process the servlets and Tomcat is one of them. You can remove the embed tomcat and can run outside tomcat by building a war too.... – DEBENDRA DHINDA May 03 '19 at 11:15
1

I have came across with the same issue, file download every-time when i hit the Page. So, I have makes some changes & error has resolved.

  1. Adding "tomcat-embed-jasper" in pom.xml file

  2. having same version of your tomcat

    org.apache.tomcat.embed tomcat-embed-jasper 9.0.68

enter image description here

Jitendra
  • 31
  • 1
  • 4
0

Embedded tomcat consider jsp rendering as optional. That's why it is needed.Also As I can understand. You must have provided scope of tomcat-embed-jasper dependency as provided.

Sritam Jagadev
  • 955
  • 4
  • 18
  • 46
0

You can do as below :

DemoController.java :

@Controller
public class DemoController {    
    @GetMapping("/")
    public String getLoginView(){
        return "login";  
    }       
    @GetMapping("/welcoome")
    public String welcome(){
        return "login";   
    }
}

ApplicationConfigurerAdapter.java

@Configuration
@EnableWebMvc
public class ApplicationConfigurerAdapter extends WebMvcConfigurerAdapter{

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

    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
}

DemoApplication.java

@Configuration
@EnableAutoConfiguration
@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer{

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

application.properties

server.contextPath=/
server.servlet.contextPath=/

server.port=6565

pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>   
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

You can build it as mvn package and can also run outside container(such as Tomcat)

Project structure Output

DEBENDRA DHINDA
  • 1,163
  • 5
  • 14
0

Add this tomcat dependancy to your pom file:

<dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-jasper</artifactId>
</dependency>
Jdslk
  • 97
  • 3
  • 8