2016-09-21 122 views
0

我在書中的Spring in Action試圖構建Spittr應用程序。我建立了我的配置類和家居控制器,但是當我打電話本地主機:8080 /,我收到了404 URL映射不觸發控制器


項目

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", 
    [email protected](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 (我刪除了做一個新的春天當Eclipse擺在這裏默認的配置Web MVC項目,因爲它抱怨多個上下文聽衆)

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


我試圖導航到​​3210,​​和 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 


什麼我需要做的就是我的應用程序運行?謝謝!

+1

來訪問應用程序如果使用localhost:8080給你404,我的猜測是,有一些錯誤的服務器 –

+1

您是否嘗試過不同的終點?例如:[login](http:// localhost:8080/login)或[registration](http:// localhost:8080/registration)?看看'HomeController'還有更多。我剛剛克隆了存儲庫,在我的IDE中設置了該項目,並按預期工作。 –

+0

@DanielBubenheim我還沒有發展到這麼遠,但 – zero01alpha

回答

0

我沒有發佈我的pom.xml,因爲我不認爲它是相關的。顯然是這樣。

,這是該行 <version>1.0.0-BUILD-SNAPSHOT</version>

方面取得的戰爭文件的罪魁禍首有一個版本附加到名稱,這意味着它必須是在我使用它來訪問應用程序的URL的結尾。

解決的辦法是將此添加到pom的<build>部分。XML

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

現在所產生的戰爭是簡單地命名爲spittr.war,我能夠在localhost:8080/spittr

-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"); 
    } 

如果這樣做,然後嘗試與建議的其他端點,以便爲基礎訪問應用程序。

+0

這不適合我 – zero01alpha