gpt4 book ai didi

java - Tomcat 不呈现 jsp 页面

转载 作者:行者123 更新时间:2023-11-28 22:48:54 26 4
gpt4 key购买 nike

为什么 Tomcat 不呈现 jsp 页面?如果我告诉 servlets 容器 URL:http://localhost:8080/contacts 没关系(因为 Controller 处理 GET - 请求),但每当我直接告诉 Tomcat jsp 在哪里在项目中 http://localhost:8080/WEB-INF/views/list.jsp 比 get

404 error

这是 IntelliJ IDEA 项目结构:

enter image description here

我的pom.xml:

<dependencies>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>

<!--support @Service, @Repository -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>



<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>

<!-- teg library -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
<scope>test</scope>
</dependency>

</dependencies>

Controller :

@Controller
@RequestMapping("/contacts")
public class ContactController {

@RequestMapping(method = RequestMethod.GET)
public String list(Model model) {


return "list";
}
}

jsp:

<%@ page contentType="text/html; charset=UTF-8" language="java" %>

<%--
Created by IntelliJ IDEA.
User: Игорь
Date: 07.10.2017
Time: 13:18
To change this template use File | Settings | File Templates.
--%>

<html>
<head>
<title>Title</title>
</head>
<body>

<% System.out.println("Inside JSP file!!!!!!!!!!!!!!!!!!!"); %>

<h1>Contact Listing</h1>


SOME MESSAGE!

</body>
</html>

最佳答案

如 servlet 规范第 10.5 节所述:

... except for the case where static resources are packaged in JAR files, any requests from the client to access the resources in WEB-INF/ directory must be returned with a SC_NOT_FOUND(404) response.

即Web 应用程序的 WEB-INF 目录中唯一可访问的资源是那些打包在 Web 片段 jars 中的资源。

您可以使用请求调度程序包含或转发到 WEB-INF 目录中的 JSP(或其他资源),但客户端可能无法直接访问它。

这可以防止最终用户访问可能属于您的配置或类文件本身的潜在敏感信息。

关于java - Tomcat 不呈现 jsp 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46729730/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com