简体   繁体   中英

jsp file not recognized in controller class

When I try to return my jsp view file I get yellow squiggly lines saying "Cannot resolve MVC view index". Ive tried moving the webapp folder to different locations but doesn't seem to matter. And obviously i get an error when trying to access the index jsp file when i '/action'. I get "Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Fri May 15 23:18:57 CDT 2020 There was an unexpected error (type=Not Found, status=404). No message available" Picture of directories

HomeController:


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HomeController
{
    @GetMapping("/action")
    public String index()
    {return "index";}

}

applications.properties:

spring.mvc.view.prefix="/WEB-INF/view/"
spring.mvc.view.suffix=".jsp"

index.jsp:

<html>
<body>
<h1>hello</h1>
</body>
</html>

pom.xml:

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>WBAPP</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>WBAPP</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <version>10.0.0-M5</version>
        </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>

If you are using a spring boot with JSP or thymeleaf or any other template engines you do not need to set anything.
You have to put your template files under this path src/main/resources/templates .
If you are not using any template engine then just put your HTML file under this path src/main/resources/static/ .
Also, I suggest that use thymeleaf instead of JSP .

Reference: Spring MVC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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