目录结构

web.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <!-- Configure ContextLoaderListener to use AnnotationConfigWebApplicationContext
  4. instead of the default XmlWebApplicationContext -->
  5. <context-param>
  6. <param-name>contextClass</param-name>
  7. <param-value>
  8. org.springframework.web.context.support.AnnotationConfigWebApplicationContext
  9. </param-value>
  10. </context-param>
  11. <!-- Configuration locations must consist of one or more comma- or space-delimited
  12. fully-qualified @Configuration classes. Fully-qualified packages may also be
  13. specified for component-scanning -->
  14. <context-param>
  15. <param-name>contextConfigLocation</param-name>
  16. <param-value>springdemo.config.AppConfig</param-value>
  17. </context-param>
  18. <!-- Bootstrap the root application context as usual using ContextLoaderListener -->
  19. <listener>
  20. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  21. </listener>
  22. <!-- Declare a Spring MVC DispatcherServlet as usual -->
  23. <servlet>
  24. <servlet-name>dispatcher</servlet-name>
  25. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  26. <!-- Configure DispatcherServlet to use AnnotationConfigWebApplicationContext
  27. instead of the default XmlWebApplicationContext -->
  28. <init-param>
  29. <param-name>contextClass</param-name>
  30. <param-value>
  31. org.springframework.web.context.support.AnnotationConfigWebApplicationContext
  32. </param-value>
  33. </init-param>
  34. <!-- Again, config locations must consist of one or more comma- or space-delimited
  35. and fully-qualified @Configuration classes -->
  36. <init-param>
  37. <param-name>contextConfigLocation</param-name>
  38. <param-value>springdemo.config.MvcConfig</param-value>
  39. </init-param>
  40. </servlet>
  41. <!-- map all requests for /app/* to the dispatcher servlet -->
  42. <servlet-mapping>
  43. <servlet-name>dispatcher</servlet-name>
  44. <url-pattern>/</url-pattern>
  45. </servlet-mapping>
  46. </web-app>

SpringMVC配置类:

  1. @EnableWebMvc
  2. @Configuration
  3. @ComponentScan(basePackages={"springdemo.controller"})
  4. public class MvcConfig {
  5. /**
  6. * 配置JSP视图解析器
  7. * @return
  8. */
  9. @Bean
  10. public ViewResolver viewResolver(){
  11. InternalResourceViewResolver resolver = new InternalResourceViewResolver();
  12. resolver.setPrefix("/WEB-INF/views/");
  13. resolver.setSuffix(".jsp");
  14. resolver.setExposeContextBeansAsAttributes(true);
  15. return resolver;
  16. }
  17. /**
  18. * 配置默认的Servlet来处理静态资源
  19. * @return
  20. */
  21. @Bean
  22. public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
  23. return new WebMvcConfigurerAdapter() {
  24. @Override
  25. public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
  26. configurer.enable();
  27. }
  28. };
  29. }
  30. }

Controller:

  1. @Controller
  2. public class CoffeeController {
  3. @RequestMapping(value="/show",method=RequestMethod.GET)
  4. public String showCoffee(){
  5. return "coffee";
  6. }
  7. }

测试类:

  1. @RunWith(SpringRunner.class)
  2. @WebAppConfiguration
  3. @ContextConfiguration(classes={MvcConfig.class})
  4. public class CoffeeTest {
  5. private MockMvc mockMvc;
  6. @Autowired
  7. private WebApplicationContext wac;
  8. @Before
  9. public void init(){
  10. mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
  11. }
  12. @Test
  13. public void testShowCoffee() throws Exception{
  14. ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders.get("/show"));
  15. resultActions.andExpect(MockMvcResultMatchers.view().name("coffee"));
  16. }
  17. }

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

  1. public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{
  2. @Override
  3. protected Class<?>[] getRootConfigClasses() {
  4. return new Class<?>[]{AppConfig.class};
  5. }
  6. @Override
  7. protected Class<?>[] getServletConfigClasses() {
  8. return new Class<?>[]{MvcConfig.class};
  9. }
  10. @Override
  11. protected String[] getServletMappings() {
  12. return new String[]{"/"};
  13. }
  14. }

pom.xml :Spring pom.xml配置

使用JavaConfig配置SpringMVC的更多相关文章

  1. 使用Java配置SpringMVC

    在此之前,一直使用的是XML的方式配置SpringMVC,现在为了适应Servlert3.0以及JavaConfig的Spring配置方式,在这里记录一下使用Java代码配置SpringMVC.首先, ...

  2. spring mvc:练习:javaConfig配置和注解

    Spring4 MVC HelloWorld 注释/JavaConfig为示例,一步一步以简单的方式学习Spring4 MVC 的注解,项目设置,代码,部署和运行. 我们已经使用XML配置开发了一个H ...

  3. 第一节(配置springmvc环境)学习尚硅谷-springmvc视频教程

    之前,一直从事C#开发.后来,公司调整后领导决定使用java开发,因此需要收集相关学习资料.该视频教程比较入门,也适合自己,于是边看边写的同时再总结一下便于自己牢记,遇到分歧不对之处望指正. 开发环境 ...

  4. 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建四:配置springmvc

    在用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建三:配置spring并测试的基础上 继续进行springmvc的配置 一:配置完善web.xml文件

  5. Spring 使用javaconfig配置

    除了使用xml,spring提供javaconfig配置,下面是简单的例子: 1.声明接口 /** * */ package com.junge.demo.spring.service; /** * ...

  6. 第7章—SpringMVC高级技术—不用web.xml,而使用java类配置SpringMVC

    不用web.xml,而使用java类配置SpringMVC DispatcherServlet是Spring MVC的核心,按照传统方式, 需要把它配置到web.xml中. 我个人比较不喜欢XML配置 ...

  7. spring boot配置springMVC拦截器

    spring boot通过配置springMVC拦截器 配置拦截器比较简单, spring boot配置拦截器, 重写preHandle方法. 1.配置拦截器: 2重写方法 这样就实现了拦截器. 其中 ...

  8. 配置springMVC时出现的问题

    配置springMVC时出现的问题 项目结构如图:

  9. myeclipse配置springmvc教程

    之前一直是使用Eclipse创建Web项目,用IDEA和MyEclipse的创建SpringMVC项目的时候时不时会遇到一些问题,这里把这个过程记录一下,希望能帮助到那些有需要的朋友.我是用的是MyE ...

随机推荐

  1. 汉诺塔问题深度剖析(python实现)

    当我们学习一门编程语言的时候,都会遇到递归函数这个问题.而学习递归的一个经典案例就是汉诺塔问题.通过这篇文章,观察移动三个盘子和四个盘子的详细过程,您不仅可以深刻的了解递归,也更加熟悉了汉诺塔的游戏的 ...

  2. 【Js】创建对象的6种方式总结、(底部包含属性名为动态的形式)

    一.new 操作符 + Object 创建对象 1 var person = new Object(); 2 person.name = "lisi"; 3 person.age ...

  3. Vinagre(Gnome Remote Desktop) cannot connect to RDP Server

    In a Sudden , this client cannot be used. Tried to install kdenetwork-krdc , then found there are de ...

  4. 请指出document load和document ready的区别

    document load文档的所有内容都加载完成 document ready文档的DOM加载完成

  5. pacemaker和keepalived的区别

    1.pacemaker Pacemaker 是一款开源的高可用资源管理软件,适合大集群或者小集群. Pacemaker 由Novell支持,SLES HAE就是用Pacemaker来管理集群,并且Pa ...

  6. 在 Debian 上的 SQL Server 的安裝指引

    我想在 linux 环境下尝试一下 Microsoft SQL Server,但是微软只发布了针对 Red Hat,SUSE,Ubuntu 和 Docker 引擎的.我平时习惯使用 Debian, U ...

  7. QFileInfo().created() 警告 created is deprecated 怎么改?

    有这样一行代码操作: QFileInfo(...).created().toString(...); QtCreator提示警告: 'created' is deprecated 'created' ...

  8. SQL数据同步到ELK(四)- 利用SQL SERVER Track Data相关功能同步数据(上)

    一.相关文档 老规矩,为了避免我的解释误导大家,请大家务必通过官网了解一波SQL SERVER的相关功能. 文档地址: 整体介绍文档:https://docs.microsoft.com/en-us/ ...

  9. SQL2008R2下数据库修复一例

    某天访问某个数据库的时候,系统报错.连上去看了一下,服务器是SQL2008R2.由于有上次修复的经验,先使用DBCC查看数据库情况. DBCC的返回: XXXXXXXXXXX发生异常数据库 ID 7, ...

  10. 解决myeclipse2017 properties中文被Unicode编码

    输入:http://propedit.sourceforge.jp/eclipse/updates/ 在线安装插件解决.