【发布时间】:2023-03-03 22:10:02
【问题描述】:
我正在 Eclipse 版本 4.9.0 Java EE IDE 上为 Web 开发人员创建新的 spring mvc 动态 Web 项目。我正在设置一个新的 Server Tomcat 版本 9.0.8。
当我选择hello.jsp 和right-click ->Run As -> Run on Server 时,hello.jsp 工作正常,我访问helo.jsp。但是当我在服务器上选择项目根文件夹和right-click ->Run As -> Run 时,它会显示HTTP Status 404 并带有描述:
源服务器没有找到目标资源的当前表示或不愿意透露存在的表示。'
这是我的 Web.xml:
<web-app id = "WebApp_ID" version = "2.4"
xmlns = "http://java.sun.com/xml/ns/j2ee"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
这是我完整的 HelloWeb-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:context = "http://www.springframework.org/schema/context"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.tutorialspoint"/>
<mvc:annotation-driven/>
<bean name="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WebContent/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
这是我的控制器:
package com.mypackage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;
@Controller
public class HelloController {
@RequestMapping(path="/hello",method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}
这是我的目录结构: https://imgur.com/CHKEsJo
这是我的 hello.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>Hello World</title>
</head>
<body>
<h2>done!!!</h2>
<h2>${message}</h2>
</body>
</html>
Tomcat 版本 9.0.8 上的 server.xml 保持原样。我没有改变任何东西。只有我改变了 HTTP/1.1 端口、AJP/1.3 端口和 Tomcat 管理端口。
这里有什么问题,这是什么原因?我不知道为什么。谁能在这里指出我的错误?
【问题讨论】:
-
不走运。我从 Web.xml 编辑了
/* 。保存并在Tomcat服务器上运行,显示同样的错误。 -
另外提一下,你是如何访问 jsp 文件的,意思是我要问的上下文路径
-
朋友们,抱歉耽搁了回复。这是我的目录结构和 hello.jsp
标签: java spring-mvc tomcat9