URL映射不触发控制器

时间:2016-09-21 18:50:35

标签: java spring spring-mvc

我正在阅读Spring in Action一书,试图构建Spittr应用程序。我创建了我的配置类和家庭控制器,但是当我调用localhost时:8080 /我得到了404。

<小时/> 项目

enter image description here

<小时/> SpittrWebAppInitializer.java

package com.hamerm.spittr.config;

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

public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[] {RootConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] {WebConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] {"/"};
    }

}

<小时/> WebConfig.java

package com.hamerm.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("com.hamerm.spittr.web.controllers")
public class WebConfig extends WebMvcConfigurerAdapter {

    @Bean
    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.java

package com.hamerm.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="com.hamerm.spittr", 
    excludeFilters=@Filter(type=FilterType.ANNOTATION, value=EnableWebMvc.class))
public class RootConfig {

}

<小时/> HomeController.java

package com.hamerm.spittr.web.controllers;

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

@Controller
public class HomeController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home() {

        return "home";
    }

}

<小时/> web.xml中 (我在制作一个新的Spring Web MVC项目时默认删除了eclipse放在这里的配置,因为它抱怨多个上下文监听器)

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


</web-app>

<小时/> 我尝试导航到http://localhost:8080/Spittr/http://localhost:8080/http://localhost:8080。所有结果都是404.但是,Java控制台中没有例外,因此它甚至没有注册请求。 这是我运行应用程序

时控制台打印的一部分
INFO: 1 Spring WebApplicationInitializers detected on classpath
Sep 21, 2016 2:21:43 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization started
INFO : org.springframework.web.context.support.AnnotationConfigWebApplicationContext - Refreshing Root WebApplicationContext: startup date [Wed Sep 21 14:21:43 EDT 2016]; root of context hierarchy
INFO : org.springframework.web.context.support.AnnotationConfigWebApplicationContext - Registering annotated classes: [class com.hamerm.spittr.config.RootConfig]
INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 2062 ms
Sep 21, 2016 2:21:45 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcher'
INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'dispatcher': initialization started
INFO : org.springframework.web.context.support.AnnotationConfigWebApplicationContext - Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Wed Sep 21 14:21:45 EDT 2016]; parent: Root WebApplicationContext
INFO : org.springframework.web.context.support.AnnotationConfigWebApplicationContext - Registering annotated classes: [class com.hamerm.spittr.config.WebConfig]
INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/],methods=[GET]}" onto public java.lang.String com.hamerm.spittr.web.controllers.HomeController.home()
INFO : org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler]
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter - Looking for @ControllerAdvice: WebApplicationContext for namespace 'dispatcher-servlet': startup date [Wed Sep 21 14:21:45 EDT 2016]; parent: Root WebApplicationContext
INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'dispatcher': initialization completed in 1649 ms
Sep 21, 2016 2:21:46 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Sep 21, 2016 2:21:46 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Sep 21, 2016 2:21:46 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 9745 ms

<小时/> 我需要做什么才能让我的应用程序运行?谢谢!

2 个答案:

答案 0 :(得分:0)

我没有发布我的pom.xml,因为我认为它不相关。显然是。

这条线的罪魁祸首 <version>1.0.0-BUILD-SNAPSHOT</version>

将生成的war文件附加到名称后面,这意味着它必须位于我用来访问应用程序的URL的末尾。

解决方案是将其添加到pom.xml的<build>部分

<finalName>${project.artifactId}</finalName>

现在由此产生的战争名为spittr.war,我可以在localhost:8080/spittr

访问该应用程序

答案 1 :(得分:-1)

INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/],methods=[GET]}" onto public java.lang.String com.hamerm.spittr.web.controllers.HomeController.home()

这表示该条目已经注册,应该被触发。

还有另一种方法可以使用下面的方法触发欢迎文件。 将此添加到您的WebConfig,其中添加home.jsp作为您的欢迎文件。

  @Override
  public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/").setViewName("home");
  }

如果这样做,那么按照建议尝试使用其他端点,这样就可以访问应用程序。