2017-07-15 68 views
0

继Spring MVC的教程中,我得到的错误Spring MVC的,Maven的,举足轻重的TC服务器HTML 404 Not Found错误

HTTP状态[404] - [未找到] 源服务器没有找到目前的目标资源代表或不愿意透露目标资源的存在。

任何人都可以识别什么是缺少的或可以指向正确的方向吗?

使用Spring工具套装3.9.o和关键TC服务器和Maven

HomeController.java

package com.infiniteskills.mvc; 

    import java.text.DateFormat; 
    import java.util.Date; 
    import java.util.Locale; 

    import org.slf4j.Logger; 
    import org.slf4j.LoggerFactory; 
    import org.springframework.stereotype.Controller; 
    import org.springframework.ui.Model; 
    import org.springframework.web.bind.annotation.RequestMapping; 
    import org.springframework.web.bind.annotation.RequestMethod; 

    /** 
     * Handles requests for the application home page. 
     */ 
     @Controller 
     public class HomeController { 

     private static final Logger logger = 
     LoggerFactory.getLogger(HomeController.class); 

     /** 
     * Simply selects the home view to render by returning its name. 
     */ 
     @RequestMapping(value = "/", method = RequestMethod.GET) 
     public String home(Locale locale, Model model) { 
      logger.info("Welcome home! The client locale is {}.", locale); 

      Date date = new Date(); 
      DateFormat dateFormat = 
    DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG,locale); 

      String formattedDate = dateFormat.format(date); 

     model.addAttribute("serverTime", formattedDate); 

      return "home"; 
     } 

      } 

的pom.xml

<?xml version="1.0" encoding="UTF-8"?> 
    <project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
    http://maven.apache.org/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.infiniteskills</groupId> 
    <artifactId>mvc</artifactId> 
    <name>hello-world</name> 
    <packaging>war</packaging> 
    <version>1.0.0-BUILD-SNAPSHOT</version> 
    <properties> 
     <java-version>1.6</java-version> 
     <org.springframework-version>3.1.1.RELEASE</org.springframework-version> 
     <org.aspectj-version>1.6.10</org.aspectj-version> 
     <org.slf4j-version>1.6.6</org.slf4j-version> 
     </properties> 
    <dependencies> 
    <!-- Spring --> 
     <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-context</artifactId> 
     <version>${org.springframework-version}</version> 
     <exclusions> 
      <!-- Exclude Commons Logging in favor of SLF4j --> 
      <exclusion> 
       <groupId>commons-logging</groupId> 
       <artifactId>commons-logging</artifactId> 
      </exclusion> 
     </exclusions> 
    </dependency> 

    <dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-webmvc</artifactId> 
    <version>4.1.6.RELEASE</version> 
    </dependency> 
    <!-- Servlet --> 
     <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>servlet-api</artifactId> 
     <version>2.5</version> 
     <scope>provided</scope> 
    </dependency> 
     <dependency> 
     <groupId>javax.servlet.jsp</groupId> 
     <artifactId>jsp-api</artifactId> 
     <version>2.1</version> 
     <scope>provided</scope> 
     </dependency> 
     <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>jstl</artifactId> 
     <version>1.2</version> 
    </dependency> 

web.xml中

 <?xml version="1.0" encoding="UTF-8"?> 
     <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 

      <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> 
     <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/root-context.xml</param-value> 
      </context-param> 

     <!-- Creates the Spring Container shared by all Servlets and Filters --> 
      <listener> 
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
       </listener> 

      <!-- Processes application requests --> 
      <servlet> 
      <servlet-name>appServlet</servlet-name> 
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
     </servlet> 

      <servlet-mapping> 
      <servlet-name>appServlet</servlet-name> 
      <url-pattern>/</url-pattern> 
      </servlet-mapping> 

     </web-app> 

servlet的content.xml中

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

     <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> 

     <!-- Enables the Spring MVC @Controller programming model --> 
<annotation-driven /> 

根的context.xml

 <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> 
     <resources mapping="/resources/**" location="/resources/" /> 

     <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> 
     <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
      <beans:property name="prefix" value="/WEB-INF/views/" /> 
      <beans:property name="suffix" value=".jsp" /> 
     </beans:bean> 

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



    </beans:beans> 


    <?xml version="1.0" encoding="UTF-8"?> 
    <beans xmlns="http://www.springframework.org/schema/beans" 
     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.xsd"> 

     <!-- Root Context: defines shared resources visible to all other web components --> 

     </beans> 
+0

尝试这些选项:1)右键单击项目,部署程序集,查看是否添加了Maven依赖项。 2)右键单击项目,项目构面,将pivotaltc服务器添加为运行时。 – vijayanand

+0

春天工具套件部署组件通过右击项目>首选项>展开组件 Maven依赖已添加 枢纽TC服务器也设置运行 – succeed

回答

0

需要@Controller后使用@RequestMapping("/homeController")用于控制器的映射。方法之前使用@RequestMapping(value = "/hello", method = RequestMethod.GET)。 和URL将b:http://localhost:8080/project-name/homeController/hello并使用GET方法。

+0

@RequestMapping注解(以上家庭法)后的设定发现@控制器(上级)注释已设置。 为什么我需要设置值为'/ hello'?默认的请求方法是GET,所以没有必要明确地设置它,我希望默认路径(“/”)指向家庭控制器,所以我不必输入扩展的url路径。 – succeed

+0

我你不想使用'设置值为'/你好',然后删除'值'它会采用默认'/'。您可以尝试将您的servlet-content.xml移动到WEB-INF中,并在web.xml中使用'classpath:servlet-content.xml'。 – Sharma

相关问题