使用java配置将spring和springmvc整合(无xml)

1、创建项目

2、配置pom.xml

​
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.whty</groupId>
	<artifactId>webMvcTest</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>5.0.8.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>5.0.8.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>5.0.8.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>5.0.8.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>5.0.8.RELEASE</version>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

​

添加servlet-api  jar包

3、配置DispatcherServlet

package com.whty.config;
/**  
 * ClassName:SpitterWebInitializer <br/>  
 * Function: TODO ADD FUNCTION. <br/>  
 * Reason:   TODO ADD REASON. <br/>  
 * Date:     2018年8月18日 上午11:17:43 <br/>  
 * @author   yym  
 * @version    
 * @since    JDK 1.8  
 * @see        
 */

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import com.whty.web.WebMvcConfigure;

/**
 * ClassName: SpitterWebInitializer <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON(可选). <br/>
 * date: 2018年8月18日 上午11:17:43 <br/>
 * 
 * @author yym
 * @version
 * @since JDK 1.8
 */

public class MyWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

	/**
	 * TODO 简单描述该方法的实现功能(可选). 该方法返回的类将会用来配置ContextLoaderListener创建的应用上下文的bean
	 * 
	 */

	@Override
	protected Class<?>[] getRootConfigClasses() {

		return new Class<?>[] { RootConfigure.class };

	}

	/**
	 * TODO 简单描述该方法的实现功能(可选). 该方法返回的类将会用来定义DispatcherServlet应用上下文中的bean
	 */

	@Override
	protected Class<?>[] getServletConfigClasses() {

		return new Class<?>[] { WebMvcConfigure.class };

	}

	/**
	 * 将DispatcherServlet映射到"/"
	 * 
	 * @author yym Date: 2018年8月18日 上午11:17:43 <br/>
	 */

	@Override
	protected String[] getServletMappings() {

		return new String[] { "/" };

	}


}

最后一个方法是getServletMappings(),它会将一个或多个路径映射到DispatcherServlet上。在本例中,它映射的是“/”,这表示
它会是应用的默认Servlet。它会处理进入应用的所有请求。

 

当DispatcherServlet启动的时候,它会创建Spring应用上下文,并加载配置文件或配置类中所声明的bean。

但是在Spring Web应用中,通常还会有另外一个应用上下文。另外的这个应用上下文是由ContextLoaderListener创建的。

我们希望DispatcherServlet加载包含Web组件的bean,如控制器、视图解析器以及处理器映射,而ContextLoaderListener要
加载应用中的其他bean。

getServletConfigClasses()方法返回的带有@Configuration注解的类将会用来定义DispatcherServlet应用上下文中的
bean。

getRootConfigClasses()方法返回的带有@Configuration注解的类将会用来配置ContextLoaderListener创建的应用上下文中的bean

4、配置springmvc


/**  
 * Project Name:springAction  
 * File Name:WebMvcConfigure.java  
 * Package Name:com.whty.config  
 * Date:2018年8月21日上午9:13:17  
 * Copyright (c) 2018
 *  
*/

/**  
 * Project Name:springAction  
 * File Name:WebMvcConfigure.java  
 * Package Name:com.whty.config  
 * Date:2018年8月21日上午9:13:17  
 * @author yym 
 *  
 */

package com.whty.web;
/**  
 * ClassName:WebMvcConfigure <br/>  
 * Function: TODO ADD FUNCTION. <br/>  
 * Reason:   TODO ADD REASON. <br/>  
 * Date:     2018年8月21日 上午9:13:17 <br/>  
 * @author   yym  
 * @version    
 * @since    JDK 1.8  
 * @see        
 */

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.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

/**
 * ClassName: WebMvcConfigure <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON(可选). <br/>
 * date: 2018年8月21日 上午9:13:17 <br/>
 * 
 * @author yym
 * @version
 * @since JDK 1.8
 */
@Configuration
@EnableWebMvc
@ComponentScan({ "com.whty.web" })
public class WebMvcConfigure extends WebMvcConfigurationSupport {
	@Bean
	public ViewResolver viewResolver() {
		InternalResourceViewResolver resolver = new InternalResourceViewResolver();
		resolver.setPrefix("/view/");
		resolver.setSuffix(".jsp");
		return resolver;
	}

	/**
	 * TODO 简单描述该方法的实现功能(可选).
	 * 
	 * @see org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport#configureDefaultServletHandling(org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer)
	 */

	@Override
	public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
		configurer.enable();
	}

	/**
	 * TODO 简单描述该方法的实现功能(可选).
	 * 
	 * @see org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport#addResourceHandlers(org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry)
	 */

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		super.addResourceHandlers(registry);
	}

}

@EnableWebMvc注解驱动 相当于<mvc:annotation-driven>

@ComponentScan({ "com.whty.web" }) 启用组件扫描

配置视图解析器

@Bean
	public ViewResolver viewResolver() {
		InternalResourceViewResolver resolver = new InternalResourceViewResolver();
		resolver.setPrefix("/view/");
		resolver.setSuffix(".jsp");
		return resolver;
	}

配置静态资源的处理

将对静态资源的请求转发到Servlet容器中默认的Servlet上,而不是使用DispatcherServlet本身来处理此类请求

@Override
	public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
		configurer.enable();
	}

重写addResourceHandlers方法可以实现对静态资源的访问

5、配置spring


/**  
 * Project Name:springAction  
 * File Name:RootConfigure.java  
 * Package Name:com.whty.config  
 * Date:2018年8月18日上午11:42:06  
 * Copyright (c) 2018
 *  
*/

/**  
 * Project Name:springAction  
 * File Name:RootConfigure.java  
 * Package Name:com.whty.config  
 * Date:2018年8月18日上午11:42:06  
 * @author yym 
 *  
 */

package com.whty.config;
/**  
 * ClassName:RootConfigure <br/>  
 * Function: TODO ADD FUNCTION. <br/>  
 * Reason:   TODO ADD REASON. <br/>  
 * Date:     2018年8月18日 上午11:42:06 <br/>  
 * @author   yym  
 * @version    
 * @since    JDK 1.8  
 * @see        
 */

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;

/**
 * ClassName: RootConfigure <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON(可选). <br/>
 * date: 2018年8月18日 上午11:42:06 <br/>
 * 
 * @author yym
 * @version
 * @since JDK 1.8
 */
@Configuration
@ComponentScan(basePackages = { "com.whty" }, excludeFilters = {//excludeFilters:指定不适合组件扫描的类型。
		/*类型是注解,去除EnableWebMvc这个注解*/
		@Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class) })
public class RootConfigure {

}

6、简单的测试

写controller

@Controller
public class HomeController {
	@RequestMapping(value = "/home", method = GET)
	public String home() {
		return "home";
	}

}

配置jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<h2>欢迎</h2>

启动测试

启动成功

访问http://127.0.0.1:8080/webMvcTest/home

结果

最后附上项目结构

喜欢的点个赞!

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值