【问题标题】:SpringBoot Web MVC Application cannot resolve JSP viewsSpring Boot Web MVC 应用程序无法解析 JSP 视图
【发布时间】:2022-01-29 09:45:08
【问题描述】:

我正在尝试使用 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>

这是我的项目结构:

谁能告诉我是什么问题?

【问题讨论】:

  • 您的 jsp 文件是仅在静态文件夹中还是 /static/jsp
  • 它们只在静态文件夹中
  • 您能否以图片的形式分享您的 IDE 中的项目结构并说明您正在使用的 IDE 版本
  • 我添加了我的项目结构的图片。我正在使用 IntelliJ 2021.3

标签: java spring spring-boot spring-mvc jsp


【解决方案1】:

SpringBoot 的主要模板引擎是 Thymeleaf、Groovy、FreeMarker、Jade。在参考指南中:

应该尽可能避免使用 JSP,有几个已知的限制 将它们与嵌入式 servlet 容器一起使用时。

由于硬编码文件模式,可执行 jar 将无法工作 在 Tomcat 中。

如果 JSP 是您无法转换的遗留代码或专有代码,您必须做一些事情来开发/维护、编译和运行在 Intellij 中运行它们的 SpringBootApplication:

  1. maven:你的 pom 必须是一个 'war' 包。 这将使 intellij 正确查找和编译 JSP。

  2. web facet:将您的 .jsp 文件放在一个文件夹中,它们应该在 webapp 中:src/main/webapp/WEB-INF/jsp/ jsp 永远不会在静态中“编译”/“可解释”。

  3. spring facet:在 application.properties

    中将前缀设置为 /WEB-INF/jsp/
  4. 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>
  1. 构建可运行的战争:制作可运行的 Intellij“maven 配置”:

全新安装 -f pom.xml

  1. 运行该战争:使用这些设置进行 Intellij“jar 配置”:
  • jar 路径:target 文件夹中 war 文件的路径>
  • 启动前:运行您创建的“maven 配置”

【讨论】:

    猜你喜欢
    • 2014-08-27
    • 2018-02-10
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 2017-07-08
    • 1970-01-01
    • 2016-01-22
    • 2020-06-27
    相关资源
    最近更新 更多