2017-10-28 193 views
0

我的Spring版本是5.x,而tomcat版本是8.5,所以根據介紹,他們將支持在沒有web.xml的情況下運行的web應用程序,但是我得到了404錯誤,請參閱下面的項目結構:404錯誤的Spring MVC應用程序

enter image description here

我用這個URL來訪問我的應用程序,但得到404錯誤:
http://localhost:8080/SpringMVC/

見下面我的代碼:

個RootConfig.java:

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

} 

WebConfig.java:

package spittr.config; 

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.WebMvcConfigurer; 
import org.springframework.web.servlet.view.InternalResourceViewResolver; 


@Configuration 
@EnableWebMvc 
@ComponentScan("spittr.web") 
public class WebConfig implements WebMvcConfigurer{ 

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

SpittrWebAppInitializer.java:

package 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[] {"/"}; 
    } 

} 

HomeController.java:

package spittr.web; 

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() 
    { 
     System.out.println("test"); 
     return "home"; 
    } 
} 

回到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>this is the Home Jsp page</title> 
</head> 
<body> 
    <h1>Hello, Spring MVC!</h1> 
</body> 
</html> 

從Eclipse控制檯,我可以看到輸出的「測試」,這意味着Spring上下文找出控制器,但似乎無法找到jsp頁面,我不知道原因,有人能告訴我我的代碼有什麼問題嗎?

+0

嘗試加入公共的ViewResolver視圖解析器()方法的頂部@Bean註解。 –

+0

非常感謝,先生。現在正在工作。 – user2575502

+0

不客氣。 –

回答

1

嘗試在WebConfig#viewResolver()方法的頂部添加@Bean註釋。所以,Spring容器管理你的方法爲bean,你的自定義配置可能會起作用。

@Bean 
public ViewResolver viewResolver(){} 

Indicates that a method produces a bean to be managed by the Spring container.