WebApplicationInitializer没有调用正确的“DispatcherServlet”,总是返回404

时间:2014-03-03 14:44:20

标签: spring model-view-controller frameworks dispatcher

我从使用xml配置的spring spring MVC应用程序转换到使用spring boot的java配置,我面临以下行为。无论我调用什么jsp页面,我总是得到404错误。控制器正在被击中,因为我可以在控制台上看到来自控制器打印机的sysout消息,但是没有显示视图。我已经明确地将调度程序映射设置为“/”而不是“/ *”,但它仍然无效。

我也看到并且不知道输出是否正常 “在名为'dispatcherServlet'的DispatcherServlet中找不到带有URI [/WEB-INF/jsp/profile6.jsp]的HTTP请求的映射”

通常我的意思是我已经注册了一个名为“dispatcher”而不是“dispatcherServlet”的调度程序。对我来说,似乎调度程序没有正确注册,因为onStartup中的sysout“STARTING THE CONFIGURATION”也没有打印。然后有一些“dispatcherServlet”能够从控制器打印出sysout,但视图没有找到。

这是弹簧启动主要功能:

@ComponentScan("org.syncServer")
@Configuration
@EnableWebMvc
@EnableAutoConfiguration
public class Application {

public static void main(String[] args) {

ApplicationContext ctx = SpringApplication.run(Application.class, args);

System.out.println("PRINTING the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}

}
}

以下是按照maunual扩展WebApplicationInitializer类的Initilizer:

public class Initializer implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext servletContext)
throws ServletException {
System.out.println("STARTING THE CONFIGURATION");
AnnotationConfigWebApplicationContext mvcContext = new AnnotationConfigWebApplicationContext();
mvcContext.register(MVCConfig.class);
mvcContext.refresh();

// Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher = servletContext.addServlet(
"dispatcher", new DispatcherServlet(mvcContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}

MVC背景:

@Configuration
@ComponentScan(basePackages = "org.syncServer")
@EnableWebMvc
public class MVCConfig extends WebMvcConfigurerAdapter{

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/pdfs/**").addResourceLocations("/pdfs/");
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
 }


@Bean
public InternalResourceViewResolver internalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
//resolver.setOrder(2);
resolver.setViewClass(JstlView.class);
return resolver;
}
}

我的控制器:

@Controller
public class ProfileController {

@RequestMapping(value = "/profile6", method=RequestMethod.GET)
public String profile6 (){
System.out.println("profile hellow6 has been hit");

return "profile6";

}

profile6查看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>this is the greeting jsp profile6 </title>
</head>
<body>

<h1>Profile6</h1>

使用tomcat 8.3的弹簧启动输出

. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.0.0.RC3)

.......
.......
2014-02-24 15:23:11.644 INFO 6038 --- [ main] .t.TomcatEmbeddedServletContainerFactory : Server initialized with port: 8888
2014-02-24 15:23:12.028 INFO 6038 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2014-02-24 15:23:12.029 INFO 6038 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.3
2014-02-24 15:23:12.171 INFO 6038 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2014-02-24 15:23:12.172 INFO 6038 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 4099 ms
2014-02-24 15:23:12.809 INFO 6038 --- [ main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2014-02-24 15:23:18.749 INFO 6038 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/get],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String org.syncServer.Controller.REST.Profile.ProfileCont roller.create(org.springframework.ui.Model,org.syn cServer.Model.profile.Profile,org.springframework. validation.BindingResult)
2014-02-24 15:23:18.749 INFO 6038 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/profile6],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String org.syncServer.Controller.REST.Profile.ProfileCont roller.profile6()
2014-02-24 15:23:18.749 INFO 6038 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/create],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.syncServer.Model.profile.Profile org.syncServer.Controller.REST.Profile.ProfileCont roller.createProfile(java.lang.String,java.lang.St ring)
2014-02-24 15:23:18.750 INFO 6038 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/delete],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.util.List<org.syncServer.Model.profile.Profil e> org.syncServer.Controller.REST.Profile.ProfileCont roller.viewjson(org.springframework.ui.Model)
2014-02-24 15:23:18.750 INFO 6038 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello5],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String org.syncServer.Controller.REST.Profile.ProfileCont roller.hello5()
2014-02-24 15:23:18.750 INFO 6038 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String org.syncServer.Controller.REST.Index.profile6()
2014-02-24 15:23:18.788 INFO 6038 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/pdfs/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceH ttpRequestHandler]
2014-02-24 15:23:18.789 INFO 6038 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/resources/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceH ttpRequestHandler]
2014-02-24 15:23:19.372 INFO 6038 --- [ main] o.s.o.h.HibernateTransactionManager : Using DataSource [org.apache.commons.dbcp.BasicDataSource@1501e528] of Hibernate SessionFactory for HibernateTransactionManager
2014-02-24 15:23:19.448 INFO 6038 --- [ main] o.s.s.c.ThreadPoolTaskScheduler : Initializing ExecutorService 'defaultSockJsTaskScheduler'
2014-02-24 15:23:19.461 WARN 6038 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Neither 'urlMap' nor 'mappings' set on SimpleUrlHandlerMapping
2014-02-24 15:23:19.614 INFO 6038 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2014-02-24 15:23:19.818 DEBUG 6038 --- [ main] ationConfigEmbeddedWebApplicationContext : Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecyc leProcessor@55d1c106]
2014-02-24 15:23:19.823 DEBUG 6038 --- [ main] utoConfigurationReportLoggingInitializer :



2014-02-24 15:23:34.702 INFO 6038 --- [nio-8888-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2014-02-24 15:23:34.703 INFO 6038 --- [nio-8888-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2014-02-24 15:23:34.725 INFO 6038 --- [nio-8888-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 22 ms
INDEX INDEX has been hit
2014-02-24 15:23:34.853 WARN 6038 --- [nio-8888-exec-1] o.s.web.servlet.PageNotFound : No mapping found for HTTP request with URI [/WEB-INF/jsp/index.html.jsp] in DispatcherServlet with name 'dispatcherServlet'
2014-02-24 15:23:34.980 WARN 6038 --- [nio-8888-exec-2] o.s.web.servlet.PageNotFound : No mapping found for HTTP request with URI [/favicon.ico] in DispatcherServlet with name 'dispatcherServlet'
2014-02-24 15:23:34.986 WARN 6038 --- [nio-8888-exec-3] o.s.web.servlet.PageNotFound : No mapping found for HTTP request with URI [/favicon.ico] in DispatcherServlet with name 'dispatcherServlet'
profile hellow6 has been hit
2014-02-24 15:23:38.423 WARN 6038 --- [nio-8888-exec-4] o.s.web.servlet.PageNotFound : No mapping found for HTTP request with URI [/WEB-INF/jsp/profile6.jsp] in DispatcherServlet with name 'dispatcherServlet'

和pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!--
        The link to the hibernate-validator parent POM only exists out of convenience since we are creating
        this archetype from an existing module of the Hibernate Validator code base.
        If you want to know more about this process check the maven-archetype-plugin documentation
        http://maven.apache.org/archetype/maven-archetype-plugin, epecially the goal create-from-project

        If you want to use this quick-start project as the base for your own application you should remove the
        <parent> entry. See also the other pointers in this pom.
    -->
    <parent>
            <artifactId>spring-boot-starter-parent</artifactId>
            <groupId>org.springframework.boot</groupId>
            <version>1.0.0.RC3</version>
    </parent>


    <groupId>sync</groupId>
    <artifactId>syncServer</artifactId>
    <name>syncServer</name>

    <!-- When removing the parent project configuration you have to explicitly set the dependencies version -->
    <dependencies>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.0.Final</version>
        </dependency>
        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.1-901.jdbc4</version>
        </dependency>



        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
        </dependency>

        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>
        <dependency>
            <groupId>com.thoughtworks.xstream</groupId>
            <artifactId>xstream</artifactId>
            <version>1.4.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-messaging</artifactId>
            </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-core</artifactId>
            <version>${tomcat8.version}</version>
        </dependency>
    </dependencies>

    <version>1.0-SNAPSHOT</version>

    <build>
        <defaultGoal>test</defaultGoal>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>

            </plugin>

            <plugin>
                <artifactId>maven-deploy-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
                <version>6.1.10</version>
                <configuration>
                    <stopPort>9966</stopPort>
                    <stopKey>foo</stopKey>
                      <scanIntervalSeconds>10</scanIntervalSeconds>
                </configuration>
            </plugin>


            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>          
                <configuration>
<compilerArgument>-Xlint:unchecked</compilerArgument>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>



        </plugins>
    </build>

    <profiles>
        <!-- When removing the parent project configuration this profile can be removed -->
        <profile>
            <id>archetype</id>
            <activation>
                <file>
                    <exists>archetype.properties</exists>
                </file>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-archetype-plugin</artifactId>
                    </plugin>
                       <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

    <repositories>
        <repository>
            <id>jboss-public-repository</id>
            <url>https://repository.jboss.org/nexus/content/repositories/public</url>
        </repository>

        <repository>
            <id>springsource-repo</id>
            <name>SpringSource Repository</name>
            <url>http://repo.springsource.org/release</url>
        </repository>

        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>http://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>

        <repository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/libs-snapshot</url>
            <snapshots><enabled>true</enabled></snapshots>
        </repository>

    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/libs-snapshot</url>
            <snapshots><enabled>true</enabled></snapshots>
        </pluginRepository>
    </pluginRepositories>

    <properties>
        <java-version>1.7</java-version>
        <spring-version>4.0.1.RELEASE</spring-version>
        <org.aspectj-version>1.6.10</org.aspectj-version>
        <org.slf4j-version>1.6.6</org.slf4j-version>
        <start-class>org.syncServer.core.Application</start-class>
        <springBootVersion>1.0.0</springBootVersion>
        <tomcat8.version>8.0.3</tomcat8.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <packaging>jar</packaging>



</project>

0 个答案:

没有答案