java JSP文件未在Sping Boot 应用程序中呈现

wfypjpf4  于 2023-05-05  发布在  Java
关注(0)|答案(2)|浏览(108)

我有一个Sping Boot Web应用程序,它读取index.jsp并使用tomcat碧玉来支持JSP。然而,页面显示为“有一个意外的错误(类型=未找到,状态=404)”。
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 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.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.Alex</groupId>
    <artifactId>WebApp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>WebApp</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>14</java.version>
    </properties>

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

<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-jasper</artifactId>
    <version>9.0.35</version>
</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>
    </dependencies>

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

</project>

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Hello this shit works
</body>
</html>

该方法在print语句执行时运行

@Controller
public class indexController {

// Tomcat Jasper to return jsp files
@RequestMapping("index")
public String getIndex() {
    System.out.println("test1");
    return "index.jsp";
}

文件结构

c9x0cxw0

c9x0cxw01#

pom文件中添加此依赖项和其他(如web)。

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

application.properties文件中添加文件位置的配置属性(您的.jsp文件在类路径中的位置

属性配置:

spring.mvc.view.prefix = /WEB-INF/views/
        spring.mvc.view.suffix =.jsp
        spring.mvc.static-path-pattern=/resources/**

类级别配置:

@Configuration
        @EnableWebMvc
        @ComponentScan(basePackages= {"com.imran"})
        public class WebConfig implements WebMvcConfigurer {

            public void configureViewResolvers(ViewResolverRegistry registry) {
                registry.jsp().prefix("/WEB-INF/views/").suffix(".jsp");
            }
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
            }

        }

您的控制器将像这样(带out .jsp扩展名,只有文件名):

@Controller
        public class indexController  {
            
            @RequestMapping(value = "/index", method = RequestMethod.GET)
            public String getIndex() {
                System.out.println("test1");
                return "index";
            }

        }

如果需要,可以从here获得一些帮助。

yizd12fk

yizd12fk2#

如果你使用的是spring starter项目,那么你将无法找到创建新项目的路径,如“动态web项目”,然后你将获得文件夹web-inf,在里面你可以创建视图,并在属性文件中设置路径...spring.mvc.view.prefix = /WEB-INF/views/ spring.mvc.view.suffix =.jsp

相关问题