获取资源404不可用tomcat

时间:2017-08-28 15:01:04

标签: java spring eclipse tomcat

我正在尝试在Spring MVC中开发一个HelloWorld示例,我已经了解Java但是我在春天是新的,我正在关注这个教程点教程: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";
    }
}

-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中的项目&gt;出口&gt; WAR文件,并将其保存在Tomcat的webapps文件夹中,我也有Jenkins的战争,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 />

1 个答案:

答案 0 :(得分:0)

您尚未在HelloWeb-servlet.xml中启用注释,请添加以下代码

<context:annotation-config />

之前

<context:component-scan base-package = "com.tutorialspoint" />
相关问题