Spring MVC中的简介程序(使用java配置而不使用Maven)无法按预期工作

时间:2017-02-01 17:40:41

标签: spring-mvc

我是Spring MVC的新手。我指的是Craig Walls的第四版“Spring in Action”一书。我正在使用Eclipse IDE来练习该书中给出的程序。我的第一个示例程序没有工作,尽管我已经完成了本书中提到的所有内容。请帮忙解决问题。下面给出了我正在使用的代码。如果有人需要任何其他信息,请告诉我。

我正在使用Eclipse中集成的Tomcat v8.0,它开始没有任何问题。

Project Folder structure in Eclipse

我的DispatcherServlet如下:

package spittr.config;

import org.springframework.web.servlet.support.
AbstractAnnotationConfigDispatcherServletInitializer;

public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[] { RootConfig.class };
    }
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] { WebConfig.class };
    }
}

我的WebConfig如下:

package spittr.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.
DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan("spittr.web")
public class WebConfig
extends WebMvcConfigurerAdapter {
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver =
                new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }
    @Override
    public void configureDefaultServletHandling(
            DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}

我的RootConfig如下:

package spittr.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@ComponentScan(basePackages={"spittr"},
excludeFilters={
        @Filter(type=FilterType.ANNOTATION, value=EnableWebMvc.class)
})
public class RootConfig {
}

我的控制器代码如下:

package spittr.web;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

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

@Controller
public class HomeController {
    @RequestMapping(value="/", method=GET)
    public String home() {
        return "home";
    }
}

我的JSP主页是(FromBook / WebContent / WEB-INF / views / home.jsp):

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Introduction to Spring MVC</title>
</head>
<body>
<h1>Welcome to Spring MVC</h1>
</body>
</html>

控制台中的服务器输出如下:

Feb 01, 2017 12:24:43 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:FromBook' did not find a matching property.
Feb 01, 2017 12:24:43 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version:        Apache Tomcat/8.0.28
Feb 01, 2017 12:24:43 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Oct 7 2015 18:25:21 UTC
Feb 01, 2017 12:24:43 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number:         8.0.28.0
Feb 01, 2017 12:24:43 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Windows 8.1
Feb 01, 2017 12:24:43 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            6.3
Feb 01, 2017 12:24:43 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
Feb 01, 2017 12:24:43 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             C:\Program Files\Java\jre1.8.0_121
Feb 01, 2017 12:24:43 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.8.0_121-b13
Feb 01, 2017 12:24:43 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Feb 01, 2017 12:24:43 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         E:\J2EE_workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Feb 01, 2017 12:24:43 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         E:\Tomcat\apache-tomcat-8.0.28
Feb 01, 2017 12:24:43 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=E:\J2EE_workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Feb 01, 2017 12:24:43 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=E:\Tomcat\apache-tomcat-8.0.28
Feb 01, 2017 12:24:43 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=E:\J2EE_workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
Feb 01, 2017 12:24:43 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=E:\Tomcat\apache-tomcat-8.0.28\endorsed
Feb 01, 2017 12:24:43 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
Feb 01, 2017 12:24:43 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_121\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre1.8.0_121/bin/server;C:/Program Files/Java/jre1.8.0_121/bin;C:/Program Files/Java/jre1.8.0_121/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\;C:\Program Files (x86)\Microsoft SQL Server\100\DTS\Binn\;E:\Eclipse for J2EE\eclipse;;.
Feb 01, 2017 12:24:43 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Feb 01, 2017 12:24:43 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Feb 01, 2017 12:24:43 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Feb 01, 2017 12:24:43 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Feb 01, 2017 12:24:43 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 616 ms
Feb 01, 2017 12:24:43 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Feb 01, 2017 12:24:43 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.28
Feb 01, 2017 12:24:43 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Feb 01, 2017 12:24:43 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Feb 01, 2017 12:24:43 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 295 ms

浏览器输出如下:

Browser Output

1 个答案:

答案 0 :(得分:0)

我找到了上述问题的解决方案。 代码很好,没有问题。

以前,我没有使用maven build。 使用maven(如下所示的pom.xml)之后,它运行正常。

<properties>
    <spring.version>4.0.2.RELEASE</spring.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<dependencies>

    <!-- Spring -->

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <!-- JavaEE APIs -->

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
    </dependency>

</dependencies>

<build>
    <plugins>

        <!-- Compiler -->

        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>

        <!-- WAR -->

        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>

        <!-- Jetty -->

        <plugin>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>9.0.6.v20130930</version>
            <configuration>
                <httpConnector>
                    <port>8080</port>
                    <host>localhost</host>
                </httpConnector>
                <scanIntervalSeconds>10</scanIntervalSeconds>
            </configuration>
        </plugin>
    </plugins>
</build>

`