0

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?

4
  • Are your jsp files inside static folder only or /static/jsp Commented Jan 29, 2022 at 11:38
  • they're inside static folder only Commented Jan 29, 2022 at 12:30
  • Can you share your project structure from your IDE as a picture and state which version of the IDE you are using Commented Jan 29, 2022 at 17:26
  • I added a picture of my project Structure. I am using IntelliJ 2021.3 Commented Jan 30, 2022 at 5:17

3 Answers 3

4

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

I have ultimate version of intellij, it was throwing me error saying cannot resolve jsp. Showing me warning. I created the module, specified the path. And it was working. Click on the + icon of web resource directory, and mention the path of your jsp file. It will work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.