Spring(三):使用java config配置spring mvc

时间:2022-05-25 20:33:28

一.配置DispatcherServlet
通常使用web.xml配置,此处使用java代码配置
SpittrWebAppInitializer.java

package spittr.config;

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

public class SpittrWebAppInitializer
extends AbstractAnnotationConfigDispatcherServletInitializer{


@Override
protected String[] getServletMappings(){
return new String[]{"/"};
}

@Override
protected Class<?>[] getRootConfigClasses(){
return new Class<?>[] {RootConfig.class};
}

@Override
protected Class<?>[] getServletConfigClasses(){
return new Class<?>[] {WebConfig.class};
}
}

spring会自动查找继承AbstractDispatcherServletInitializer的类用来配置servlet容器,AbstractAnnotationConfigDispatcherServletInitializer继承了AbstractDispatcherServletInitializer,所以spring会自动查找到SpittrWebAppInitializer,用来配置servlet;
getServletMappings表示将/为前缀的路径映射到此servlet;getServletConfigClasses方法返回的类,DispatchServlet加载应用上下文时,会使用这些配置类中的bean;getRootConfigClasses方法返回的类,当ContextLoaderListener创建上下文时,会加载这些类中配置的bean;

二.配置spring mvc
WebConfig.java

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;


@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "spittr.web")
public class WebConfig extends WebMvcConfigurerAdapter {

@Bean
public ViewResolver viewResolver(){
InternalResourceViewResolver resolver =
new InternalResourceViewResolver();

resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);

return resolver;
}

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

}

@Configuration注解标识这是一个配置类,@EnableWebMvc注解启用spring mvc,@ComponentScan注解配置要扫描的组件,根据此配置来查找控制器;viewResolver方法,返回jsp视图解析器,用于映射jsp;configureDefaultServletHandling方法配置默认的sevlet,当请求匹配不到DispatchServlet的映射路径时,会使用默认的Servlet;

ContextLoaderListener使用的配置:
RootConfig.java

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;

@Configuration
@ComponentScan(basePackages={"spitter"},
excludeFilters={
@Filter(type=FilterType.ANNOTATION, value=EnableWebMvc.class)
})
public class RootConfig {

}

三.编写控制器
HomeController.java

package spittr.web;

import static org.springframework.web.bind.annotation.RequestMethod.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

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

@Controller注解基于@Component,会产生一个bean,此控制器的方法home根据注解@RequestMapping(value=”/”, method=GET),匹配到url的/,方法是GET时,返回home,视图解析器,根据home,找到/WEB-INF/views/home.jsp返回;需要添加文件/WEB-INF/views/home.jsp
/WEB-INF/views/home.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
<title>Spittr</title>
<link rel="stylesheet"
type="text/css"
href="<c:url value="/resources/style.css" />
" >

</head>

<body>
<h1>Welcome to Spitter</h1>

<a href="<c:url value="/spittles" />">Spittles</a>
<a href="<c:url value="/spitter/register" />">Register</a>
</body>
</html>

注意,解析此jsp文件依赖jar包jstl.jar、standard.jar,需要放到tomcat的lib或者应用的lib目录下;

四.测试
在tomcat的webapps下创建目录spittr,把类copy到webapps\spittr\WEB-INF\classes下,home.jsp文件copy到webapps\spittr\WEB-INF\views\home.jsp,启动tomcat
浏览器访问http://127.0.0.1:8080/spittr/
显示

Welcome to Spitter

Spittles Register

java config和xml配置的对应关系,参考Servlet 3 + Spring MVC零配置:去除所有xml