使用动态Web项目Http 404资源不可用,但在控制台中没有错误

时间:2015-03-15 13:29:14

标签: java spring spring-mvc

我正在构建一个Spring MVC应用程序;我正在使用Spring Tool Suite 3.6.4和TomCat8。当我运行网络应用时,当我访问 /问候时,我在控制台中收到“Http Status 404请求的资源不可用”,我有既没有错误< / strong>也不是我使用System.out.println显示的消息

在我的代码下面。请帮忙

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>fitTracker</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>fitness</display-name>
    <servlet-name>fitness</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>fitness</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app

健身servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<context:annotation-config/>
<context:component-scan base-package="com.thewizardofoz.controllers.controllers"/>

 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
     p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />

</beans>

HelloController.java

package com.thewizardofoz.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {

    @RequestMapping(value="/greeting")
    public String sayHello(Model model){

        System.out.println("Hello Console");
        model.addAttribute("greeting", "Welcome to our world!");
        return "hello";
    }

}

的hello.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
  <h1>${greeting}</h1>
</body>
</html>

的index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
  <h1>INDEX</h1>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

<context:component-scan/>扫描包com.thewizardofoz.controllers.controllers,但您的控制器位于包com.thewizardofoz.controllers

将其更改为

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

它应该有效

答案 1 :(得分:0)

请更改标签:

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

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

您还可以访问本教程:http://www.breathejava.com/spring-framework-2/create-spring-mvc-project-with-maven-in-eclipse/