前言:本文是springMVC的入门级案例,采用完全注解式开发
versions:
java - 1.8
tomcat - 8.5
spring - 5.3.1
gitee地址:https://gitee.com/liubin2000/springMVC-firstDemo
SpringBoot太便捷了,就想看看原来的SpringMVC怎么手动配置的。doge.jpg
一、项目依赖
- pom.xml
1.8
1.8
1.8
war
org.springframework
spring-webmvc
5.3.1
ch.qos.logback
logback-classic
1.2.3
javax.servlet
javax.servlet-api
3.1.0
provided
org.thymeleaf
thymeleaf-spring5
3.0.12.RELEASE
com.fasterxml.jackson.core
jackson-databind
2.12.4
commons-fileupload
commons-fileupload
1.3.1
二、使用Java配置类代替web.xml 和 springMVC.xml
1. src文件目录
|-- src
|-- main
| |-- java
| | |-- com
| | |-- lb
| | |-- mvc
| | |-- config
| | | |-- SpringConfig.java
| | | |-- WebConfig.java
| | | |-- WebInit.java
| | |-- controller
| | | |-- ExceptionController.java
| | | |-- TestController.java
| | |-- interceptors
| | |-- MyInterceptor.java
| |-- resources
| |-- webapp
| |-- WEB-INF
| |-- templates
| |-- error.html
| |-- index.html
| |-- success.html
|-- test
|-- java
2.WebInit类
因其继承于AbstractAnnotationConfigDispatcherServletInitializer类,因此Tomcat将会自动加载这个类
public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer {
/**
* 指定spring的配置类
*
* @return
*/
@Override
protected Class>[] getRootConfigClasses() {
return new Class[]{SpringConfig.class};
}
/**
* 指定SpringMVC的配置类
*
* @return
*/
@Override
protected Class>[] getServletConfigClasses() {
return new Class[]{WebConfig.class};
}
/**
* 指定DispatcherServlet的映射规则,
*
* @return
*/
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
/**
* 注册过滤器
*
* @return
*/
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter("UTF-8", true);
HiddenHttpMethodFilter hiddenHttpMethodFilter = new HiddenHttpMethodFilter();
return new Filter[]{characterEncodingFilter, hiddenHttpMethodFilter};
}
}
3. SpringConfig类
最基础的mvc项目不用配置该类,仅做个标识
@Configuration
public class SpringConfig {
}
4. WebConfig类
/** * 该类用于代替SpringMVC的配置文件,可以实现如下功能的配置 * 1. 注解扫描 2. 视图解析器 3. view-controller 4. default-servlet-handler * 5. mvc注解驱动 6. 文件上传解析器 7. 异常处理 8.拦截器 */
@Configuration
@ComponentScan(basePackages = {"com.lb.mvc"}) // 1. 注解扫描
@EnableWebMvc // 5. mvc注解驱动
public class WebConfig implements WebMvcConfigurer {
//4. default-servlet-handler
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
// 8.拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyInterceptor())
.addPathPatterns("/**"); // 拦截所有路径
}
// 3. view-controller
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
}
// 6. 文件上传解析器
@Bean
public CommonsMultipartResolver multipartResolver() {
return new CommonsMultipartResolver();
}
//7. 异常处理 , 也可以另外写类加上@ControllerAdvice
/*@Override
public void configureHandlerExceptionResolvers(List resolvers) {
SimpleMappingExceptionResolver exceptionResolver = new SimpleMappingExceptionResolver();
Properties properties = new Properties();
properties.setProperty("ArithmeticException", "error");
exceptionResolver.setExceptionMappings(properties);
exceptionResolver.setExceptionAttribute("exception");
resolvers.add(exceptionResolver);
}*/
/**
* 配置模板解析器
*
* @return
*/
@Bean
public ITemplateResolver templateResolver() {
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
//ServletContextTemplateResolver 需要一个 ServletContext 作为构造参数,可以通过 WebApplicationContext 获得
ServletContextTemplateResolver templateResolver =
new ServletContextTemplateResolver(webApplicationContext.getServletContext());
templateResolver.setPrefix("/WEB-INF/templates/"); // 视图前缀
templateResolver.setSuffix(".html"); // 视图后缀
templateResolver.setCharacterEncoding("UTF-8");
templateResolver.setTemplateMode(TemplateMode.HTML);
return templateResolver;
}
/**
* 配置模板引擎
*
* @param templateResolver 模板解析器
* @return
*/
@Bean
public SpringTemplateEngine templateEngine(ITemplateResolver templateResolver) {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
return templateEngine;
}
/**
* 配置视图解析器, thymeleaf
*
* @param templateEngine 模板引擎
* @return
*/
@Bean
public ViewResolver viewResolver(SpringTemplateEngine templateEngine) {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setOrder(1);
viewResolver.setCharacterEncoding("UTF-8");
viewResolver.setTemplateEngine(templateEngine);
return viewResolver;
}
}
5. MyInterceptor类
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("拦截器配置成功");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
6. ExceptionController类
异常处理类
@ControllerAdvice
public class ExceptionController {
@ExceptionHandler(value = {ArithmeticException.class, NullPointerException.class})
public String test(Exception e, Model model) {
model.addAttribute("exception", e); // 将异常类共享到request域中
return "error"; // 转发到error视图
}
}
7. TestController类
@Controller
public class TestController {
@RequestMapping("/testArithmeticException")
public String testArithmeticException() {
int i = 10 / 0;
return "success";
}
@RequestMapping("/testNullPointerException")
public String testNullPointerException() {
Object o = null;
System.out.println(o.hashCode());
return "success";
}
}
8. index、success、error页面
index
首页
success
success
error
别气馁,马上就要成功了,还差最后一步!
三、Idea集成Tomcat
- step - 1, 点击Idea中的 Add Configurations,下图红圈部分
- step - 2,点击左上角的+号,添加一个Tomcat server
- step - 3,选择本地安装好的tomcat,并起个名字
以我自己的为例:
- step - 4,最后一步,配置我们要运行的项目,首先选择
Deployement
选项,然后点击右侧+号,选择aritifacts
, - 选择当前项目的 :war exploded
例:
到此结束,配置完成,点击add configuration边上的 run/debug 即可运行项目