apache没有错误,但我收到404错误

时间:2015-03-19 17:48:23

标签: java spring spring-mvc

我现在已经在这一天了,不明白为什么它不起作用。我按照教程,阅读弹簧文档仍然不明白,更好的是有错误发生。

    Mar 19, 2015 6:42: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:Spring with JSP' did not find a matching property.
Mar 19, 2015 6:42: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:JavaSpringMVCTake2' did not find a matching property.
Mar 19, 2015 6:42:04 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version:        Apache Tomcat/8.0.17
Mar 19, 2015 6:42:04 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Jan 9 2015 15:58:59 UTC
Mar 19, 2015 6:42:04 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number:         8.0.17.0
Mar 19, 2015 6:42:04 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Mac OS X
Mar 19, 2015 6:42:04 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            10.10.2
Mar 19, 2015 6:42:04 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          x86_64
Mar 19, 2015 6:42:04 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JAVA_HOME:             /Library/Java/JavaVirtualMachines/jdk1.8.0_31.jdk/Contents/Home/jre
Mar 19, 2015 6:42:04 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.8.0_31-b13
Mar 19, 2015 6:42:04 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Mar 19, 2015 6:42:04 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         /Users/DrewJocham/Documents/caveOfprograming/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
Mar 19, 2015 6:42:04 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         /apache-tomcat-8.0.17
Mar 19, 2015 6:42:04 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=/Users/DrewJocham/Documents/caveOfprograming/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
Mar 19, 2015 6:42:04 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=/apache-tomcat-8.0.17
Mar 19, 2015 6:42:04 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=/Users/DrewJocham/Documents/caveOfprograming/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps
Mar 19, 2015 6:42:04 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=/apache-tomcat-8.0.17/endorsed
Mar 19, 2015 6:42:04 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=UTF-8
Mar 19, 2015 6:42: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: /Users/DrewJocham/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
Mar 19, 2015 6:42:04 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Mar 19, 2015 6:42:05 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Mar 19, 2015 6:42:05 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Mar 19, 2015 6:42:05 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Mar 19, 2015 6:42:05 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 2855 ms
Mar 19, 2015 6:42:05 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Mar 19, 2015 6:42:05 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.17
Mar 19, 2015 6:42: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.
Mar 19, 2015 6:42:06 PM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [223] milliseconds.
Mar 19, 2015 6:42:08 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Mar 19, 2015 6:42:08 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'offers'
Mar 19, 2015 6:42:08 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'offers': initialization started
Mar 19, 2015 6:42:08 PM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'offers-servlet': startup date [Thu Mar 19 18:42:08 CET 2015]; root of context hierarchy
Mar 19, 2015 6:42:08 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/offers-servlet.xml]
Mar 19, 2015 6:42:10 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping registerHandlerMethod
INFO: Mapped "{[/],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.learnspring.controller.OffersController.showHome()
Mar 19, 2015 6:42:10 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
INFO: Looking for @ControllerAdvice: WebApplicationContext for namespace 'offers-servlet': startup date [Thu Mar 19 18:42:08 CET 2015]; root of context hierarchy
Mar 19, 2015 6:42:10 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
INFO: Looking for @ControllerAdvice: WebApplicationContext for namespace 'offers-servlet': startup date [Thu Mar 19 18:42:08 CET 2015]; root of context hierarchy
Mar 19, 2015 6:42:10 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'offers': initialization completed in 2159 ms
Mar 19, 2015 6:42:12 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Mar 19, 2015 6:42:12 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Mar 19, 2015 6:42:12 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Mar 19, 2015 6:42:12 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 6971 ms

的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>JavaSpringMVCTake2</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>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>
</web-app>

控制器Servlet

<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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.controller"></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>

控制器类

package com.learnspring.controller;

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

@Controller
public class OffersController {

    @RequestMapping("/")
    public String showHome() {
        return "home";
    }

}

针对home.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>
Hello World from JSP...
</body>
</html>

1 个答案:

答案 0 :(得分:0)

<强>提供-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"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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.controller"></context:component-scan>
    <mvc:annotation-driven></mvc:annotation-driven>
    <mvc:default-servlet-handler />
    <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>

因此,您的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>SpringJSP</display-name>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:offers-servlet.xml</param-value>
    </context-param>
    <servlet>
        <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>
</web-app>

OffersController.java

package com.learnspring.controller;

import javax.servlet.http.HttpServletRequest;

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
@RequestMapping(value = "/")
public class OffersController {
    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView homeGet(HttpServletRequest request) {
        System.out.println("Check mapping");
        ModelAndView model = new ModelAndView("home");
        return model;
    }
}

最后的说明:

  • 使用Spring webmvc jar。 checkpom.xml你需要的就是这个。
  • 添加到offers-servlet.xml。请查看以下注释 - 不要在eclipse中使用run on server并从浏览器向http://localhost:8080/offers/发出请求。

  

重写“/”Servlet映射的警告是   必须按名称检索默认Servlet的RequestDispatcher   而不是路径。 DefaultServletHttpRequestHandler将尝试   在启动时自动检测容器的默认Servlet,   使用大多数主要Servlet容器的已知名称列表   (包括Tomcat,Jetty,GlassFish,JBoss,Resin,WebLogic和   的WebSphere)。

Resource