2017-01-16 142 views
0

所以,我試圖學習Spring框架,但目前我被困在這個問題上,出於某種原因,我的映射不起作用,當我嘗試訪問該地址時,我得到一個404錯誤。Spring @RequestMapping,404錯誤

有人可以澄清我做錯了什麼(都在春季班的使用和我上面描述的問題的來源)?

我使用本視頻中顯示的示例配置spring:YouTube Video,代碼基本相同,唯一的區別是包名和JSP文件的位置(這可能是我的問題的根源?) 。

該項目於IntelliJ IDEA的製作,並連接到下面的鏈接(谷歌驅動器):Project Link

正如我的servlet容器我使用Tomcat8(試用過9和8.5,沒有雪茄)。

HomeController.java

package com.demo.spring.controller; 

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

package com.demo.spring.controller; 

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

/** 
* Created by ARTERC on 1/16/2017. 
*/ 
@Controller 
public class HomeController { 
    @RequestMapping("/") 
    public String home() { 
     return "home"; 
    } 
} 

WebInit.java

package com.demo.spring.config; 

import org.springframework.context.annotation.Configuration; 
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; 

@Configuration 
public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer { 

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

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

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

WebConfig.java

package com.demo.spring.config; 

import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
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.demo.spring") 
public class WebConfig extends WebMvcConfigurerAdapter{ 

    @Bean 
    public InternalResourceViewResolver resolver() { 
     InternalResourceViewResolver resolver = new InternalResourceViewResolver(); 
     resolver.setPrefix("/"); 
     resolver.setSuffix(".jsp"); 
     return resolver; 
    } 

} 

home.jsp path:/web/home.jsp

+0

你應該把你的代碼在這裏。 – viruskimera

+0

哎呀,對不起,忘了。完成 –

+0

因此,我忘了在我的pom.xml中添加 war,但它仍然沒有解決我的問題,它確實映射了url,但仍然收到404錯誤。它說HTTP狀態404 -/home.jsp所以由於某種原因,它無法找到我的看法又名home.jsp –

回答

0

好的,所以我找到了解決方案。

  1. 加入戰爭插件的pom.xml
  2. 修復網絡資源的目錄路徑(Web應用程序是)Image Link
+0

如何添加戰爭插件:https://maven.apache.org/plugins/maven-war-plugin/usage。 HTML –