java - 运行项目时项目中未检测到jsp

标签 java spring spring-mvc jsp intellij-idea

我使用 spring security 有简单的登录屏幕。如果登录成功,则重定向到 home.jsp。但是,它说无法找到Jsp页面。在 maven 中创建新项目时,没有名为 webapp 的文件夹,因此我自己创建了该 webapp,并将该 webapp 添加到 webapp/WEB-INF/view/home.jsp 中,如下所示:

enter image description here

我的项目结构是:

enter image description here

因此,登录屏幕成功出现,当我登录时,会显示这样的消息:

enter image description here

它说找不到 home.jsp。

我的配置是::

package com.ashwin.security.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.ashwin.security")
public class DemoAppConfig {

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver=new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/view/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }



}

我的 pom.xml 是:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ashwin</groupId>
    <artifactId>springsecurity2</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>spring-security-demo</name>

    <properties>
        <springframework.version>5.0.2.RELEASE</springframework.version>

       <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <springsecurity.version>5.0.0.RELEASE</springsecurity.version>
    </properties>

    <dependencies>

        <!-- Spring MVC support -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${springframework.version}</version>
        </dependency>

        <!-- Servlet, JSP and JSTL support -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <!-- spring security config -->
        <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-web -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>${springsecurity.version}</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-config -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>${springsecurity.version}</version>
        </dependency>



    </dependencies>

    <!-- TO DO: Add support for Maven WAR Plugin -->
    <build>
      <!--  <finalName>/springsecurity</finalName>-->
        <sourceDirectory>src/main/java</sourceDirectory>


        <resources>


        <resource>

        <directory>src/main/resources</directory>

    </resource>

    </resources>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.2.0</version>
                </plugin>

                <plugin>

                <groupId>org.apache.tomcat.maven</groupId>

                <artifactId>tomcat7-maven-plugin</artifactId>

                <version>2.2</version>


                <configuration>

                <path>/spring</path>

            </configuration>

            </plugin>
            </plugins>
        </pluginManagement>
    </build>


</project>

DispatcherServlet 类是

package com.ashwin.security.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class SpringMvcDispatcherServletInitializer  extends AbstractAnnotationConfigDispatcherServletInitializer {


     @Override
        protected Class <?> [] getRootConfigClasses() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        protected Class <?> [] getServletConfigClasses() {
            return new Class[] { DemoAppConfig.class  };
        }

        @Override
        protected String[] getServletMappings() {
            return new String[] { "/" };
        }

}

最佳答案

您的tomcat7-maven-plugin配置包含<path>/spring</path> 。请替换它并尝试使用 <path>/${project.build.finalName}</path> .

关于java - 运行项目时项目中未检测到jsp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57440524/

相关文章:

java - Java 中的变量作用域和可见性

java - 使用 jsch 执行命令 ntpq -p

java - spring boot oauth2 feign 允许匿名请求

java - 我们可以使用属性文件中的表达式获取属性吗

css - 带有整个 html 模板的 spring mvc

java - 找不到类 [org.apache.shiro.spring.LifecycleBeanPostProcessor]

java - 在 Hibernate 中无法删除没有子级的父级

java - 在没有用户 session 计数限制的情况下使用 sessionRegistry?

java - MongoSocketReadException : Prematurely reached end of stream (Java to Mongo using ssl)

java - 缓存控制在 Firefox 中不起作用