返回 JSP 视图名称时使我的 Spring Boot 索引页面和控制器映射工作的问题

我正在使用 Spring Boot 和 JSP 来学习 Spring Security 中的一些快速教程,但我的控制器映射并index.jsp不起作用。似乎找不到JSP页面。这是我的配置和项目结构:


springsecurity-for-reactive-apps [boot] - Project folder

  - src/main/java

    - com.springsecurity

      - SpringsecurityForReactiveAppsApplication.java

    - com.springsecurity.config

      - ApplicationConfig.java 

      - SecurityWebApplicationInitializer.java

      - SpringMvcWebApplicationInitializer.java 

      - SpringSecurityConfig.java 

      - WebApplicationConfig.java 

  - src

    - main

      - webapp

        - WEB-INF

          - view

             - home.jsp

          - index.jsp

com.springsecurity包包含


@SpringBootApplication

public class SpringsecurityForReactiveAppsApplication {


    public static void main(String[] args) {

        SpringApplication.run(SpringsecurityForReactiveAppsApplication.class, args);

    }

}

com.springsecurity.config 包包含以下类


@Configuration

    public class ApplicationConfig {

         @Value("${spring.datasource.driver-class-name}")

          private String DB_DRIVER;


              @Value("${spring.datasource.password}")

              private String DB_PASSWORD;


              @Value("${spring.datasource.url}")

              private String DB_URL;


              @Value("${spring.datasource.username}")

              private String DB_USERNAME;


            @Autowired

            private Environment env;


            @Bean

            public DataSource getDataSource() {

                DriverManagerDataSource dataSource = new DriverManagerDataSource();

                dataSource.setDriverClassName(DB_DRIVER);

                dataSource.setUrl(DB_URL);

                dataSource.setUsername(DB_USERNAME);

                dataSource.setPassword(DB_PASSWORD);

                return dataSource;

            }


        }


饮歌长啸
浏览 149回答 2
2回答

慕勒3428872

在你的 pom.xml 中删除<dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<groupId>javax.servlet.jsp</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<artifactId>javax.servlet.jsp-api</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<version>2.3.1</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<scope>provided</scope>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<groupId>javax.servlet.jsp.jstl</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<artifactId>javax.servlet.jsp.jstl-api</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<version>1.2.1</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>并添加这些依赖项<dependency>&nbsp; &nbsp;<groupId>org.apache.tomcat.embed</groupId>&nbsp; &nbsp;<artifactId>tomcat-embed-jasper</artifactId></dependency><dependency>&nbsp; <groupId>javax.servlet</groupId>&nbsp; <artifactId>jstl</artifactId></dependency>据我所知,tomcat-embed-jasper需要在启动时呈现jsp页面。

慕森王

1.viewResolver.setPrefix("/WEB-INF/views/"); 视图->视图2.spring可以实现ErrorController@Controllerpublic class ViewController implements ErrorController {&nbsp; &nbsp; @GetMapping("/home")&nbsp; &nbsp; public String home(Model model) {&nbsp; &nbsp; &nbsp;System.out.println("___________home()___________________");&nbsp; &nbsp; &nbsp; &nbsp; return "home";&nbsp; &nbsp; }&nbsp; &nbsp; @GetMapping("/error")&nbsp; &nbsp; public String error(Model model) {&nbsp; &nbsp; &nbsp;System.out.println("___________ERROR-<<error___________________");&nbsp; &nbsp; &nbsp; &nbsp; return "home";&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public String getErrorPath() {&nbsp; &nbsp; &nbsp; &nbsp; return "/error";&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java