java - apache 中没有错误,但出现 404 错误

标签 java spring spring-mvc

我已经这样做一天了,不明白为什么它不起作用。我按照教程,阅读了 spring 文档,但仍然不明白,更好的是,还有一些错误需要解决。

    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>

Controller 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>

Controller 类

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>

最佳答案

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"
    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/ 发出请求来自浏览器。

<强>

The caveat to overriding the "/" Servlet mapping is that the RequestDispatcher for the default Servlet must be retrieved by name rather than by path. The DefaultServletHttpRequestHandler will attempt to auto-detect the default Servlet for the container at startup time, using a list of known names for most of the major Servlet containers (including Tomcat, Jetty, GlassFish, JBoss, Resin, WebLogic, and WebSphere).

Resource

关于java - apache 中没有错误,但出现 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29151597/

相关文章:

java - Activity 加载时第一次出现 NullPointerException 但第二次工作正常

java - 用字符串列表构造一棵树并计数

java - 如何以动态方式创建 Spring Bean。使用 Quartz SchedulerFactoryBean

java - Spring-服务被eureka服务器发现后关闭

java - 在 Spring 中加载单例 bean 中的错误消息

java - 如何按最新日期对列表进行排序?

java - 如何在与 JTextPane 关联的 StyledDocument 中使用 onmouseover

java - 当某些值为空时,如何从逗号分隔的值列表中提取值?

java - 解释 JobLauncherApplicationRunner 类是如何工作的?

java - 更改 ModelAttribute 名称后 BindingResult 不起作用