gpt4 book ai didi

java - Tomcat 和 STS 上的 Spring Web MVC - 请求的资源不可用

转载 作者:行者123 更新时间:2023-11-30 08:53:28 24 4
gpt4 key购买 nike

使用 Spring Tool Suite (STS) 和 Tomcat v8.0,我遵循了 this学习并获得一些有关 Spring 的知识的教程。我的意图是显示一个简单的“Hello World”之类的东西,并以此为基础进行构建。

这是我的代码。大多数这些文件是在启动项目时自动创建的:

HomeController.java:

public class HomeController {

private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
logger.info("Welcome home! The client locale is {}.", locale);

Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

String formattedDate = dateFormat.format(date);

model.addAttribute("serverTime", formattedDate );
model.addAttribute("welcomeMessage", "Spring MVC trial: Welcome!");

return "home";
}
}

home.jsp:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>
Hello world!
</h1>

<P> The time on the server is ${serverTime}. </P>
<P> ${welcomeMessage} </P>
</body>
</html>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

我唯一偏离教程的地方是使用了 Tomcat v8.0 服务器。

问题:

这是内部浏览器的结果,设置为(默认情况下,在运行项目时):
http://localhost:8080/spring/WEB-INF/views/home.jsp

enter image description here

这就是我在运行该项目时得到的结果。从我从日志中收集到的信息来看,服务器工作正常。

但是,根据教程,我应该得到:

"Welcome home! The client locale is XXXXX"

在控制台日志中,我没有看到。

我做错了什么?

最佳答案

你的请求应该是http://localhost:8080/{applicationname}

因为 return "home" 你的 HomeController 会自动调用一个名为 home.jsp

的 JSP 文件

如何查找http请求的真实应用名称?

  1. 在 STS 的 Servers 窗口中双击您的服务器。在此示例中,它是 Pivotal tc Server Developer Edition v3.1

Double click on Pivotal tc Server...

  1. Web 模块文件现在已打开。然后单击Modules,您将看到请求应用程序名称,在本例中为/mvc:

enter image description here

所以浏览器 url 现在是 http://localhost:8080/mvc

关于java - Tomcat 和 STS 上的 Spring Web MVC - 请求的资源不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29792572/

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