【问题标题】:Getting HTTP-404 error while running spring MVC application in Eclipse - Tutorials point hello world example在 Eclipse 中运行 Spring MVC 应用程序时出现 HTTP-404 错误 - 教程指向 hello world 示例
【发布时间】:2015-11-07 08:01:48
【问题描述】:

我是 Spring 的初学者,我正在从教程点学习,下面的代码与教程点完全相同,但是当我尝试运行它时出现 404 错误,当我运行时代码是 hello world 程序eclipse 使用 tomcat 服务器出现这样的错误

我在浏览器上使用的网址是http://localhost:8080/HelloWeb/hello

HelloController.java:

package com.tutorialspoint;
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
@RequestMapping("/hello")
public class HelloController{

   @RequestMapping(method = RequestMethod.GET)
   public String printHello(ModelMap model) {
      model.addAttribute("message", "Hello Spring MVC Framework!");

      return "hello";
   }

}

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-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.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>

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>

你好.jsp:

<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
   <h2>${message}</h2>
</body>
</html>

这是我的目录结构

HelloWeb 项目:

|--HelloController.java
|-- WebContent
   `-- WEB-INF
         |-- jsp
              |-- web.xml

【问题讨论】:

  • 您尝试访问哪个 URL?访问http://localhost:8080/可以看到Tomcat主页吗?
  • 你打的是什么网址?
  • 是的,我可以访问tomcat主页localhost:8080
  • MVC 框架的重点是永远不要直接点击视图(JSP),而总是通过控制器。顺便说一句,根据设计,WEB-INF 下的所有内容都无法从外部访问。点击控制器的 URL:/HelloWeb/hello。此外,web.xml 文件必须直接位于 WEB-INF 下。不在WEB-INF/jsp下

标签: java eclipse jsp spring-mvc tomcat


【解决方案1】:

http://howtodoinjava.com/2015/02/05/spring-mvc-hello-world-example/#hello_world

按照上面的 URL,它完全可以工作我已经尝试过,并确保你在 pom.xml 中添加了 spring 的依赖项。 同样在您的 webapp 文件夹中 HelloWeb-servlet.xml 也应该存在。

【讨论】:

    猜你喜欢
    • 2016-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多