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:
can anyone tell me what is the problem?
CodePudding user response:
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:
maven: your pom must be a 'war' package. That will makes intellij look for and compile the JSPs right.
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.
spring facet: set the prefix to /WEB-INF/jsp/ in your application.properties
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>
- build runnable war: make an Intellij "maven configuration" that run :
clean install -f pom.xml
- 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