我正在嘗試使用 Springboot 實作一個 Web 應用程式。但是當我請求方法時,我得到 404 錯誤。Springboot 找不到 Jsp 檔案。
這是我的控制器代碼:
@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");
}
這是我的安全配置:
@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();
}
}
我還在 application.properties 中指定了前綴和后綴:
spring.mvc.view.prefix=/static/
spring.mvc.view.suffix=.jsp
我的 pom 檔案中也有這些依賴項:
<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>
這是我的專案結構:
誰能告訴我有什么問題?
uj5u.com熱心網友回復:
SpringBoot的主要模板引擎有 Thymeleaf、Groovy、FreeMarker、Jade。在參考指南中:
如果可能,應避免使用 JSP,將它們與嵌入式 servlet 容器一起使用時有幾個已知的限制。
由于 Tomcat 中的硬編碼檔案模式,可執行 jar 將無法作業。
如果 JSP 是您無法轉換的遺留代碼或專有代碼,您必須做一些事情來開發/維護、編譯和運行在 Intellij 中運行它們的 SpringBootApplication:
maven:你的 pom 必須是一個' war '包。這將使 intellij 正確查找和編譯 JSP。
web方面:將您的.jsp檔案放在一個檔案夾中,它們應該在webapp中:src/main/webapp/WEB-INF/jsp/ jsp永遠不會在靜態中“編譯”/“解釋”。
spring facet:在application.properties中將前綴設定為 /WEB-INF/jsp/
tomcat:有那些依賴:
<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>
- 構建可運行的戰爭:制作一個運行的 Intellij“maven 配置”:
干凈安裝 -f pom.xml
- 運行那場戰爭:使用這些設定進行 Intellij“jar 配置”:
- path to jar : <目標檔案夾中war檔案的路徑>
- before launch : run the "maven configuration" you created
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/435201.html
