我想在Spring MVC中開發一個HelloWorld示例,我已經知道Java,但在新的春天,我正在遵循這個教程點教程:https://www.tutorialspoint.com/spring/spring_mvc_hello_world_example.htm 我有一些問題,但在有一刻它正常工作,然後我轉到下一個教程,然後它不再工作,即使我刪除了所有新文件,但我找不到原因。 (我只是指出了教程的改變,以防這是原因,但也許這不相關)。獲取資源404不可用tomcat
,尤其是圓形是我的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";
}
}
-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文件夾,裏面我有詹金斯戰爭也和詹金斯作品好的,我可以看到Tomcat的主頁也可以,所以我認爲這不是一個Tomcat問題,但我願意接受建議。
然後我去localhost:8080/HelloWeb/hello
,我得到的錯誤。 我已經搜索了一些日子,我發現任何解決方案都爲我工作。
編輯1
我的HelloWeb-servlet.xml中後阿米特ķBIST回覆
<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中添加 ,並且您的bean配置需要引用/更新爲xmlns:mvc =「http://www.springframework.org/schema/mvc」xsi: schemaLocation =「http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd」 –
我剛編輯問題以回覆喲你的回答,我仍然有一個錯誤。 –