目录结构

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<!-- Configure ContextLoaderListener to use AnnotationConfigWebApplicationContext
instead of the default XmlWebApplicationContext -->
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param> <!-- Configuration locations must consist of one or more comma- or space-delimited
fully-qualified @Configuration classes. Fully-qualified packages may also be
specified for component-scanning -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>springdemo.config.AppConfig</param-value>
</context-param> <!-- Bootstrap the root application context as usual using ContextLoaderListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- Declare a Spring MVC DispatcherServlet as usual -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- Configure DispatcherServlet to use AnnotationConfigWebApplicationContext
instead of the default XmlWebApplicationContext -->
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<!-- Again, config locations must consist of one or more comma- or space-delimited
and fully-qualified @Configuration classes -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>springdemo.config.MvcConfig</param-value>
</init-param>
</servlet> <!-- map all requests for /app/* to the dispatcher servlet -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

SpringMVC配置类:

@EnableWebMvc
@Configuration
@ComponentScan(basePackages={"springdemo.controller"})
public class MvcConfig { /**
* 配置JSP视图解析器
* @return
*/
@Bean
public ViewResolver viewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
} /**
* 配置默认的Servlet来处理静态资源
* @return
*/
@Bean
public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
return new WebMvcConfigurerAdapter() {
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
};
}
}

Controller:

@Controller
public class CoffeeController {
@RequestMapping(value="/show",method=RequestMethod.GET)
public String showCoffee(){
return "coffee";
}
}

测试类:

@RunWith(SpringRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes={MvcConfig.class})
public class CoffeeTest {
private MockMvc mockMvc; @Autowired
private WebApplicationContext wac; @Before
public void init(){
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
} @Test
public void testShowCoffee() throws Exception{
ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders.get("/show"));
resultActions.andExpect(MockMvcResultMatchers.view().name("coffee"));
}
}

可以选择继承AbstractAnnotationConfigDispatcherServletInitializer来替换web.xml中的配置:

public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{

	@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[]{AppConfig.class};
} @Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[]{MvcConfig.class};
} @Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}

pom.xml :Spring pom.xml配置

最新文章

  1. PhpStorm 快捷键大全 PhpStorm 常用快捷键和配置
  2. android view : 动画
  3. 关于databinding的细节
  4. Emit学习(1) - HelloWorld
  5. 。Java注意事项
  6. 设计模式-&gt;观察者模式
  7. UnityShader之Shader格式篇【Shader资料1】
  8. MFC 颜色选择对话框、颜色按钮
  9. CentOS 7 / RHEL 7 上安装 LAMP + phpMyAdmin
  10. [LeetCode] Rank Scores -- 数据库知识(mysql)
  11. css小demo
  12. 通过全备+binlog_server同步恢复被drop的库或表
  13. 4.2计算字符的ASCII碼
  14. 三重DEC加密在java中的实现
  15. 零基础学习python_模块(50-52课)
  16. 在Docker Swarm上部署Apache Storm:第1部分
  17. tms mqtt
  18. vim去除行显示;vim全部复制命令
  19. UDP与TCP笔记
  20. Linux 进程间通信(posix消息队列 简单)实例

热门文章

  1. 高斯混合模型GMM与EM算法的Python实现
  2. Java多线程编程核心技术-第4章-Lock的使用-读书笔记
  3. java中的this关键字三种作用
  4. 测试基础【第六篇】bug要素及其生命周期
  5. JDK9下载与安装
  6. 常用dos命令(4)
  7. HTML基础二-DOM操作
  8. 总结敏捷开发之Scrum
  9. CF1174E Ehab and the Expected GCD Problem(DP,数论)
  10. python client.py