'SpringBoot Web MVC Application cannot resolve JSP views

I am trying to implement a web application using Springboot. but when I request methods I get 404 Error. Springboot cannot find Jsp files.

this is my Controller Code:

@PostMapping(value = "/loginSuccess")
public ModelAndView loginSuccess() {
    System.out.println("in login success");
    return new ModelAndView("index");
}

@GetMapping(value = "/loginError")
public ModelAndView showLoginError() {
    System.out.println("in login error");
    return new ModelAndView("error");
}

and this is my SecurityConfig:

@ComponentScan
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private EmployeeRepository employeeRepository;

    @Bean
    public PasswordEncoder passwordEncoder(){
        return  new BCryptPasswordEncoder();
    }

    @Bean
    public UserDetailsService userDetailsService(){
        return new EmployeeDetailService(employeeRepository, passwordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .formLogin()
                .successForwardUrl("/loginSuccess")
                .failureUrl("/loginError")
                .permitAll()
                .and()
                .authorizeRequests()
                .antMatchers("/").permitAll()
                .and()
                .httpBasic();
    }
}

I also specified prefix and suffix in application.properties:

spring.mvc.view.prefix=/static/
spring.mvc.view.suffix=.jsp

I also have these dependencies in my pom file:

 <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
 </dependency>
 <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
 </dependency>

and this is my Project Structure:

enter image description here can anyone tell me what is the problem?



Solution 1:[1]

The main template engines for SpringBoot are Thymeleaf, Groovy, FreeMarker, Jade. In the reference guide:

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

An executable jar will not work because of a hard coded file pattern in Tomcat.

If the JSPs are legacy or proprietary codes that you can't convert, you have to do a few things in order to develop/maintain, compile and kinda run a SpringBootApplication running them in Intellij:

  1. maven: your pom must be a 'war' package. That will makes intellij look for and compile the JSPs right.

  2. web facet: put your .jsp files in a folder where they are expected to be in a webapp: src/main/webapp/WEB-INF/jsp/ The jsp will never be 'compiled'/'interpretable' in static.

  3. spring facet: set the prefix to /WEB-INF/jsp/ in your application.properties

  4. tomcat: have those dependencies:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-tomcat</artifactId>
   <scope>provided</scope>
</dependency>
<dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-jasper</artifactId>
   <scope>provided</scope>
</dependency>
<dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
</dependency>
  1. build runnable war: make an Intellij "maven configuration" that run :

clean install -f pom.xml

  1. run that war: make an Intellij "jar configuration" with those settings:
  • path to jar : <the path to the war file in the target folder>
  • before launch : run the "maven configuration" you created

Solution 2:[2]

i had a similar problem and for me specifying the web resource directory containing the JSP files resolved the issue.

  1. open project structure and under modules -> web -> web resource directory specify the directory in which your view are contained (in this case webapp)
  2. open application.properties
  3. specify the folder within your webresource directory which contains your views as prefix (in this case /views/)
  4. specify your view suffix .jsp

this should enable intelij to resolve the views returned by the controller.

enter image description here enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cybergrenouille
Solution 2 HenriDev