1. 新建 SpringMVC 的 Web 项目
2. 代码编写
代码参考 《Spring 实战》(第四版),本文和书中代码略有差异
删除不需要的配置文件
编写 JavaConfig 文件
package com.yangrd.springmvc.config; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; public class HelloWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { System.out.println("getRootConfig"); return new Class<?>[]{RootConfig.class}; } @Override protected Class<?>[] getServletConfigClasses() { System.out.println("getServletConfig"); return new Class<?> []{WebConfig.class}; } @Override protected String[] getServletMappings() { System.out.println("getServletMappings"); return new String[]{"/"}; } }
新建配置文件 RootConfig.java
package com.yangrd.springmvc.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; import org.springframework.web.servlet.config.annotation.EnableWebMvc; @Configuration @ComponentScan(basePackages = {"com.yangrd.springmvc"}, excludeFilters = {@ComponentScan.Filter(type= FilterType.ANNOTATION,value = EnableWebMvc.class)}) public class RootConfig { }
新建配置文件 WebConfig.java
package com.yangrd.springmvc.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("com.yangrd.springmvc.controller") public class WebConfig extends WebMvcConfigurerAdapter { @Bean public ViewResolver viewResolver(){ InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".html"); resolver.setExposeContextBeansAsAttributes(true); return resolver; } @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){ configurer.enable(); } }
编写 Controller
package com.yangrd.springmvc.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import static org.springframework.web.bind.annotation.RequestMethod.GET; /** * */ @Controller //声明为一个控制器 public class HelloController { @RequestMapping(value = "/home",method = GET)//处理对 “/” 的 GET 请求 public String hello(){ return "hello"; //逻辑视图名为hello } }
编写 view 文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Hello</title> </head> <body> hello world </body> </html>
3. Tomcat 的配置和启动
配置tomcat服务
将Sping MVC 相关包放到 Web 工程 中的 lib 下
启动tomcat
这是启动tomcat 会报错
Error:(5, 8) java: 无法访问javax.servlet.ServletException
找不到javax.servlet.ServletException的类文件
这时需要添加 javax.servlet-api
4. 测试
浏览器访问 http://localhost:8080/home
显示
hello world