有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java获取资源404不可用tomcat

我试图在SpringMVC中开发一个HelloWorld示例,我已经知道Java,但我在Spring是新手,我遵循这个教程点教程:https://www.tutorialspoint.com/spring/spring_mvc_hello_world_example.htm 我遇到了一些问题,但在一瞬间它还可以正常工作,然后我转到下一个教程,然后它不再工作,即使我删除了所有新文件,我也找不到原因。 (我只是指出了教程的变化,以防这是原因,但可能这并不相关)

-这是我的网站。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>

-HelloWebservlet。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>

-Hello控制器。爪哇

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

我的HelloWebservlet。Amit K Bist回复后的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) 个答案

  1. # 1 楼答案

    您尚未在HelloWebservlet中启用注释。xml,请添加以下代码

    <context:annotation-config />
    

    以前

    <context:component-scan base-package = "com.tutorialspoint" />