我是Spring MVC中基于Java的配置新手。我正在尝试构建一个简单的mvc应用程序。但是当我运行应用程序时,我收到404错误。
请查看下面的代码
AppInitializer.java
public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { WebConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
}
WebConfig.java
@Configuration
@EnableWebMvc
@ComponentScan({"com.rajumandala.securemvc"})
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
ElloController.java
@Controller
@RequestMapping("/")
public class ElloController {
public ElloController() {
System.out.println("Ello Controller");
}
@RequestMapping(method=RequestMethod.GET)
public String ello(ModelMap model){
System.out.println("in ello() method");
model.addAttribute("greeting", "Hello Raju");
return "ello";
}
}
ello.jsp
Greeting : ${greeting}
我没有理解为什么当我点击http://localhost:8285/securemvc/
以下是我的项目目录结构
也许这对你们来说是一个愚蠢的问题。但是我在这里发布之前尝试了很多。任何帮助将不胜感激。