简体   繁体   English

URL映射未触发控制器

[英]URL mapping not triggering controller

I am following along in the book Spring in Action trying to build the Spittr application. 我在《 Spring in Action》一书中一直在尝试构建Spittr应用程序。 I created my configuration classes and home controller, but when I call localhost:8080/ I get a 404. 我创建了配置类和家庭控制器,但是当我调用localhost:8080 /时,我得到了404。


Project 项目

在此处输入图片说明


SpittrWebAppInitializer.java SpittrWebAppInitializer.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();
    }

}


WebConfig.java 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 RootConfig.java

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


HomeController.java 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 (I removed the config that eclipse put in here by default when making a new Spring Web MVC projectbecause it was complaining about multiple context listeners) 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> 


I have tried navigating to http://localhost:8080/Spittr/ , http://localhost:8080/ , and http://localhost:8080 . 我尝试导航到http://localhost:8080/Spittr/http://localhost:8080/http://localhost:8080 All result in a 404. No exceptions show up in the Java console though, so it's like its not even registering the request. 所有结果都为404。不过,Java控制台中不会显示任何异常,因此就好像它没有注册请求一样。
This is a part of what the console prints when I run my application 这是我运行应用程序时控制台打印内容的一部分

 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 


What do I need to do to get my application running? 我该怎么做才能使我的应用程序运行? Thanks! 谢谢!

I didn't post my pom.xml because I didn't think it was relevant. 我没有发布pom.xml,因为我认为这不相关。 Apparently it was. 显然是。

The culprit that was this line <version>1.0.0-BUILD-SNAPSHOT</version> 此行的罪魁祸首<version>1.0.0-BUILD-SNAPSHOT</version>

Made the resulting war file have that version appended to the name, which meant it had to be at the end of the url I use to access the application. 使生成的war文件在名称后附加该版本,这意味着它必须位于我用来访问该应用程序的url的末尾。

The solution was to add this to the <build> section of pom.xml 解决方案是将其添加到pom.xml的<build>部分中

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

now the resulting war was simply named spittr.war and I was able to access the application at localhost:8080/spittr 现在产生的战争被简单地命名为spittr.war ,我可以在localhost:8080/spittr上访问该应用程序

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

This indicates the entry has been registered and it should be triggered. 这表明该条目已被注册并且应该被触发。

There is one another method where a welcome file can be triggered using below. 还有一种方法可以使用下面的方法触发欢迎文件。 Add this to your WebConfig which adds home.jsp as your welcome file. 将此添加到您的WebConfig中,它将home.jsp添加为您的欢迎文件。

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

If this works, then try with other endpoints as suggested, so that gives the base to access to application. 如果可行,请按照建议尝试其他端点,以便为访问应用程序奠定基础。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM