基于Java配置的SpringMvc项目
使用Java配置,Spring应该在3.0以上版本,tomcat7.0以上
1、 配置DispatcherServlet
使用Java配置将DispatcherServlet 放在servlet容器中
SpittrWebAppInitializer .java
package spittr.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
/**
* Created by 杨Sir on 2017/10/11.
* 配置 DispatcherServlet, 要继承 AbstractAnnotationConfigDispatcherServletInitializer
* 启动时创建Spring上下文,并加载配置类或配置文件
*/
public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
/**
* getRootConfigClasses()方法返回的带有@Configuration注解的类将 会用来配置ContextLoaderListener创建的应用上下文中的bean。 。
* @return
*/
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[]{ RootConfig.class};
}
/**
* 设置加载应用上下文时,使用配置类WebConfig.class
* 该方法返回的带有@Configuration注解的 类将会用来定义DispatcherServlet应用上下文中的bean。
*/
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[]{ WebConfig.class}; //指定配置类
}
/**
* 将路径映射到 DispatcherServlet',"/"表示处理所有请求
* @return
*/
@Override
protected String[] getServletMappings() {
return new String[]{"/"}; //将DispatcherServlet 映射到 "/"
}
}
2、 启动SpringMvc
package spittr.config;
import org.springframework.context.annotation.Bean;
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.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
/**
* Created by 杨Sir on 2017/10/11.
*/
@Configuration
@EnableWebMvc //启用SpringMvc
@ComponentScan("spitter.web") //启用注解扫描
public class WebConfig extends WebMvcConfigurerAdapter {
//配置 JSP视图解析器
@Bean
public ViewResolver viewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setPrefix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
}
/**
* 配置静态资源的处理
*/
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
3、 添加非web组件的启动类
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;
/**
* Created by 杨Sir on 2017/10/11.
* 此处使用了 @ComponentScan 注解,因此可以用很多非 WEB组件来完善 RootConfig
*/
@Configuration
@ComponentScan(basePackages = {"spitter"},
excludeFilters = {
@Filter(type = FilterType.ANNOTATION,value = EnableWebMvc.class)})
public class RootConfig {
}
4、配置一个Controller
package spitter.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* Created by 杨Sir on 2017/10/11.
*/
@RequestMapping("/") //定义类级别的请求处理,该路径加到该控制器所有方法指定的路径之前
@Controller //声明为一个控制器
public class HomeController {
@RequestMapping(method = RequestMethod.GET) //处理对"/"的 GET请求
public String home(){
return "home"; //视图名叫home
}
/**
* @RequestMapping(value={"/add","/save"}):可以处理多个请求
* @return
*/
@RequestMapping(value={"/add","/save"}, method = RequestMethod.GET) //对类级别请求处理做补充,
public String action(){
return "action";
}
}
5、 测试homeController,以控制器的角度
package spitter.web.test;
import org.junit.Test;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import spitter.web.controller.HomeController;
import static org.junit.Assert.assertEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
/**
* Created by 杨Sir on 2017/10/11.
*/
public class HomeControllerTest {
/**
* 测试home()中会发生什么
*/
@Test
public void homePageTest() throws Exception{
HomeController controller = new HomeController();
assertEquals("home",controller.home()); // 断言返回 "hello"
}
/**
* 测试 home方法:
* 站在控制器的角度测试 springMvc控制器,
* spring 包含了一种mock Springmvc并针对控制器执行 HTTP请求的机制,不用再启动web服务器和web浏览器
*
* 该测试需要加入 javax.servlet-api.jar
*/
@Test
public void homePageTest2() throws Exception{
HomeController controller = new HomeController();
//搭建 MockMvc
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
mockMvc.perform(get("/")) // 对 "/" 执行GET请求,
.andExpect(view().name("home")); //预期得到 home视图
}
/**
* 测试action方法
* @throws Exception
*/
@Test
public void homePageTest3() throws Exception{
HomeController controller = new HomeController();
//搭建 MockMvc
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
mockMvc.perform(get("/add")) // 对 "/" 执行GET请求,
.andExpect(view().name("action")); //预期得到 home视图
}
@Test
public void homePageTest4() throws Exception{
HomeController controller = new HomeController();
//搭建 MockMvc
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
mockMvc.perform(get("/save")) // 对 "/" 执行GET请求,
.andExpect(view().name("action")); //预期得到 home视图
}
}
6、 web.xml 不用配置
7、完整目录结构