1: 创建spring boot项目
使用 Spring initializr 可以直接选择创建包的方式
也可以选择在Pom中更改
<groupId>com.dgwgroupId> <artifactId>demoartifactId> <version>0.0.1-SNAPSHOTversion> <packaging>warpackaging> <name>demoname> <description>Demo project for Spring Bootdescription>
2: 创建初始化类
使用 Spring initializr 自动已经为我们创建完成了, 当然可以选择手动创建下面的初始化器 继承 SpringBootServletInitializer
public class ServletInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(DemoApplication.class); } }
2.1配置视图解析器访问路径
在application.properties 或者 yaml 下配置
spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp
3: 创建webapp目录
Spring Boot 在 Project Structure 中创建
Web Resource 创建 这个路径
\src\main\webapp
Deployment Descriptors 创建web.xml
# 路径
\src\main\webapp\WEB-INF\web.xml
web.xml配置信息
xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> web-app>
正常创建的目录结构
4: 配置测试
创建控制器
@RestController public class FisrtController { @GetMapping("/sayhi") public ModelAndView sayHi(){ ModelAndView andView = new ModelAndView(); andView.setViewName("success"); andView.addObject("msg","HELLO"); return andView; } }
webapp 路径 创建index.jsp文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Titletitle> head> <body> <a href="sayhi">send msga> body> html>
webapp\WEB-INF 下创建success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Titletitle> head> <body> <h2>${msg}h2> body> html>
结果:
附录 POM:
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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0modelVersion> <parent> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-parentartifactId> <version>2.2.2.RELEASEversion> <relativePath/> parent> <groupId>com.dgwgroupId> <artifactId>demoartifactId> <version>0.0.1-SNAPSHOTversion> <packaging>warpackaging> <name>demoname> <description>Demo project for Spring Bootdescription> <properties> <java.version>1.8java.version> properties> <dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-tomcatartifactId> <scope>providedscope> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-testartifactId> <scope>testscope> <exclusions> <exclusion> <groupId>org.junit.vintagegroupId> <artifactId>junit-vintage-engineartifactId> exclusion> exclusions> dependency> dependencies> <build> <plugins> <plugin> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-maven-pluginartifactId> plugin> plugins> build> project>