我正在尝试在 Spring MVC 中开发一个 HelloWorld 示例,我已经了解 Java 但我是 spring 的新手,我正在关注这个 tutorialspoint 教程:https://www.tutorialspoint.com/spring/spring_mvc_hello_world_example.htm 我遇到了一些问题,但有一段时间它还可以,然后我继续学习下一个教程,然后它就不再工作了,即使我删除了所有新文件也找不到原因。 (我只是指出了教程的变化,以防这是原因,但也许这不相关)。
-这是我的 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>
</web-app>
-HelloWeb-servlet.xml
<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"
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">
<context:component-scan base-package = "com.tutorialspoint" />
<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value = "/WEB-INF/jsp/" />
<property name = "suffix" value = ".jsp" />
</bean>
</beans>
-HelloController.java
package com.tutorialspoint;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/hello")
public class HelloController {
@RequestMapping(method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}
-你好.jsp
<%@ page contentType = "text/html; charset = UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
还有我的目录 Directories
要在 Tomcat 中部署它,我只需右键单击 Eclipse 中的项目 > 导出 > WAR 文件,并将其保存在 Tomcat 中的 webapps 文件夹中,那里我也有 Jenkins war,Jenkins 工作正常,我可以看到 Tomcat 主页好的,所以我认为这不是 Tomcat 的问题,但我愿意接受建议。
然后我转到 localhost:8080/HelloWeb/hello
并收到错误。
我已经搜索了几天,发现的任何解决方案都对我有用。
编辑 1
Amit K Bist 回复后我的 HelloWeb-servlet.xml
<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="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
springframework.org/schema/mvc
springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven />
<context:annotation-config />
<context:component-scan base-package = "com.tutorialspoint" />
<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value = "/WEB-INF/jsp/" />
<property name = "suffix" value = ".jsp" />
</bean>
</beans>
我遇到了错误
Multiple annotations found at this line:
- cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mvc:annotation-driven'.
- schema_reference.4: Failed to read schema document 'springframework.org/schema/mvc/spring-mvc.xsd', because 1) could not find the document;
2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
在第 12 行
<mvc:annotation-driven />
最佳答案
你没有在你的HelloWeb-servlet.xml中启用注释,请添加下面的代码
<context:annotation-config />
之前
<context:component-scan base-package = "com.tutorialspoint" />
关于java - 获取资源404不可用tomcat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45921997/