Spring映射失败:HTTP状态404(请求的资源不可用)

时间:2015-08-02 14:14:30

标签: java spring jsp spring-mvc http-status-code-404

我知道有数百万种可能的解决方案,我尝试了很多,但仍然没有任何好处。

我正在尝试将基本控制器映射到url并返回JSP视图。它显示带有描述的HTTP状态404请求的资源不可用。请帮忙。

控制器

package controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class HomePageController {

@RequestMapping(value = "/twenty-eight/index", method = RequestMethod.GET)
public ModelAndView homePage(HttpServletRequest httpServletRequest, 
        HttpServletResponse httpServletResponse){
    ModelAndView mav = new ModelAndView("HomePage");
    System.out.println("Going to index page...");
    return mav;
    }
}

的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 Web MVC Application</display-name>
<servlet>
    <servlet-name>dispatcher-servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

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

servlet的context.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
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="controllers" />

<mvc:annotation-driven />

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/views/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

JSP视图

<!DOCTYPE html>
<html>
<head>
    <title>HTML5 Login</title>
    <link rel="stylesheet" href="CSS/normalize.css">
    <link rel="stylesheet" href="CSS/style.css">
</head>
<body>
    <h1>Working Fine</h1>
</body>
</html>

Tomcat记录

Aug 02, 2015 7:16:04 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Twenty Eight' did not find a matching property.
Aug 02, 2015 7:16:04 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version:        Apache Tomcat/8.0.21
Aug 02, 2015 7:16:04 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Mar 23 2015 14:11:21 UTC
Aug 02, 2015 7:16:04 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number:         8.0.21.0
Aug 02, 2015 7:16:04 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Linux
Aug 02, 2015 7:16:04 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            3.16.0-44-generic
Aug 02, 2015 7:16:04 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
Aug 02, 2015 7:16:04 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             /usr/local/java/jdk1.8.0_40/jre
Aug 02, 2015 7:16:04 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.8.0_40-b26
Aug 02, 2015 7:16:04 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Aug 02, 2015 7:16:04 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         /home/kaustubh/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
Aug 02, 2015 7:16:04 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         /var/local/apache-tomcat-8.0.21
Aug 02, 2015 7:16:04 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=/home/kaustubh/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
Aug 02, 2015 7:16:04 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=/var/local/apache-tomcat-8.0.21
Aug 02, 2015 7:16:04 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=/home/kaustubh/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps
Aug 02, 2015 7:16:04 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=/var/local/apache-tomcat-8.0.21/endorsed
Aug 02, 2015 7:16:04 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=UTF-8
Aug 02, 2015 7:16:04 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
Aug 02, 2015 7:16:04 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Aug 02, 2015 7:16:04 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Aug 02, 2015 7:16:04 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Aug 02, 2015 7:16:04 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Aug 02, 2015 7:16:04 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 989 ms
Aug 02, 2015 7:16:04 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Aug 02, 2015 7:16:04 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.21
Aug 02, 2015 7:16:06 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Aug 02, 2015 7:16:06 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Aug 02, 2015 7:16:07 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcher-servlet'
Aug 02, 2015 7:16:07 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'dispatcher-servlet': initialization started
Aug 02, 2015 7:16:07 PM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'dispatcher-servlet-servlet': startup date [Sun Aug 02 19:16:07 IST 2015]; root of context hierarchy
Aug 02, 2015 7:16:07 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/servlet-context.xml]
Aug 02, 2015 7:16:08 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping register
INFO: Mapped "{[/twenty-eight/index],methods=[GET]}" onto public org.springframework.web.servlet.ModelAndView controllers.HomePageController.homePage(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
Aug 02, 2015 7:16:09 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
INFO: Looking for @ControllerAdvice: WebApplicationContext for namespace 'dispatcher-servlet-servlet': startup date [Sun Aug 02 19:16:07 IST 2015]; root of context hierarchy
Aug 02, 2015 7:16:09 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
INFO: Looking for @ControllerAdvice: WebApplicationContext for namespace 'dispatcher-servlet-servlet': startup date [Sun Aug 02 19:16:07 IST 2015]; root of context hierarchy
Aug 02, 2015 7:16:09 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'dispatcher-servlet': initialization completed in 1772 ms
Aug 02, 2015 7:16:09 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Aug 02, 2015 7:16:09 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Aug 02, 2015 7:16:09 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 4680 ms

项目结构

https://drive.google.com/open?id=0B2s3KIDgKxy8RlVqZmdneW9WV1U

0 个答案:

没有答案