去掉web.xml
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package Servlet3;
- import Config.AppConfig;
- import Config.DispatcherConfig;
- import javax.servlet.ServletContext;
- import javax.servlet.ServletException;
- import javax.servlet.ServletRegistration;
- import org.springframework.web.WebApplicationInitializer;
- import org.springframework.web.context.ContextLoaderListener;
- import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
- import org.springframework.web.context.support.XmlWebApplicationContext;
- import org.springframework.web.servlet.DispatcherServlet;
- /**
- *
- * @author Administrator
- */
- public class MyWebAppInitializer implements WebApplicationInitializer {
- @Override
- public void onStartup(ServletContext container) {
- AnnotationConfigWebApplicationContext dispatcherContext =new AnnotationConfigWebApplicationContext();
- dispatcherContext.register(DispatcherConfig.class);
- //Register and map the dispatcher servlet
- ServletRegistration.Dynamic dispatcher =container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
- dispatcher.setLoadOnStartup(1);
- dispatcher.addMapping("*.html");
- }
- }
- package Config;
- import org.springframework.context.annotation.Bean;
- 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.view.InternalResourceViewResolver;
- @Configuration//相当于<mvc:annotation-driven/>
- @EnableWebMvc//包含<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
- @ComponentScan(basePackages={"Controller"})//相当于<context:component-scan base-package="Controller"/>
- public class DispatcherConfig {
- @Bean
- public InternalResourceViewResolver viewResolver() {
- InternalResourceViewResolver resolver = new InternalResourceViewResolver();
- resolver.setPrefix("/WEB-INF/jsp/");
- resolver.setSuffix(".jsp");
- return resolver;
- }
- }
3.控制器
- package Controller;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- @Controller
- public class IndexController {
- @RequestMapping("index")
- public String hello(){
- return "index";
- }
- }
index.html
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Page</title>
- </head>
- <body>
- <h1>pass</h1>
- </body>
- </html>