目录结构

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配置

使用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. 让istio中的jaeger跑起来

    现在的水平,仅止于让它跑起来.:) 同样的环境,microk8s+istio. 步骤如下: 一,使用kubectl get pod -n istio-system查看所有istio的POD运行正常. ...

  2. Codeforces Round #605 (Div. 3)

    地址:http://codeforces.com/contest/1272 A. Three Friends 仔细读题能够发现|a-b| + |a-c| + |b-c| = |R-L|*2 (其中L ...

  3. 解决tomcat出现乱码问题---韦大仙

    1. 改这两个文件 URIEncoding="UTF-8" 2.然后重启idea

  4. MVC Core 使用TagHelper扩展几个插件

    未完待续... 案例1:按钮权限校验,无权限不显示,利用TagHelper控制元素是否输出 [HtmlTargetElement("tc-permission", TagStruc ...

  5. 排序算法-快速排序(Java)

    package com.rao.sort; import java.util.Arrays; /** * @author Srao * @className QuickSort * @date 201 ...

  6. Win10解决修改host没有权限问题(其他文件同理) 一步都不能少哦:先添加再授权

    Step1:右键文件选择属性,选择安全,点击编辑: Step2:在弹窗中点击添加,在弹窗中点击高级: Step3:在弹窗中点击立即查找,选中当前用户,点击确定: Step4:此时选中用户已经被加入进来 ...

  7. Spring Cloud微服务安全实战_3-1_API安全 常见的安全机制

    本章讲解,在不考虑微服务,只考虑一个简单的API ,如何保证这个API的安全? 三个问题: 1,什么是API ? 2,API安全的要素有哪些? 3,API安全基本机制 一.什么是API 百度百科:AP ...

  8. 研究下vc++的abort函数

    最近在调试几个问题时,发现跟abort函数有关,以前只是简单使用,现在却发现不简单,就多留意了下. 简介 abort中止当前进程并返回错误代码.异常终止一个进程.中止当前进程,返回一个错误代码.错误代 ...

  9. java 动态数组

    package testjavapro; import java.util.*; public class testjava { public static void main(String args ...

  10. 使用Redis搭建电商秒杀系统

    背景 秒杀活动是绝大部分电商选择的低价促销.推广品牌的方式.不仅可以给平台带来用户量,还可以提高平台知名度.一个好的秒杀系统,可以提高平台系统的稳定性和公平性,获得更好的用户体验,提升平台的口碑,从而 ...