在IDEA中搭建最小可用SpringMVC项目(纯Java配置)
1.新建SpringMVC的Web项目
- File-New-Project..
- 勾选SpringMVC和WebApplication,点击Next
- 填写Projectname:hello
- 点击Finish
- IDEA会自动下载所需的SpringMVC的jar包
2.代码编写
代码参考《Spring实战》(第四版),本文和书中代码略有差异
删除不需要的配置文件
- 删除WEB-INF下的web.xml
- 删除WEB-INF下的dispatcher-servlet.xml
- 删除WEB-INF下的applicationContext.xml
- 删除web下的index.jsp
编写JavaConfig文件
- 新建package:com.yangrd.springmvc.config
- 新建配置文件HelloWebAppInitializer.java
packagecom.yangrd.springmvc.config;
importorg.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
publicclassHelloWebAppInitializerextendsAbstractAnnotationConfigDispatcherServletInitializer{
@Override
protectedClass>[]getRootConfigClasses(){
System.out.println("getRootConfig");
returnnewClass>[]{RootConfig.class};
}
@Override
protectedClass>[]getServletConfigClasses(){
System.out.println("getServletConfig");
returnnewClass>[]{WebConfig.class};
}
@Override
protectedString[]getServletMappings(){
System.out.println("getServletMappings");
returnnewString[]{"/"};
}
}
新建配置文件RootConfig.java
packagecom.yangrd.springmvc.config;
importorg.springframework.context.annotation.ComponentScan;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.context.annotation.FilterType;
importorg.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@ComponentScan(basePackages={"com.yangrd.springmvc"},
excludeFilters={@ComponentScan.Filter(type=FilterType.ANNOTATION,value=EnableWebMvc.class)})
publicclassRootConfig{
}
新建配置文件WebConfig.java
packagecom.yangrd.springmvc.config;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.ComponentScan;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.web.servlet.ViewResolver;
importorg.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
importorg.springframework.web.servlet.config.annotation.EnableWebMvc;
importorg.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
importorg.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan("com.yangrd.springmvc.controller")
publicclassWebConfigextendsWebMvcConfigurerAdapter{
@Bean
publicViewResolverviewResolver(){
InternalResourceViewResolverresolver=newInternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".html");
resolver.setExposeContextBeansAsAttributes(true);
returnresolver;
}
@Override
publicvoidconfigureDefaultServletHandling(DefaultServletHandlerConfigurerconfigurer){
configurer.enable();
}
}
编写Controller
- 新建package:com.yangrd.springmvc.controller
- 新建文件HelloController.java
packagecom.yangrd.springmvc.controller;
importorg.springframework.stereotype.Controller;
importorg.springframework.web.bind.annotation.RequestMapping;
importstaticorg.springframework.web.bind.annotation.RequestMethod.GET;
/**
*
*/
@Controller//声明为一个控制器
publicclassHelloController{
@RequestMapping(value="/home",method=GET)//处理对“/”的GET请求
publicStringhello(){
return"hello";//逻辑视图名为hello
}
}
编写view文件
- 在WEB-INF下新建文件夹views
- 在views文件夹下新建hello.html
Hello helloworld