- Spring mvc 是子容器
- Spring 是 父容器
===================================================================
public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { RootConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { App1Config.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/app1/*" };
}
}
刚才我们是写个类实现人家的接口,现在我们写个类继承人家的接口
===================================================================
===================================================================
- 继承AbstractAnnotationConfigDispatcherServletInitializer
package com.min.demo1;
import com.min.config.App1Config;
import com.min.config.RootConfig;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { RootConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { App1Config.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "*.do" };
}
}
===================================================================
- 编写配置类
**App1Config **
package com.min.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(value = "com.min.controller")
public class App1Config {
}
**RootConfig **
package com.min.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(value = "com.min.service")
public class RootConfig {
}
===================================================================
- 编写Controller
package com.min.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class TestController {
@RequestMapping(value = "/hello2.*")
@ResponseBody
public String hello(){
return "hello";
}
}
- 访问hello2.do
===================================================================
===================================================================
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Override
public void addFormatters(FormatterRegistry registry) {
// ...
}
}
===================================================================
默认实现方法
===================================================================
- 添加跳转jsp视图(配置视图解析器)
package com.min.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@ComponentScan(value = "com.min.controller")
@EnableWebMvc
public class App1Config implements WebMvcConfigurer {
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
/*
* public UrlBasedViewResolverRegistration jsp() {
* return this.jsp("/WEB-INF/", ".jsp");
* }
*/
//registry.jsp();
registry.jsp("/WEB-INF/jsps/",".jsp");
}
}
- 编写controlelr
package com.min.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class TestController {
@RequestMapping(value = "/hello.do")
@ResponseBody
public String hello(){
return "hello";
}
@RequestMapping(value = "/hello2.do")
public String hello2(){
return "a";
}
}
- /web-inf/jsps/下编写a.jsp页面
<%--
Created by IntelliJ IDEA.
User: lenovo
Date: 2020/6/5
Time: 9:20
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>我是JSP页面视图</h1>
</body>
</html>
- App1Config 还可以配置拦截器和视图解析器