最近我正在学习基于spring java的配置。我试图用WebConfig和替换web.xml WebApplicationInitializer。Spring WebApplicationInitializer
每当我要求的网址:http://localhost:8080/spring-demo/greeting.html我得到404说明请求的资源不可用。 以下是我的项目详情。
WebConfig.java
package com.soumya.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.soumya.spring")
public class WebConfig {
}
WebAppInitializer.java
控制器
package com.soumya.spring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping(value = "/greeting")
public String greeting(Model model) {
model.addAttribute("greeting", "Hello World!");
return "greeting.jsp";
}
}
项目结构图片
看看你的配置,我会说使用正确的URL是http:// localhost:8080/spring-demo/greeting 删除最后的.html –
你使用的是什么版本的servlet api?它必须比(包括)3.0.0更新。记录什么?通常您可以在启动时看到请求映射。 – hotzst
@soumya,你可以请你发布你的日志文件? –