编程部署但是给出了一个404错误Spring

时间:2015-03-29 17:25:14

标签: java spring spring-mvc

我正在学习春天,正在制作练习课程。运行程序时,我收到以下404错误。我将在最后发布整个控制台输出。我正在尝试实现DAO方法并使用控制器创建新页面。我将发布代码,以便您了解我想说的内容:

URL

http://localhost:8080/SpringMVCTest/

提供控制器

@Controller
public class OffersController {

    public OffersController() {

        System.out.println("loaded OffersController");
    }

    /*
     * private OffersService offersService;
     * 
     * @Autowired public void setOffersService(OffersService offersService) {
     * this.offersService = offersService; }
     */
    @RequestMapping("/offers")
    public ModelAndView showOffers() {
        System.out.println("in offers");
        ModelAndView mv = new ModelAndView("/offers");
        return mv;
    }

    @RequestMapping("/createoffer")
    public ModelAndView createOffer() {
        System.out.println("in createoffer");
        ModelAndView mv = new ModelAndView("/createoffer");
        return mv;
    }
}

offer.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>

    <c:forEach var="offers" items="${offer}">
        <p><c:out value= ${offers}></c:out></p>
        <p />
    </c:forEach>
</body>
</html>

home.jsp控制器

package com.learnspring.web.config;

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

@Controller
public class HomeController {

    @RequestMapping("/")
    public ModelAndView showMessage() {
        System.out.println("in controller");
        ModelAndView mv = new ModelAndView("/home");
        return mv;
    }

}

针对home.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>


<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>

<p> <a href="${pageContext.request.contextPath}/offers">Show current offers</a></p>
<p> <a href="${pageContext.request.contextPath}/createoffer">Add a new offer</a></p>

</body>
</html>

的web.xml

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

    <display-name>SpringMVCTest</display-name>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/offers-servlet.xml</param-value>
    </context-param>

    <!-- Database bean named offer and the DAO implementation -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:com/learnspring/web/config/dao-context.xml</param-value>
    </context-param>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
        classpath:com/learnspring/web/config/dao-context.xml
        classpath:com/learnspring/web/config/service-context.xml
        </param-value>
    </context-param>

    <!-- Servlet for offers -->
    <servlet>
        <description></description>
        <display-name>offers</display-name>
        <servlet-name>offers</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>offers</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- MySQL configuration -->
    <description>Spring Database</description>
    <resource-ref>
        <description>DB Connection</description>
        <res-ref-name>jdbc/spring</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
</web-app>

服务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:context="http://www.springframework.org/schema/context"
    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">

    <!-- handles OffersService.java -->
    <context:annotation-config></context:annotation-config>
    <context:component-scan base-package="com.learnspring.service"></context:component-scan>
</beans>

OffersService.java

package com.learnspring.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.learnspring.DAO.Offer;
import com.learnspring.DAO.OfferDAO;

@Service("offersService")
public class OffersService {

    private OfferDAO offersDAO;

    @Autowired
    public void setOffersDAO(OfferDAO offersDAO) {
        this.offersDAO = offersDAO;
    }

    public List<Offer> getCurrent() {

        return offersDAO.getOffers();
    }

}

控制台输出

Mar 29, 2015 7:31:59 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:SpringMVCTest' did not find a matching property.
Mar 29, 2015 7:31:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version:        Apache Tomcat/8.0.17
Mar 29, 2015 7:31:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Jan 9 2015 15:58:59 UTC
Mar 29, 2015 7:31:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number:         8.0.17.0
Mar 29, 2015 7:31:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Mac OS X
Mar 29, 2015 7:31:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            10.10.2
Mar 29, 2015 7:31:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          x86_64
Mar 29, 2015 7:31:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JAVA_HOME:             /Library/Java/JavaVirtualMachines/jdk1.8.0_31.jdk/Contents/Home/jre
Mar 29, 2015 7:31:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.8.0_31-b13
Mar 29, 2015 7:31:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Mar 29, 2015 7:31:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         /apache-tomcat-8.0.17
Mar 29, 2015 7:31:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         /apache-tomcat-8.0.17
Mar 29, 2015 7:31:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=/apache-tomcat-8.0.17
Mar 29, 2015 7:31:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=/apache-tomcat-8.0.17
Mar 29, 2015 7:31:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=/apache-tomcat-8.0.17/webapps
Mar 29, 2015 7:31:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=/apache-tomcat-8.0.17/endorsed
Mar 29, 2015 7:31:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=UTF-8
Mar 29, 2015 7:31:59 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: /Users/DrewJocham/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
Mar 29, 2015 7:31:59 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Mar 29, 2015 7:31:59 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Mar 29, 2015 7:31:59 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Mar 29, 2015 7:31:59 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Mar 29, 2015 7:31:59 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1248 ms
Mar 29, 2015 7:31:59 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Mar 29, 2015 7:31:59 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.17
Mar 29, 2015 7:32:01 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Mar 29, 2015 7:32:01 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
Mar 29, 2015 7:32:01 PM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization started
Mar 29, 2015 7:32:01 PM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing Root WebApplicationContext: startup date [Sun Mar 29 19:32:01 CEST 2015]; root of context hierarchy
Mar 29, 2015 7:32:01 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [com/learnspring/web/config/dao-context.xml]
Mar 29, 2015 7:32:02 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [com/learnspring/web/config/service-context.xml]
It loaded DAO
Mar 29, 2015 7:32:02 PM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization completed in 834 ms
Mar 29, 2015 7:32:02 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'offers'
Mar 29, 2015 7:32:02 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'offers': initialization started
Mar 29, 2015 7:32:02 PM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'offers-servlet': startup date [Sun Mar 29 19:32:02 CEST 2015]; parent: Root WebApplicationContext
Mar 29, 2015 7:32:02 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/offers-servlet.xml]
loaded OffersController
Mar 29, 2015 7:32:03 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping registerHandlerMethod
INFO: Mapped "{[/offers],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView com.learnspring.test.OffersController.showOffers()
Mar 29, 2015 7:32:03 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping registerHandlerMethod
INFO: Mapped "{[/createoffer],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView com.learnspring.test.OffersController.createOffer()
Mar 29, 2015 7:32:03 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
INFO: Looking for @ControllerAdvice: WebApplicationContext for namespace 'offers-servlet': startup date [Sun Mar 29 19:32:02 CEST 2015]; parent: Root WebApplicationContext
Mar 29, 2015 7:32:03 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
INFO: Looking for @ControllerAdvice: WebApplicationContext for namespace 'offers-servlet': startup date [Sun Mar 29 19:32:02 CEST 2015]; parent: Root WebApplicationContext
Mar 29, 2015 7:32:03 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'offers': initialization completed in 1053 ms
Mar 29, 2015 7:32:03 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /apache-tomcat-8.0.17/webapps/docs
Mar 29, 2015 7:32:03 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.
Mar 29, 2015 7:32:03 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /apache-tomcat-8.0.17/webapps/docs has finished in 124 ms
Mar 29, 2015 7:32:03 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /apache-tomcat-8.0.17/webapps/examples
Mar 29, 2015 7:32:03 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized()
Mar 29, 2015 7:32:03 PM org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized()
Mar 29, 2015 7:32:03 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /apache-tomcat-8.0.17/webapps/examples has finished in 297 ms
Mar 29, 2015 7:32:03 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /apache-tomcat-8.0.17/webapps/host-manager
Mar 29, 2015 7:32:04 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.
Mar 29, 2015 7:32:04 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /apache-tomcat-8.0.17/webapps/host-manager has finished in 132 ms
Mar 29, 2015 7:32:04 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /apache-tomcat-8.0.17/webapps/manager
Mar 29, 2015 7:32:04 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.
Mar 29, 2015 7:32:04 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /apache-tomcat-8.0.17/webapps/manager has finished in 97 ms
Mar 29, 2015 7:32:04 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /apache-tomcat-8.0.17/webapps/ROOT
Mar 29, 2015 7:32:04 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.
Mar 29, 2015 7:32:04 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /apache-tomcat-8.0.17/webapps/ROOT has finished in 102 ms
Mar 29, 2015 7:32:04 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Mar 29, 2015 7:32:04 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Mar 29, 2015 7:32:04 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 4440 ms
Mar 29, 2015 7:32:05 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/SpringMVCTest/] in DispatcherServlet with name 'offers'
Mar 29, 2015 7:35:30 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/SpringMVCTest/] in DispatcherServlet with name 'offers'

报价-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:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.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-4.1.xsd">

    <context:component-scan base-package="com.learnspring.test">
    </context:component-scan>
    <mvc:annotation-driven></mvc:annotation-driven>

    <bean id="jspViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="prefix" value="/WEB-INF/jsps"></property>
        <property name="suffix" value=".jsp"></property>

    </bean>

</beans>

1 个答案:

答案 0 :(得分:0)

您的HomeController似乎在com.learnspring.web.config,并且没有被扫描包。我想你的OffersController在同一个包中。否则,您还需要将其添加到打包扫描中。希望这能解决问题。所以请尝试更改以下内容

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

<context:component-scan base-package="com.learnspring.web.config">
    </context:component-scan>