简体   繁体   English

Spring boot:找不到 JSP (404)

[英]Spring boot: JSP not found (404)

I'm trying to create a new project with spring boot.我正在尝试使用 Spring Boot 创建一个新项目。 But I'm getting Error described below.但我收到下面描述的错误。 I have added my code.我已经添加了我的代码。

Error错误

HTTP ERROR 404 page not found找不到 HTTP 错误 404 页面

Here is the link of my project structure.这是我的项目结构的链接。 Structure image link结构图链接

pom.xml 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.wc</groupId>
    <artifactId>wc</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>wc</name>
    <description>Work configurator project</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</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>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>`

login.jsp contains just hello login.jsp只包含hello

LoginController.class登录控制器.class

@Controller
public class LoginController {
@RequestMapping(value = {"/","/login"},method = RequestMethod.GET)
public ModelAndView getLoginPage(){
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("login");
    return modelAndView;
    }
}

WcApplication.class应用程序类

@SpringBootApplication
public class WcApplication {

    public static void main(String[] args) {
        SpringApplication.run(WcApplication.class, args);
    }
}

Why I'm getting this and how to solve this problem?为什么我得到这个以及如何解决这个问题? Thanks谢谢

I think you have missed out ViewResolver configuration class.我认为您错过了ViewResolver配置类。

@Configuration
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter
{
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");
        registry.viewResolver(resolver);
    }
}

Try above configuration and it should call login.jsp page.尝试上面的配置,它应该调用login.jsp页面。


Update更新

As you have correctly added resolver in application.properties .由于您已在application.properties正确添加了解析器。 I figured that the problem is with folder WEB-INF .我认为问题出在文件夹WEB-INF This folder is secured from outer access and .jsp files should not be inside of this folder.此文件夹受外部访问保护, .jsp文件不应位于此文件夹内。 It is for files like web.xml which should not be exposed to client.它用于像web.xml这样不应暴露给客户端的文件。

Just rename it to view (or something else) and change application.properties as -只需将其重命名以view (或其他内容)并将application.properties更改为 -

spring.mvc.view.prefix=/view/jsp/
spring.mvc.view.suffix=.jsp

This should definitely work.这绝对有效。

In my case I had everything correctly setup in spring boot.就我而言,我在 Spring Boot 中正确设置了所有内容。 The jsps were in src/main/webapp/WEB-INF/jsp and index.html the welcome page in src/main/resources/static/index.html . jsps位于src/main/webapp/WEB-INF/jspindex.html src/main/resources/static/index.html的欢迎页面。 Still howevere hard I tried the jsp pages were not getting rendered.尽管如此,我还是努力尝试jsp页面没有被渲染。 I always got 404 return.我总是得到404回报。

But when I changed the build process to create a war field instead of the jar file it worked fine.但是当我更改构建过程以创建一个战争领域而不是 jar 文件时,它运行良好。

Just made additional line change in the build.gradle file as below刚刚在build.gradle文件中进行了额外的行更改,如下所示

apply plugin: "war" 

and after that while ruuing the war as java -jar demo.war everything fell in place and started working.在那之后,在以 java -jar demo.war进行战争的同时,一切都就位并开始工作了。

I just had this same issue occur.我刚刚发生了同样的问题。 Project structure was correct and everything looked good.项目结构正确,一切看起来都很好。 I made a slight change to my application.properties spring.mvc.view.prefix.我对我的 application.properties spring.mvc.view.prefix 做了一点改动。 I changed "/WEB-INF/jsp/" to "WEB-INF/jsp/" and everything works correctly.我将“/WEB-INF/jsp/”更改为“WEB-INF/jsp/”,一切正常。 I think in the generate target and war it is the root directory so the additional / as a prefix was confusing something我认为在生成目标和战争中它是根目录,所以附加的 / 作为前缀会混淆一些东西

Its a hierarchy problem, the WEB-INF should be under webapp folder.这是一个层次结构问题,WEB-INF 应该在 webapp 文件夹下。

yourMainPackage/src/main/webapp/WEB_INF

(note that for it to work with springboot default configuration, webapp/ should be under main/ and not under resources) (请注意,要使其与 springboot 默认配置一起使用,webapp/ 应该在 main/ 下而不是在资源下)

I faced the same issue and it took me long time to figure out that there are three conditions.我遇到了同样的问题,我花了很长时间才弄清楚存在三个条件。

1.) In application properties you need to have the following view resolver mapping (You can use java based as well): 1.) 在应用程序属性中,您需要具有以下视图解析器映射(您也可以使用基于 Java 的):

logging.level.web=DEBUG
spring.mvc.view.prefix=/WEB-INF/pages/
spring.mvc.view.suffix=.jsp

Note: you have to create "WEB-INF" folder by yourself as in my case "webapp" 
   folder was empty. DEBUG property is not necessary just keep it as it will 
   help you find the root cause from logs. 

2.) I had to make the packaging as "war" instead of jar. 2.) 我不得不把包装做成“战争”而不是罐子。

<packaging>war</packaging>

3.) Make sure you have added tomcat jasper dependency to compile the jsp pages at runtime. 3.) 确保您已添加 tomcat jasper 依赖项以在运行时编译 jsp 页面。

    <dependency>
      <groupId>org.apache.tomcat.embed</groupId>
      <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>

Hope it helps and feel free to correct me.希望它有所帮助,并随时纠正我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM