使用注释的基本Spring Web MVC应用程序

时间:2015-06-29 16:06:43

标签: java xml spring maven spring-mvc

我的基本Spring MVC Web应用程序不起作用。我创建了Simple Spring Web Maven项目并删除了不必要的代码来简化它。然后,我添加了Controller实现,并在我最近阅读的书中注释了它。我从book和我刚刚创建的应用程序部署了一个示例应用程序。示例应用程序正常工作 - 我没有。当我尝试访问URL ... localhost ... / app-name / start / basic / show Tomcat Server显示404错误。这是我的代码:

spring.app包中的BasicController

@Controller
@RequestMapping("/basic")
public class BasicController{

    @RequestMapping("/show")
    public ModelAndView handleRequest(HttpServletRequest arg0,
            HttpServletResponse arg1) throws Exception {

        Map < String, String > modelData = new HashMap < String, String >();
        modelData.put("message", "Hello World!");

        return new ModelAndView("showMessage", modelData);
    }

}

App / src / main / webapp / WEB-INF / mvc-config.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.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">

    <mvc:annotation-driven />
    <context:component-scan base-package="spring.app"/>

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

</beans>

应用/ SRC /主/ web应用/ WEB-INF / web.xml中

<?xml version="1.0" encoding="ISO-8859-1"?>
<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_2_5.xsd"
         id="WebApp_ID" version="2.5">


    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/mvc-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/start/*</url-pattern>
    </servlet-mapping>

</web-app>

WEB-INF / view目录中的showMessage.jsp:

<!DOCTYPE html>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<html>
    <head>
        <meta charset="utf-8">
        <title>Welcome</title>
    </head> 
    <body>
        <h2>${message}</h2>
    </body>
</html>

我试图解决这个问题,但所有内容都像示例应用程序一样实现,所以我不知道它为什么会失败。

我的痕迹,当我选择“在服务器上运行”并且我只部署了这个应用程序时:

cze 29, 2015 6:44:27 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:App' did not find a matching property.
cze 29, 2015 6:44:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version:        Apache Tomcat/8.0.23
cze 29, 2015 6:44:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          May 19 2015 14:58:38 UTC
cze 29, 2015 6:44:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number:         8.0.23.0
cze 29, 2015 6:44:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Windows 8.1
cze 29, 2015 6:44:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            6.3
cze 29, 2015 6:44:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
cze 29, 2015 6:44:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             C:\Program Files\Java\jre1.8.0_45
cze 29, 2015 6:44:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.8.0_45-b15
cze 29, 2015 6:44:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
cze 29, 2015 6:44:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         C:\Users\Adrian\Desktop\apache-tomcat-8.0.23-windows-x64\apache-tomcat-8.0.23
cze 29, 2015 6:44:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         C:\Users\Adrian\Desktop\apache-tomcat-8.0.23-windows-x64\apache-tomcat-8.0.23
cze 29, 2015 6:44:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:60768
cze 29, 2015 6:44:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=C:\Users\Adrian\Desktop\apache-tomcat-8.0.23-windows-x64\apache-tomcat-8.0.23
cze 29, 2015 6:44:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=C:\Users\Adrian\Desktop\apache-tomcat-8.0.23-windows-x64\apache-tomcat-8.0.23
cze 29, 2015 6:44:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=C:\Users\Adrian\Desktop\apache-tomcat-8.0.23-windows-x64\apache-tomcat-8.0.23\wtpwebapps
cze 29, 2015 6:44:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=C:\Users\Adrian\Desktop\apache-tomcat-8.0.23-windows-x64\apache-tomcat-8.0.23\endorsed
cze 29, 2015 6:44:27 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1250
cze 29, 2015 6:44:27 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: C:\Program Files\Java\jre1.8.0_45\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre1.8.0_45/bin/server;C:/Program Files/Java/jre1.8.0_45/bin;C:/Program Files/Java/jre1.8.0_45/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\xampp\php;C:\Program Files\MySQL\MySQL Server 5.1\bin;C:\Program Files (x86)\MySQL\MySQL Server 5.1\bin;C:\Users\Adrian\Desktop\eclipse;;.
cze 29, 2015 6:44:27 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8081"]
cze 29, 2015 6:44:28 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
cze 29, 2015 6:44:28 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8010"]
cze 29, 2015 6:44:28 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
cze 29, 2015 6:44:28 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1648 ms
cze 29, 2015 6:44:28 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
cze 29, 2015 6:44:28 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.23
cze 29, 2015 6:44:28 PM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [125] milliseconds.
cze 29, 2015 6:44:28 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\Users\Adrian\Desktop\apache-tomcat-8.0.23-windows-x64\apache-tomcat-8.0.23\webapps\docs
cze 29, 2015 6:44:28 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory C:\Users\Adrian\Desktop\apache-tomcat-8.0.23-windows-x64\apache-tomcat-8.0.23\webapps\docs has finished in 63 ms
cze 29, 2015 6:44:28 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\Users\Adrian\Desktop\apache-tomcat-8.0.23-windows-x64\apache-tomcat-8.0.23\webapps\examples
cze 29, 2015 6:44:29 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized()
cze 29, 2015 6:44:29 PM org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized()
cze 29, 2015 6:44:29 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory C:\Users\Adrian\Desktop\apache-tomcat-8.0.23-windows-x64\apache-tomcat-8.0.23\webapps\examples has finished in 962 ms
cze 29, 2015 6:44:29 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\Users\Adrian\Desktop\apache-tomcat-8.0.23-windows-x64\apache-tomcat-8.0.23\webapps\host-manager
cze 29, 2015 6:44:29 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory C:\Users\Adrian\Desktop\apache-tomcat-8.0.23-windows-x64\apache-tomcat-8.0.23\webapps\host-manager has finished in 78 ms
cze 29, 2015 6:44:29 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\Users\Adrian\Desktop\apache-tomcat-8.0.23-windows-x64\apache-tomcat-8.0.23\webapps\manager
cze 29, 2015 6:44:30 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory C:\Users\Adrian\Desktop\apache-tomcat-8.0.23-windows-x64\apache-tomcat-8.0.23\webapps\manager has finished in 110 ms
cze 29, 2015 6:44:30 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\Users\Adrian\Desktop\apache-tomcat-8.0.23-windows-x64\apache-tomcat-8.0.23\webapps\ROOT
cze 29, 2015 6:44:30 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory C:\Users\Adrian\Desktop\apache-tomcat-8.0.23-windows-x64\apache-tomcat-8.0.23\webapps\ROOT has finished in 47 ms
cze 29, 2015 6:44:30 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8081"]
cze 29, 2015 6:44:30 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8010"]
cze 29, 2015 6:44:30 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 2149 ms

好吧,假设我通过创建Spring MVC Project而不是Simple Spring Web Maven Project来解决这个问题。现在一切正常。

1 个答案:

答案 0 :(得分:0)

根据您的日志

您的应用未获得部署。我从您的日志中获得了线索

INFO: Deploying

当您阅读INFO: Deploying

它没有说明你的应用程序是什么&#34; / App&#34;