1. 创建模块 springboot-webmvc
2. 添加 maven 依赖,设置打包方式,引入打包插件
代码语言:javascript
复制<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dive-in-spring-boot</artifactId>
<groupId>com.imooc</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springboot-webmvc</artifactId>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3. 创建引导类
代码语言:javascript
复制package com.imooc.webmvc.bootstrap;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Spring Boot Web MVC 引导类
*/
@SpringBootApplication(scanBasePackages = "com.imooc.webmvc")
public class SpringBootWebMvcBootstrap {
public static void main(String[] args) {
SpringApplication.run(SpringBootWebMvcBootstrap.class, args);
}
}
4. 创建 Controller 和切面通知、异常处理
代码语言:javascript
复制package com.imooc.webmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class HelloWorldController {
@RequestMapping("")
public String index(@RequestParam int value) {
return "index";
}
}
代码语言:javascript
复制package com.imooc.webmvc.controller;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestHeader;
@ControllerAdvice(assignableTypes = HelloWorldController.class)
public class HelloWorldControllerAdvice {
@ModelAttribute("message")
public String message() {
return "Hello World !";
}
@ModelAttribute("acceptLanguage")
public String acceptLanguage(@RequestHeader("Accept-Language") String acceptLanguage) {
return acceptLanguage;
}
@ExceptionHandler(Throwable.class)
public ResponseEntity<String> onException(Throwable throwable) {
return ResponseEntity.ok(throwable.getMessage());
}
}
5. 添加 Spring Web MVC 配置类
代码语言:javascript
复制package com.imooc.webmvc.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Spring Web MVC 配置类
*/
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new HandlerInterceptor() {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("拦截中 ...");
return true;
}
});
}
}
6. 添加 jsp 文件前后缀配置
- 创建
resources/application.properties
文件
代码语言:javascript
复制spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
7. 创建页面文件
- 创建
src/main/webapp/WEB-INF/jsp/index.jsp
文件
代码语言:javascript
复制<jsp:root version="2.0" xmlns:jsp="http://java.sun.com/JSP/Page">
<body>
<message>${message}</message>
<accept-language>${acceptLanguage}</accept-language>
</body>
</jsp:root>
8. 打包项目
代码语言:javascript
复制mvn -DMaven.test.skip -U clean package
9. 运行项目
代码语言:javascript
复制java -jar target/springboot-webmvc-0.0.1-SNAPSHOT.war
10. 测试
- 正常测试(http://localhost:8080/?value=1)
- 异常测试(http://localhost:8080)